Auto Refresh update panel on page using AJAX - c#
In this article, I am going to explain how we can auto refresh data on an ASP.NET page after a certain interval using AJAX UpdatePanel and other controls (assuming you have some basic knowledge of AJAX Toolkit). I am using some Ajax controls and using SQL server database and Data Grid control. Lets for the time, use database north wind and set our interval time for refreshing data as 30 seconds.
Code behind page :
public void BindData()
{
con = new SqlConnection("Initial Catalog=Northwind; Data Source=localhost; Uid=sa; pwd=;");
cmd.CommandText = "select * from Employees ";
cmd.Connection = con;
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
cmd.ExecuteNonQuery();
GridData.DataSource = ds;
GridData.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
}
Here is HTML desing code:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="30000">
</asp:Timer>
</div>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Grid not refreshed yet."></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label>
<br /><br />
<asp:DataGrid ID=GridData runat="server" Width="100%" GridLines="Both" HeaderStyle-BackColor="#999999" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="EmployeeID" HeaderText="Employee ID"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ContentTemplate>
</asp:UpdatePanel>
</form>
I hope you liked this article..if yes please drop some comments

Code behind page :
public void BindData()
{
con = new SqlConnection("Initial Catalog=Northwind; Data Source=localhost; Uid=sa; pwd=;");
cmd.CommandText = "select * from Employees ";
cmd.Connection = con;
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
cmd.ExecuteNonQuery();
GridData.DataSource = ds;
GridData.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
}
Here is HTML desing code:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="30000">
</asp:Timer>
</div>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Grid not refreshed yet."></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label>
<br /><br />
<asp:DataGrid ID=GridData runat="server" Width="100%" GridLines="Both" HeaderStyle-BackColor="#999999" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="EmployeeID" HeaderText="Employee ID"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ContentTemplate>
</asp:UpdatePanel>
</form>
I hope you liked this article..if yes please drop some comments
Thank's - it's very good sample. :-)
ReplyDeleteGreat work and nice article. Keep it up
ReplyDeleteThis is great stuff fro beginners ..like me... thanks alot..
ReplyDeletesuper cooooooooolllll it helped me a lot for my project
ReplyDeletehi sanjeev,
ReplyDeletevery well written. very nice and precise article..exactly to the point....thanx for the help
thats exactly i want
ReplyDeletethanx buddy
Great Article.
ReplyDeleteHave you seen client side memory leak problems with code before? I have a gridview and a dropdownlist inside the update panel and my timer refreshes every 30 seconds. If I watch the Memory Usage in the task manager, it goes up by about 10K with each ajax refresh. If you have any advice on what would be causing this it would be greatly appreciated.
I'm using ASP.Net 2.0.
Thanks,
Greg
Thanks nice, the asynchpostback is just what I was looking for.
ReplyDeleteTa,
Dale from London.
Great work and nice article.
ReplyDeleteRajiv Sharma
iam@rajivsharma.com
Hi, I've also doing some stuff like this. But I'm facing a problem on the ajax refresh. My gridview contains a lot of data. So, I used paging. Everything is OK but when I navigate to next page, I need to wait until the timer ticked, then it just navigate to next page of the gridview. Any idea?
ReplyDeleteFine , and i have one doubt, how to refresh a label , which displays current time, and i want to refresh it every second with out refresh a page
ReplyDeleteGreat Work.... Really nice one
ReplyDeleteI have read the article, and it sounds just like what I need. However, having implemented it with some slight modifications (a DataView being filled from an Dataset), my text field updates, but the dataset doesn't.
ReplyDeleteAny ideas anyone?
Good One
ReplyDeletethank u , that is very good
ReplyDeletethank u , that is very good
ReplyDeletelove ya!
ReplyDeleteDear that whoooooooole code can be written like this HTML tag.......
ReplyDeletemeta http-equiv="Refresh" content="300"
Cheers....
Saurabh
Hey Saurabh,
ReplyDeleteYou have a long way to go.. you in still live in the 90s...
Mads
Great, thanks a lot for this informative example on AJAX refresh. Works like a charm!
ReplyDeletenice work dude... thats xacly wat i wanted!!!
ReplyDeleteMy Gridview gets data from an ObjectDataSource and doesnt gets refreshed! Whyyyyy? :(
ReplyDeletethx, really a time saver!
ReplyDeleteGreat stuff!
ReplyDeletevery nice give me your ph number
ReplyDeletedoes this retain viewstate info?
ReplyDeletedoes the meta tag method?
Great solution!, but I have one question. What about server performance with every timer tick?.
ReplyDeleteI need implement something like this in an application that will have 400 or 500 concurrent users. How this approach might affect my server throughput
Excellent..
ReplyDelete"Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
ReplyDeleteXML error message: The element type "meta" must be terminated by the matching end-tag ""
this message show when i put ur code.why?
and how and wher exactly iput it.
Thanks for the example!
ReplyDeleteProvided me what I needed, and got me on my way. My thanks.
ReplyDeletesimple and clear. very helpful.
ReplyDeleteGreat one dude.
ReplyDeletethanks for very good and clear code.
ReplyDeleteNice Work.
ReplyDeleteThanks for the clear and good Example.
You Saved My time Lot.
Thanks alot...
ReplyDeleteUr Article is nice. Gud Job Buddy.
ReplyDeleteGreat work, thanks alot
ReplyDeleteGood Article. Thanks.
ReplyDeleteu explained it so simply...
ReplyDeletethanks a lot..
really a nice and decent work
ReplyDeleteExcellent job
ReplyDeleteIt works great........
nice yar..........
ReplyDeletei like it very much....
Thanks! help me a lot!
ReplyDeleteThank you in advance!
ReplyDeletehi,
ReplyDeletei want only gridview refresh 30sec
bt ur programmer refresh the page which create problem for me
example:if u put a textbox & write some text u cant.
contact me:sameer31.das@gmail.com
Perfect, I had started to look into writing this myself in javascript but found your post after a few problems. This solution worked perfectly and was very easy to implement. Exactly what I wanted the end result to be. Thanks!
ReplyDeletegreat article!!!!
ReplyDeletebut because of page refresh, when you scroll down the page it scrolls up. any solution to that?
please contact me on patilmilindv@gmail.com
Thanks ..
ReplyDeleteGood article ..
Thanks
ReplyDeletethnx
ReplyDeleteExcelent Thanks...
ReplyDeleteThanks,a good example.
ReplyDelete