Download Sample ProjectIn this article, I am discussing how we can refresh data after a certain interval ASP.NET by using data representation control. i.e data representation cotrol automatically refreshes after a certain interval using AJAX UpdatePanel and Timer controls. I am using Ajax’s controls , database SQL server 2005 and repeater control. My Database name is Test and table name is User. For refresh the data automatically for that purpose I am using ASP.NET’s Timer control. Below is the scema of table which I am using in the example: 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]
Insert some data in the [user] table as follows:
INSERT INTO [user] VALUES('Vijendra','vijendra@gmail.com','Vijendra','Shakya','Noida',GetDate())
INSERT INTO [user] VALUES('Rajesh','raj@gmail.com','Rajesh','Kumar','Delhi',GetDate())
INSERT INTO [user] VALUES('Asif','asif@gmail.com','Asif','Quadri','Noida',GetDate())
INSERT INTO [user] VALUES('Neeraj','neeraj@yahoo.co.in','Neeraj','Shakya','Noida',GetDate())
INSERT INTO [user] VALUES('Umesh','umesh@gmail.com','Umesh','Chandra','Gr.Noida',GetDate())
INSERT INTO [user] VALUES('Vinay','vanay@gmail.com','Vinay','Gupta','Bombay',GetDate()) My ASPX page looks like as follows: <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Auto Refresh</title>
</head>
<body>
<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 /><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>
<td><%#Eval("UserName") %></td>
<td><%#Eval("EmailAddress")%></td>
<td><%#Eval("FirstName") %></td>
<td><%#Eval("LastName") %></td>
<td><%#Eval("city") %></td>
<td><%#Eval("DateJoined") %></td>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html> Below is the code behind file has following code: private void PopulateUser()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].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();
}
protected void IntervalTimer_Tick(object sender, EventArgs e)
litrptRefresh.Text = "Repeater data will refreshed at: " + DateTime.Now.ToLongTimeString();
} use above PopulateUser() method on the page load: protected void Page_Load(object sender, EventArgs e)
litFullLoad.Text = DateTime.Now.ToString();
PopulateUser();
} now this page automatically refresh after every 15 second. suppose we have add new two more rows in the database the inserted record are:
INSERT INTO [user] VALUES('Ajit','ajit@hotmail.com','Ajit','Gupta','Delhi',GetDate())
INSERT INTO [user] VALUES('Sumit','sumit@yahoo.com','Sumit','Arora','New Delhi',GetDate())Now result looks like as follows: Hope so it will help. Waiting for your valuable comments.
Exellent coding ...Its working fine ....I've a designed a grid view with DDL, i'm having 15,000 data's in that DDL ..so its taking time for loading the page even if I use AJAX for the whole gridview..can u've any other idea to slove this problem.
hi
I'm new to ajax .
I'm working on visual studio 2005. I install the ajax Min4setup and I add the control toolkit to the controls toolbar .
I write this code above in the page.aspx and the other code in page.aspx.vb but nothing happens, the timer event is not firing.
Any idea or suggestion will be highly appreciated
Best Regards,
hi,
I have a repeater inside an updatepanel called updpanel1 just like u, but in the repeater, i have a button and i need that button refresh another updatepanel. The problem here is that the updpanel2 has another button and when run the code and click at the button, i get this error:
"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."
Can anyone help me?
Rahul thanks for comment.
Please download the sample project and see how that works.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18