ASP.NET Timer Event

喜欢而已 提交于 2020-01-02 10:33:45

问题


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


回答1:


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;

    }



回答2:


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?



来源:https://stackoverflow.com/questions/2800426/asp-net-timer-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!