By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,384 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to Update Time Displayed Inside GridView Without Full Postback

P: 85
I had a problem recently in which I had to update the time automatically without refreshing the page. I found the most simple solution which is as follows.

Place your grid view inside an update panel like this:
Expand|Select|Wrap|Line Numbers
  1. <asp:UpdatePanel ID="pnltoday" runat="server">
  2.  <ContentTemplate>
  3. <asp:GridView ID="myGrid" runat="server" />
  4. <Columns>
  5. //All your template fields or bound fields go here....
  6. </Columns>
  7. </asp:GridView>
  8.  <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick">
  9.                             </asp:Timer>
  10.  </ContentTemplate>
  11.  
  12. </asp:UpdatePanel>
  13.  
Before closing the </contentTemplate> tag, place an ASP.NET timer control and now in the code behind file, in the Timer1_Tick event call your method to bind the GridView like:
Expand|Select|Wrap|Line Numbers
  1. protected void Timer1_Tick(object sender, EventArgs e)
  2. {
  3.   myGrid.DataSource="Your Function";
  4.   myGrid.DataBind();
  5. }
  6.  
Aug 1 '12 #1
Share this Article
Share on Google+
2 Comments


Frinavale
Expert Mod 5K+
P: 9,731
You have to be really careful when using Timer Controls within UpdatePanels.

If the timer control ticks, while the last process is still taking place, you could create a deadlocked interface and the server will become very busy.

Why?

Because the timer tick will cause the page to post back to the server (partial page postback) and the request will be processed by the server.

If the server takes longer to process the request than the timer tick, then another partial page post back will take place but the webpage wont be updated with the previous response because it's now processing the new request.

Make sure that your timer ticks are spaced out enough to take into consideration the amount of time it takes to process the request (including lag between sending and receiving the request over the internet/network)
-Frinny
Aug 1 '12 #2

P: 85
@Frinavale:
yeah you are right, but that can be handled by increasing the time interval of the timer control i.e. keep it at a value sufficient enough to process any request.
Aug 2 '12 #3