There are 102 guests and 0 members online. Who is online?

home  »  articles  »  Auto Refresh data using AJAX in ASP.NET

Auto Refresh data using AJAX in ASP.NET

change text size: A A A

Published on 3/25/2009 by Vijjendra

Download Source Code

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>

 My design page as:

 

 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();

       }


Hope it will help to you..........

tags: Asp.net, Session, C#, AutoRefresh, Ajax, JQuery
To rate this article please register or login

Author

Vijjendra Vijjendra (Member since:11/29/2008)
This is Vijendra Singh Shakya.

Comments (6)

  • Test 7/12/2009 4:02:00 AM 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 '
  • Vijjendra 6/3/2009 12:11:15 PM 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.
  • Nitesh 6/1/2009 11:38:01 PM by:  Nitesh
    Update web.config file as After that 'sys' not defined error goes off..
  • Ramtamil 5/12/2009 10:29:24 PM by:  Ramtamil
    well,its not refresh.
  • phafpt.2006 4/14/2009 11:17:50 PM by:  phafpt.2006
    What is error ? Line:0 Char:1 Error: 'Sys' is undefined Code : 0
  • phafpt.2006 4/14/2009 6:23:51 PM by:  phafpt.2006
    Thank you very much ! But it still not refresh.

Post a comment

Name:
* 
Email:
* 
Website:
  example: "http://codeasp.net/"

Comment  (No HTML)   

Type the characters:
 *
 

Related articles

Join CodeAsp.Net for FREE Today!

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:

Latest Articles RSS Feed

Latest articles

.
  • how to change Visual Studio's default browser
    12/15/2009
    how to change Visual Studio's default browser
  • Access dropdownlist inside the gridview
    12/10/2009
    Hi Friends, Many times we came cross the requirement when we need to access the dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.
  • JavaScript Expandable / Collapsible Panel Control
    12/4/2009
    This is the approach that I have adopted to develop Expandable / Collapsible Panel Control through JavaScript. Please report bugs, errors and suggestions to improve this control.
  • JavaScript ListBox Control
    12/3/2009
    This is my approach to develop custom JavaScript ListBox control. Although it is only a subset of existing HTML ListBox element, it is more user friendly than the existing one. It can be further customized for different requirements. Please let me know ab...
  • Multiple File Upload User Control
    11/27/2009
    I have tried my best to make this user control code error free. I will most welcome suggestions for further improvement in this user control. I have tested this user control on various browsers and it works fine.
  • Slide-Show User Control
    11/26/2009
    So, this is my approach to implement an ASP.NET slide show using the DataList. I have tried my best to keep it bug free. I will most welcome suggestions and criticism for further improvements of this user control. I have tested this user control on variou...
  • Implement: Paging in Repeater or Datalist Control
    11/18/2009
    As we all knows that Repeater and DataList does not have auto paging support technique like Gridview or Datagrid, but we can achieve this through PagedDataSource. By using PagedDataSource we can implement paging in Repeater or DataList. Now in our mind t...
  • JavaScript Context Menu Control
    11/6/2009
    So this is my approach. I was working for a long time to create C# like event handlers for JavaScript classes and finally, I’ve done it. Please let me know of any bugs and suggestions to improve this context menu control.
  • Maintaining States of Selected CheckBoxes in Different Pages inside the GridView
    11/4/2009
    So this is my solution. If you have some other ideas about this functionality, please share them with me.
  • GridView Rows Navigation Using Arrow (Up/Down) Keys
    11/4/2009
    I have tried my best to make this code error free. Suggestions and criticism for further improvements of this code are most welcome.

More Articles