Who is online? 123 guests and 0 members
Member login | Become a member
home » articles » Auto Refresh data using AJAX in ASP.NET
3/25/2009 by Vijjendra
In this article, I am discussing how we can refresh data on an ASP.NET data representation control. i.e data representation cotrol automatically refreshes after a certain interval using AJAX UpdatePanel and other controls. I am using Ajax controls and using SQL server 2005 database and repeater control. My Database and table name is User. In this application, interval time for refreshing data is 15 seconds.To refresh the data automatically we need a timer control. Below is the structure of table:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [UserName] [varchar](50) NULL, [EmailAddress] [varchar](50) NULL, [FirstName] [varchar](50) NULL, [LastName] [varchar](50) NULL, [City] [varchar](50) NULL, [DateJoined] [datetime] NULL, CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ( [UserId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF
In this table we can insert the values as follows:
insert into [user] values('vijendra','vijendra@gmail.com','vijendra','singh','Noida',GetDate())
<form id="form1" runat="server"> Time When Full Page Load: <asp:Literal ID="litFullLoad" runat="server"></asp:Literal><br /> <br /> <asp:ScriptManager ID="scManager" runat="server" /> <div> <asp:Timer ID="IntervalTimer" OnTick="IntervalTimer_Tick" runat="server" Interval="15000"> </asp:Timer> </div> <asp:UpdatePanel ID="upPanel" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="IntervalTimer" EventName="Tick" /> </Triggers> <ContentTemplate> Time when Only Repeater data will Referesh : <asp:Literal ID="litrptRefresh" runat="server" Text="<b>repeater not refreshed yet.</b>" ></asp:Literal><br />(Repeater Will Referesh after Every 15 Second) <br /> <table border="1px"> <tbody> <asp:Repeater ID="rptUser" runat="server"> <HeaderTemplate> <tr border="1px"> <th> User Name </th> <th> User Email </th> <th> First Name </th> <th> Last Name </th> <th> Location </th> <th> Date of Joining </th> </tr> </HeaderTemplate> <ItemTemplate> <tr border="1px"> <td> <%#Eval("UserName") %> </td> <td> <%#Eval("Email") %> </td> <td> <%#Eval("FirstName") %> </td> <td> <%#Eval("LastName") %> </td> <td> <%#Eval("city") %> </td> <td> <%#Eval("DateJoined") %> </td> </tr> </ItemTemplate> </asp:Repeater> </tbody> </table> </ContentTemplate> </asp:UpdatePanel> </form>
Below is the code for bind the data in repeater:
private void PopulateUser() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conectionStr"].ConnectionString); string conText = "Select * from user"; SqlCommand com = new SqlCommand(conText, con); com.CommandType = CommandType.Text; con.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(com); da.Fill(ds); rptUser.DataSource = ds; rptUser.DataBind(); }
use this function on the page load as:
protected void Page_Load(object sender, EventArgs e) { litFullLoad.Text = System.DateTime.Now.ToString(); PopulateUser(); }
repeater data refresh time interval is:
protected void IntervalTimer_Tick(object sender, EventArgs e) { litrptRefresh.Text = "Repeater data will refreshed at: " + DateTime.Now.ToLongTimeString(); }
Name: *
Email: (your email is kept secure) *
Website:
Comment (No HTML)
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: