tags:

views:

272

answers:

2
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  A: 

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?

Dean Harding
After button clicked we will send the particular data for cheking its status . then that data will no be present in the grid view after submitting. But if the checking is not done properly again that particular data needs to be displayed after 30 sec so .. again calling the data source after the time elapsed.
K Ratnajyothi
could you please help me in writing a javascript for timer on client side ...so that a particluar event should be called after 15 min the button is clicked..
K Ratnajyothi
+2  A: 

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;

    }
geoff
But the event is to be triggered only once after the timer is expired not every time the timer expires..
K Ratnajyothi
I've updated the answer with a solution to this. Just enable the timer on the first button click and disbale it the first time the timer runs.(note: you'll also want to include the submitbutton in the updatepanel too)
geoff
Updated again to show button inside updatepanel
geoff