protected void SubmitButtonClicked(object sender, EventArgs e)
{
System.Timers.Timer timer = new System.Timers.Timer();
---
---
//line 1
get_datasource();
String message = "submitted.";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "popupAlert", "popupAlert(' " + message + " ');", true);
timer.Interval = 30000;
timer.Elapsed += new ElapsedEventHandler(timer_tick);
// Only raise the event the first time Interval elapses.
timer.AutoReset = false;
timer.Enabled = true;
}
protected void timer_tick(object sender, EventArgs e)
{
//line 2
get_datasource();
GridView2.DataBind();
}
The problem is with the data in the grid view that is being displayed... since when get_datasource which is after line 1 is called the updated data is displayed in the grid view since it is a postback event but when the timer event handler is calling the timer_tick event the get_datasource function is called but after that the updated data is not visible in the grid view. It is nnot getting updated since the timer_tick is not a post back event
The server-side timer as you have implemented it, will not work for what you are trying to achieve.
If you wrap both the timer and gridview in a updatepanel, the timer will trigger a postback everytime the tick event fires and you be able to update the data.
Heres a great blog post to get you going: http://mattberseth.com/blog/2007/08/using_the_ajax_timer_control_a.html
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:Timer id="Timer1" runat="server" Interval="30000" OnTick="Timer_Tick" Enabled="false" />
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="SubmitButtonClicked" />
</ContentTemplate>
</asp:UpdatePanel>
Server-side code:
private void Timer_Tick(object sender, EventArgs args)
{
get_datasource();
GridView2.DataBind();
Timer1.Enabled = false;
}
protected void SubmitButtonClicked(object sender, EventArgs e)
{
String message = "submitted.";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "popupAlert", "popupAlert(' " + message + " ');", true);
get_datasource();
GridView2.DataBind();
Timer1.Enabled = true;
}
You cannot use a timer like that. While ASP.NET tries to hide the request/response cycle of HTTP, the cycle is still there so you cannot just do whatever you like in your postback: you still need to understand that a HTML response is being sent back in response to a HTTP request.
Is there any particular reason why you're trying to use a timer like this? It doesn't seem to make sense to me. What is it that you're trying to achieve?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With