Add the GridView and the Timer control inside an ASP.NET AJAX UpdatePanel as shown below :
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="3600" ontick="Timer1_Tick"></asp:Timer>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Then in the code behind, add the following code which refreshes the GridView after every minute
C#
protected void Timer1_Tick(object sender, EventArgs e)
{
GridView2.DataBind();
}
VB.NET
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
GridView2.DataBind()
End Sub
Giving me +1 tells me you liked this article! Thanks in advance
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
11 Responses to "How to refresh an ASP.NET GridView automatically at regular intervals"Thank you and best of luck with the book.
sank youu
Gives me an error, Update Panel requires ScriptManager
I added a script Manager tag just above the Update Panel and it worked. It was updating my gridview every x seconds
Hello, im getting a PageRequestManagerParseErrorException, i have the code just like you suggest, what might be going wrong?
Thank you.
For the PageRequestManagerParseErrorException error, try this link
http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx
some of the function within the grid not work??
I am filling data after every 5 min. but by this code first time it take 5 min. to fill grid. how to avoid this?
Hi Rahul.
You just have to fire the Databinder code in PageLoad, like this:
protected void Page_Load(object sender, EventArgs e)
{
GridView2.DataBind();
}
LO MAS GRANDE MUCHAS GRACIAS!!!!!!!!!!!!!!!!!!!!!!!!!
FCP
Simply gr8.........
Post a Comment