During the page_load, I disable the timer. When I pressed Button1, I enable the timer, but the page refreshes. Therefore, it never reaches the timer_tick1. I need to show a popup after a certain amount of time a button is clicked. How do I prevent the refresh from happening?
Alerts Class
public static class Alert
{
public static void Show(string message, Page page)
{
// replaces the quotations to follow the script syntax
// quotations are interpretated as \\' in script code
string cleanMessage = message.Replace("'", "\\'");
string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
// Gets the executing web page
Page tempPage = page;
// Checks if the handler is a Page and that the script isn't already on the page
if (tempPage != null & !tempPage.ClientScript.IsClientScriptBlockRegistered("alert"))
{
tempPage.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script); // this isn't working, but it works on a button click event.
}
}
}
Page Class
public partial class Test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback) {
Timer1.Enabled = false;
Label2.Text = "Panel refreshed at: " +
DateTime.Now.ToLongTimeString(); // Checks if page reloads
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{ // i added a breakpoint here. It doesn't even pass through.
Alert.Show("hehehehe", this); //PopUp Shows up.
Timer1.Enabled = false; //Cancels Timer
Label1.Text = "Panel refreshed at: " +
DateTime.Now.ToLongTimeString(); // Checks if update panel reloads
}
protected void Button1_Click1(object sender, EventArgs e)
{
Timer1.Enabled = true; //Starts Timer. It seems to refresh the page.
}
}
script
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" %>
<%# Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function delayer() {
setTimeout (function () {ShowPopUp()}, 15000);
}
delayer();
</script>
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" Enabled="true">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="PanelNotRefreshedYet"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="ShowPopUp();" />
</form>
</body>
</html>
I think you're confused. Timer1 is a server side control. So it will fire on the server side, if you're still processing the page, that is, and will have no effect on the client side. By the time it fires in your code, the page has likely already rendered so you'll see no effect from that Timer1 object's Timer1_Tick event. Since the page has completed rendering, you can't inject new JavaScript, modify the page, or anything like that. Remember that web development is a disconnected thing. You send a request, you get a response. There are no events by nature of the web. There are libraries out there for triggering events and such but I think that's way beyond what you're trying to achieve.
For client side "timer" you need to use JavaScript setTimeout method, which you have verified as working and is the proper way for you to achieve the delay you're looking to implement.
setTimeout (function () {ShowPopUp()}, 15000);
If you still want to do it in your Alert class, then get rid of Timer1 and have your Alert class inject the timeout in JavaScript:
protected void Button1_Click1(object sender, EventArgs e)
{
Alert.Show("He heee", this);
}
And in Alert, change your script to:
string script = "<script type=\"text/javascript\">setTimeout(function() {alert('" + cleanMessage + "');}, 15000);</script>";
Your button is doing a postback, so yes the page will be refreshed and your Page_Load function will run again. You should test for this using the IsPostback property.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback) {
Timer1.Enabled = false;
Label2.Text = "Panel refreshed at: " +
DateTime.Now.ToLongTimeString(); // Checks if page reloads
}
}
You might want to look at showing the alert using JavaScript on the page rather than running it server side tho.
<script type="text/javascript">
function showPopup()
{
alert("Hey, click something already");
}
function delayer() {
setTimeout (showPopUp, 15000);
}
delayer();
</script>
Just put your message like this. Probably easier if your logic is simple.
Related
So there's a ListView element on my ASP.NET page that I need to be able to update from the code behind. To my understanding, Microsoft has prepared UpdatePanels and DataBindung for exactly such purpose, allowing me to "bind" the ListView's content to a property member in the code behind and promising to take care of updating the browser's view automatically (?) when the property changes.
However, only the initial load of items via GetStuff() works; I can see in the debug console that my timer keeps adding new elements to the List, but those never arrive in the browser's view. What am I missing?
In Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="myLittleProject.Default" %>
<%# Register Src="~/StuffListItemControl.ascx" TagPrefix="stf" TagName="StuffListItem" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<!-- some irrelevant stuff -->
</head>
<bod>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<!-- some irrelevant stuff -->
<asp:UpdatePanel runat="server" ID="StuffUpdatePanel" UpdateMode="Always">
<ContentTemplate>
<ul>
<asp:ListView ID="StuffBoundContent" runat="server">
<ItemTemplate>
<stf:StuffListItem runat="server" ID="StuffListItemControl" />
</ItemTemplate>
</asp:ListView>
</ul>
</ContentTemplate>
</asp:UpdatePanel>
<!-- some more irrelevant stuff -->
</form>
</body>
</html>
And in Default.aspx.cs:
using System.Collections.Generic;
namespace myLittleProject
{
public partial class Default : System.Web.UI.Page
{
public static List<Stuff> StuffContent;
protected void Page_Load(object sender, EventArgs e)
{
StuffContent = Stuff.GetStuff(); // returns a list of three elements from the database
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 3000;
t.Elapsed += T_Tick;
t.Enabled = true;
}
protected void Page_PreRender(object sender, EventArgs e)
{
StuffBoundContent.DataSource = StuffContent;
StuffBoundContent.DataBind();
}
private void T_Tick(object sender, EventArgs e)
{
StuffContent.Add(new Stuff(StuffContent.Count + 1, DateTime.Now.ToShortDateString(), new string[] { "what", "ever" }));
System.Diagnostics.Debug.WriteLine("[TIMER EVENT] StuffContent.Count = " + StuffContent.Count.ToString());
}
}
}
System.Timers.Timer does not work with a webpage. The reason that t is disposed after the page is sent to the client. Use a Timer control if you really want to use one.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>
And then you can update the ListView in Timer1_Tick.
protected void Timer1_Tick(object sender, EventArgs e)
{
//update the ListView
}
Place the Timer Control inside the UPdatePanel if you do not want a full postback when the timer is triggered.
Another thing to remember is that although you use an UpdatePanel, a complete page life cycle is triggered. So all other code you use in Page Load (and PrerRender) is executed even when only the ListView update is visible to the user. This could put a huge load on the server when an updatepanel is triggered every few seconds. Maybe better use Ajax.
PS you don't need to use Page_PreRender to bind data.
Yesterday I wrote simle example of updating UpdatePanel on timer.tick event. I noticed that on timer.tick event code of my form class is running from the beginning. Why? How to avoid it?
WebForm1.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestApp.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional" onload="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs:
namespace TestApp
{
public partial class WebForm1 : System.Web.UI.Page
{
static Random rnd = new Random();
int b = rnd.Next(100); // always 1 value
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
UpdatePanel1.Update();
}
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
int a = rnd.Next(100); // changing value
Label1.Text = a.ToString() + " - changing value<br />" + b.ToString() + " - static value";
}
}
}
Why: Because the timer is enabled when the page loads. When the time you sit elapsed it will cause a postback to the server which will reload your page again, and this is why you see the debugger goes at the beginning of you form.
How to avoid it: Simply set Enabled property to false on page load, and then set it to true whenever you want the timer to start.
Documentation from MSDN "The Tick event is raised when the number of milliseconds specified in the Interval property has elapsed either since the Web page was rendered or since the previous Tick event."
I am posting this question again, maybe this time more accurate description.
The problem is , I am using jQuery to set the Label's text value and it works fine on browser, but when I want to save it to string, it does not save it. Here is the
front End Html Code.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And here is the Back End C# Code On Page Load
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
The result displayed is
Name's not Fine
Ronaldo
Seems like the string is still Null. Is there any problem of rendering??
label is not input type so you can not get changed values through jquery on server side. You can use hidden field for this purpose.
Your server side code (c#) can not access the form data until your client side code (HTML/Javascript) posts it.
Why do you want to the name already at the PageLoad event?
You could add a asp:Button with an attached onClick event handler to read the value of your asp:Label.
Labels do not maintain viewstate. The server will not post that information back to the server. You can try explicitly enabling the ViewState on your Label, but if that doesn't work, you will have to store that value in a hidden field.
First Call Page Load event and after that call JQuery Window.Load event.
So if you want to set any content in Label then you can do using onClientClick of button.
For ex.
<asp:Button ID="btn" runat="server" Text="Click me" OnClientClick="SetClientValues();" />
<script type="text/javascript">
function SetClientValues() {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
}
</script>
At server side button event you can get Label values that sets at client side.
protected void btn_Click(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
It will print Yes Name is Fine
This should do it:
<script type="text/javascript">
$(window).load(function () {
if($('#<%= Txt1.ClientID %>').val() != "Ronaldo"){
var myNewName = "Ronaldo";
$('#<%= Txt1.ClientID %>').val(myNewName);
$('#<%= Label1.ClientID %>').text(myNewName);
$('#<%= Btn1.ClientID %>').click();
}
});
</script>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="Txt1" runat="server" style="display:none"></asp:Label>
<asp:Button ID="Btn1" runat="server" style="display:none" Click="Btn1_Click"></asp:Label>
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Label1.Text=Txt1.Text;
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
protected void Btn1_Click(object sender, EventArgs e)
{ }
Hope it helps :)
I want to start a process when i clicked the start button on webpage (asp.net site) now i want to set the label text to process started. and i want to set the label text to "Process Completed " when the process is ended. how to do this in asp.net and C#.
Thanks in advance.
You might want to consider using ASP.NET SignalR. Here's a summary of what it does:
ASP.NET SignalR is a new library for ASP.NET developers that makes it
incredibly simple to add real-time web functionality to your
applications. What is "real-time web" functionality? It's the
ability to have your server-side code push content to the connected
clients as it happens, in real-time.
The following is an example of simple web page with a button which starts Notepad.exe. Once the process is started, a label on the page shows process started. When the process exits (Notepad is closed), the label's updates to process exited.
So, first create an ASP.NET empty web application project (let's name it MyWebApplication) and get the Microsoft ASP.NET SignalR NuGet package. Add a web form to the project and name it Test. Add the following code to the Test.aspx file:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="MyWebApplication.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"
type="text/javascript"></script>
<script src="Scripts/jquery.signalR-1.0.1.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$('#label').text(message);
};
// Start the connection
$.connection.hub.start();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button runat="server" Text="Start Notepad.exe"
ID="button" OnClick="button_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="button" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<span id="label"></span>
</div>
</form>
</body>
</html>
Add a new class file to your project and name it Chat. In Chat.cs you will have:
using Microsoft.AspNet.SignalR;
namespace MyWebApplication
{
public class Chat : Hub
{
public void Send(string message)
{
//Call the addMessage method on all clients
var c = GlobalHost.ConnectionManager.GetHubContext("Chat");
c.Clients.All.addMessage(message);
}
}
}
Add the following to the Test.aspx.cs file:
using System;
using System.Diagnostics;
using Microsoft.AspNet.SignalR;
namespace MyWebApplication
{
public partial class Test : System.Web.UI.Page
{
Chat chat = new Chat();
protected void Page_Load(object sender, EventArgs e)
{
}
void MyProcess_Exited(object sender, EventArgs e)
{
chat.Send("process exited");
}
protected void button_Click(object sender, EventArgs e)
{
Process MyProcess = new Process();
MyProcess.StartInfo = new ProcessStartInfo("notepad.exe");
MyProcess.EnableRaisingEvents = true;
MyProcess.Exited += MyProcess_Exited;
MyProcess.Start();
chat.Send("process started");
}
}
}
Add the Global.asax file:
using System;
using System.Web.Routing;
namespace MyWebApplication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
}
}
Some things I haven't covered:
The label is updated on all connections.
I'm not verifying if the process is already running or not (but that shouldn't be very difficult to check).
Use javascript to do the callback. And on each stage; Initiated, Completed or Error; update a label in your html. This should be fairly simple if you look for some samples with jQuery AJAX.
jQuery AJAX POST example
If you dont want to use javascript...what you can do is to change label text first when button click event is fired.
lblLabel.text="process started"
and last line in button_click event should be like:
lblLable.text="process completed";
Add this in CodeBehind:
ScriptManager.RegisterStartupScript(this, GetType(), "Records Inserted Successfuly", "Showalert();", true);
JAVASCRIPT add this in source code (aspx):
function Showalert() {
alert('Records inserted Successfully!');
}
And do add using System.Web.UI;
OR
You can simply add a Label to the webform like this in aspx..
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
and in codebehind aspx.cs add ..
Labelname.Text = "whatever msg you wanna display."
Based on my current understandings, when you have an UpdatePanel control there is no full postback. Therefore if I dynamically add custom user controls and they have UpdatePanels that are updated in their code they shouldnt disappear from the page they are loaded into, right? Apparently not. I made a simple project to test and still my dynamic controls disappear when clicked even though they should not be triggering a full postback. I have an aspx page that loads the controls:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TESTmultipleScriptManagerControls.aspx.cs" Inherits="myPlayground.TESTmultipleScriptManagerControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e)
{
TESTcontrol1 temp = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
PlaceHolder1.Controls.Add(temp);
TESTcontrol1 temp2 = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
PlaceHolder1.Controls.Add(temp2);
}
And a simple user control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TESTcontrol1.ascx.cs" Inherits="myPlayground.TESTcontrol1" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}
Any ideas on why the controls are disappearing even though there shouldnt be a postback triggering?
OK based on my current understandings, when you have an UpdatePanel
control there is no full postback.
Postbacks triggered from UpdatePanels always execute the full page life-cycle. All the events are triggered normally. It makes no difference whether you use an UpdatePanel or not. Every time you add a control programmatically you need to re-add it on every postback.
Read this post, it may help you understand a bit better what's going on here.
By End of the Page Life Cycle all the controls generated at Runtime/Compile Time will be Disposed.
Below are the Page Events. Please set the BreakPoint on each Event and you can figure out that on each Asynchronous/Synchronous Request, all these Page Events are being executed.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
}
You need to populate them on each Page Init event in order to persist Viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next Postback. My suggestion is to keep a list of what you have added dynamically, and store it in a session variable