Automatic Refresh data on page using AJAX UpdatePanel
By fatma78
I am explaining how we can automatic refresh data on an ASP.NET page after a certain interval using AJAX UpdatePanel and other controls. I am using some Ajax controls and using SQL server database and Data Grid control. Database name is north wind. In this application my interval time for refresh data is 60 second. We can change your time by times interval property.
//This code for display data in the datagrid:
public void DisplayData()
{
System.Data.SqlClient.SqlConnection = new System.Data.SqlClient.SqlConnection("Initial Catalog=Northwind; Data Source=localhost; Uid=sa; pwd=;");
System.Data.SqlClient.SqlDataAdapter ds = new System.Data.SqlClient.SqlDataAdapter("select * from Employees", cn);
DataSet ds = new DataSet();
da.Fill(ds);
myGrid.DataSource = ds;
myGrid.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
//You can check your current time on page load. Write this code:
lblTime.Text = System.DateTime.Now.ToString();
DisplayData();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//Datagrid refresh time
lbldgTime.Text = "Refresh at:" + System.DateTime.Now.ToString();
DisplayData();
}
//This is HTML Code:
<form id="form1" runat="server">
<asp:Label ID="Label2" runat="server" Text="This is Time, When The Full Page Load :" Font-Bold="true"></asp:Label>
<asp:Label ID="lblTime" runat="server"></asp:Label><br /><br />
<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="Label3" runat="server" Text="This is The Time when Only Data Grid will Referesh :" Font-Bold="true"></asp:Label>
<asp:Label ID="lbldgTime" runat="server" Text="Grid not refreshed yet."></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every 60 Sec)" Font-Bold="True"></asp:Label>
<br /><br />
<asp:DataGrid ID=myGrid 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>
<HeaderStyle BackColor="#999999" />
</asp:DataGrid>
</ContentTemplate>
</asp:UpdatePanel>
</form>
I hope this code example will be useful
Comments
Thank you
the dataset and the data adapter both have the name "ds" at declaration - data adapter after dat is refered as "da"
test
test
54354354353
csacsac
www.dotnettechy.com
I found good website for Microsoft.Net professional www.dotnettechy.com here we search articles, Problem and solutions and interview questions and answer.
We can also bookmark for further reference.
Also we can submit and manage .net articles URLs, problems, and interview questions.
1test
This is best one article so far I have read online. I would like to appreciate you for making it very simple and easy. I have found another nice post related to this post over the internet which also explained very well. For more details you may check it by visiting this url........
http://mindstick.com/Articles/385ebacd-89e2-40d3-8
Thanks
Super!!!!
Excellent!
wow....thanks...a.....lot
Really it is good and working great yar.
Thanx for posting such good and usefull one.....
alagar samy 2 years ago
The Iformation what u gave is good.