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();
}
问题在于正在显示的网格视图中的数据...因为当调用第 1 行之后的 get_datasource 时,更新的数据会显示在网格视图中,因为它是回发事件,但是当计时器事件处理程序正在调用时timer_tick 事件调用 get_datasource 函数,但之后更新的数据在网格视图中不可见。由于timer_tick不是回发事件,所以它没有得到更新
您实现的服务器端计时器将无法实现您想要实现的目标。
如果您将计时器和 gridview 都包装在 updatepanel 中,则计时器将在每次触发事件时触发回发,并且您能够更新数据。
这是一篇很棒的博客文章,可以帮助您继续前进: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>
服务器端代码:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)