ARTICLE

Auto Refresh Data on Page Using AJAX

Posted by Raj Kumar Articles | AJAX in C# September 29, 2007
In this article, I explain how to auto-refresh data on an ASP.NET page after a certain interval using AJAX UpdatePanel and other controls.
Reader Level:
 

Hi this is my first article on C# corner. I have some good topics in my mind, stay tuned. 

In this article, I explain how to auto-refresh data on an ASP.NET page after a certain interval using AJAX UpdatePanel and other controls.  I am using some AJAX controls and using a SQL Server database and a Data Grid control. The Database name is Northwind. In this application my interval time for refresh data is 30 seconds. You can change your time by the times interval property.

Here is a snapshot:

refreshgrid1.JPG

This code is for binding data:

public void BindData()

    {

       

        con = new SqlConnection("Initial Catalog=Northwind; Data Source=localhost; Uid=sa; pwd=;");

        cmd.CommandText = "select * from Employees ";

        cmd.Connection = con;

        con.Open();

        da = new SqlDataAdapter(cmd);

        da.Fill(ds);

        cmd.ExecuteNonQuery();

        GridData.DataSource = ds;

        GridData.DataBind();

    }

You can check your current time on page load. Write this code:

MyLabel.Text = System.DateTime.Now.ToString();
BindData();

And the grid refresh time is:

protected void Timer1_Tick(object sender, EventArgs e)

    {

Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();

    }

Here is the HTML design code:

<form id="form1" runat="server">

     <asp:Label ID="Label2" runat="server" Text="This is Time, When The Full Page Load :" Font-Bold="true"></asp:Label>&nbsp;

     <asp:Label ID="MyLabel" runat="server"></asp:Label><br /><br />       

  

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <div>

            <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="30000">

            </asp:Timer>

        </div>

        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

            <Triggers>

                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

            </Triggers>

            <ContentTemplate>

                <asp:Label ID="Label3" runat="server" Text="This is The Time when Only Data Grid will Referesh :" Font-Bold="true"></asp:Label>&nbsp;

                <asp:Label ID="Label1" runat="server" Text="Grid not refreshed yet."></asp:Label><br />

                <asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label>&nbsp;

                <br /><br />

                <asp:DataGrid ID=GridData runat="server" Width="100%" GridLines="Both" HeaderStyle-BackColor="#999999" AutoGenerateColumns="false">

                    <Columns>

                        <asp:BoundColumn DataField="EmployeeID" HeaderText="Employee ID"></asp:BoundColumn>

                        <asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>

                         <asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>

                        <asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>

                    </Columns>

                </asp:DataGrid>

                   

            </ContentTemplate>

        </asp:UpdatePanel>     

     

    </form> 

I hope you will like this article. If yes then drop me a line or write a comment below in the comments section.

Login to add your contents and source code to this article
post comment
     

Check how many script manager have you added on page? or if you are using master page then there should be only one script manager.

Posted by Raj Kumar Apr 07, 2013

why error, Only one instance of a ScriptManager can be added to the page. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Only one instance of a ScriptManager can be added to the page.Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [InvalidOperationException: Only one instance of a ScriptManager can be added to the page.]System.Web.UI.ScriptManager.OnInit(EventArgs e) +660584System.Web.UI.Control.InitRecursive(Control namingContainer) +143System.Web.UI.Control.InitRecursive(Control namingContainer) +391System.Web.UI.Control.AddedControl(Control control, Int32 index) +271BI.LBUS.WebParts.StatusLaporan.AntrianValidasi.AntrianValidasi.CreateChildControls() +155System.Web.UI.Control.EnsureChildControls() +146System.Web.UI.Control.PreRenderRecursiveInternal() +61System.Web.UI.Control.PreRenderRecursiveInternal() +224System.Web.UI.Control.PreRenderRecursiveInternal() +224System.Web.UI.Control.PreRenderRecursiveInternal() +224System.Web.UI.Control.PreRenderRecursiveInternal() +224System.Web.UI.Control.PreRenderRecursiveInternal() +224System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

Posted by firman Apr 06, 2013

Gr8 job! Thanx for sharing with us.

Posted by Navin Pandit Nov 15, 2012

thanks

Posted by sole May 07, 2012

I got problems with the download could anybody help me?

Posted by roman Jan 20, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.