3
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
  • Phew, that's a confusing looking message - Mr. Ratnajyothi, could you re-format it so it looks readable? Also, you won't be able to use a Timer in an ASP.NET application to cause a post-back as it only runs on the server. You'll want to use a JavaScript client script instead. Commented May 10, 2010 at 5:08

2 Answers 2

4

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;

    }
Sign up to request clarification or add additional context in comments.

2 Comments

But the event is to be triggered only once after the timer is expired not every time the timer expires..
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)
1

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?

3 Comments

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.
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..
@KRatnajyothi Here is an example of what you are asking for: w3schools.com/js/js_timing.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.