asp.net in Timer no refresh timer implementation method

  • 2021-01-19 22:04:24
  • OfStack

This paper describes the implementation of Timer non-refresh timer in asp.net as an example. Timer control to achieve no refresh, use ajax technology, here use VS2008 comes with ajax technology.

First add 1 ScriptManager control, and then add 1 UpdatePanel to store the contents of Timer control, you can achieve no refresh. Here are the details:

1. Front desk code is as follows:


<form id="form1" runat="server">
  <asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="60000" ontick="Timer1_Tick">
        </asp:Timer>
      </ContentTemplate>
      </asp:UpdatePanel>
</form>

Remember that ScriptManager 1 has to be there < form > Tag, can be placed anywhere. After adding the UpdatePanel control, it is necessary to use its 1 very important property ContentTemplate, otherwise it is impossible to achieve no refresh effect. Here we set the 6 second timer to trigger the event once.

2. The background code is as follows:


protected void Page_Load(object sender, EventArgs e)
{}
protected void Timer1_Tick(object sender, EventArgs e)
{
// Here you can do whatever you want, such as query the database periodically 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Hello ' );", true);
}

I hope the examples described in this paper are helpful to asp.net program design.


Related articles: