Loading ...

Auto Refresh data using AJAX in ASP.NET

Who is online?  0 guests and 0 members
home  »  articles  »  Auto Refresh data using AJAX in ASP.NET

Auto Refresh data using AJAX in ASP.NET

(22414)
0
/5
Avg: 0/5: (0 votes)
Published: 3/25/2009 by  Vijendra Shakya

Download Sample Project

In 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]

)

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>

                        <tr border="1px">

                        <td><%#Eval("UserName") %></td>

                        <td><%#Eval("EmailAddress")%></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>

</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.

 

Comments (12)

phafpt.2006
Phoi Pha Pham said:
Thank you very much ! But it still not refresh.
4/14/2009
 · 
 
by
phafpt.2006
Phoi Pha Pham said:
What is error ? Line:0 Char:1 Error: 'Sys' is undefined Code : 0
4/14/2009
 · 
 
by
Ramtamil
Ram Tamil said:
well,its not refresh.
5/12/2009
 · 
 
by
Nitesh
Update web.config file as After that 'sys' not defined error goes off..
6/1/2009
 · 
 
by
Vijjendra
Hi, It refresh the repeater data after every 15 second. It refresh the page via ajax, I think that's why you can't find this. To test this delete the record from DB,then see the the results.
6/3/2009
 · 
 
by
Test
This is good solution, but im getting some error from the page like. Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near '
7/12/2009
 · 
 
by
sonu
sonu said:
i need my repeater control to show just three items on each page out of 15 items... is it possible????
2/3/2010
 · 
 
by
Sheila
Sheila said:
Can you include screen shots of the Design page?
3/2/2010
 · 
 
by
karthik
karthik said:

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.

6/11/2010
 · 
 
by
Elie123
Elie Elie said:

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,

10/12/2010
 · 
 
by
elnass
elnass s said:

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?

10/15/2010
 · 
 
by
Vijjendra

Rahul thanks for comment.

Please download the sample project and see how that works.

1/8/2011
 · 
 
by
  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Confirm

Product Spotlight

ASP.NET Hosting Spotlight

Most Recent Articles

 

Product Spotlight

ASP.NET Hosting Spotlight

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?