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

Auto Refresh data using AJAX in ASP.NET

change text size: A A A

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)

  • phafpt.2006 4/14/2009 6:23:51 PM by:  phafpt.2006
    Thank you very much ! But it still 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
  • Ramtamil 5/12/2009 10:29:24 PM by:  Ramtamil
    well,its not refresh.
  • Nitesh 6/1/2009 11:38:01 PM by:  Nitesh
    Update web.config file as After that 'sys' not defined error goes off..
  • 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.
  • 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 '

Post a comment

Name:
 *  

Email: (your email is kept secure)
 *  

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

  • 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.
  • 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.
  • 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 about bugs and/or errors & give suggestions to improve this ListBox control.
  • 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.
  • 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 various browsers and it works fine.
  • 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 there is question arise what is PagedDataSource. PagedDataSource is a class which encapsulates
  • 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.
  • So this is my solution. If you have some other ideas about this functionality, please share them with me.
  • I have tried my best to make this code error free. Suggestions and criticism for further improvements of this code are most welcome.
  • So this is the approach that I've adopted to solve the Hover Delay problem. Although originally I developed Hover Delay to deactivate the click event for 1 second on a GridView row, later I also used Hover Delay to deactivate Drag n Drop of GridView rows. Kindly let me know if any one has some other better or different solution.
  • In this article I will explain how to import data from EXCEL to SQL in ASP.NET . In many situations we have data in the form of excel sheet but we have the requirement to have that data in SQL SERVER DB. I have explained importing data both from Excel 97-2003 as well as Excel 2007 format.
  • The source code shows how to use Regular Expressions in C#. The code Functions written for Validation Alphabet, AlphaNumeric, Integer, Postive Integer, Floating point numbers. You just cut copy these functions and use in any program.
  • That’s all about this technique. Just download the sample application and happy CSS! I have tested this application on various browsers and it worked fine.
  • This script is cross-browser compatible and fast as it iterates elements of a specific tag inside a target element [GridView] rather than iterating in a whole form. It searches the elements of a specific type in a particular column of the target element [GridView].
  • This Article is used to insert a numeric value on the sever control(text box) This is a java script code for the the client side validation. On Page Load Event You can change the events in txtNoOfQuestion.Attributes.Add("onkeypress", "return numericOnly(this);"); like onfocus events like other according to needs.
  • I have toggled visibility of all TD elements of a GridView column in order to create an illusion of smooth dynamic effect with the help of setTimeout method through recursion. Different browsers have different effects of Expanding / Collapsing GridView Columns. In Internet Explorer 7/8, Safari, Google Chrome and Opera, it seems that columns are Exp
  • In this article, I've used the setTimeout method in order to achieve a smooth expand/collapse functionality.
  • Introduction I am going to present here a functionality that selects / deselects all checkboxes of a particular column inside a GridView control, provided the header checkbox of that column is checked or unchecked using JavaScript. This functionality also has a feature that when all checkboxes of a particular column inside the GridView are check
  • This article describes how to toggle the states of all CheckBoxes inside a particular DataGridView column.
  • This article describes how to apply client-side mouse over & mouse out effects on the GridView’s rows.