ASP.NET计时器事件 [英] ASP.NET Timer Event

查看:173
本文介绍了ASP.NET计时器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
}

问题是与正在显示在网格视图中的数据...从何时起get_datasource这是1号线后,被称为更新的数据显示在网格视图,因为它是一个回发事件,但是当计时器事件处理程序调用timer_tick事件get_datasource函数被调用,但在此之后,更新的数据是不是在网格视图中可见。据n不是得到更新,因为timer_tick不是回发事件

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.

如果你在一个包裹的UpdatePanel同时向定时器和GridView,计时器会触发回传,每次蜱事件触发,你可以更新数据。

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.

继承人一个伟大的博客文章让你去: http://mattberseth.com/博客/ 2007/08 / using_the_ajax_timer_control_a.html

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>

服务器端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;

    }

这篇关于ASP.NET计时器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆