Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to update GridView after certain interval using UpdatePanel and Timer Controls in ASP.NET

Author: Jeff Nava

In this article, we will see how we can update GridView control after certain interval using AJAX UpdatePanel and Timers Controls in ASP.NET.

 

Timer Control is used for postbacks after specified intervals without need of any action from user. It is often used with AJAX UpdatePanel control to update the content inside UpdatePanel after defined interval. It can also be used to postback entire page after certain interval. It frequently updates entire page or content inside UpdatePanel.  Timer control automatically workds as trigger for UpdatePanel when we use it with UpdatePanel. “Interval” property is used to set the interval in milliseconds.
 
  1. Open Visual Studio 2010
  2. File > New > Website
  3. Visual basic or Visual C# > ASP.NET Empty Web Site
  4. Website > Add New Item > Web Form
  5. Open web form and add a ScriptManager control from “AJAX Extensions” tab.

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
     
    ScriptManager control is necessary when you are using Ajax functionality in your application.
     
  6. Add an UpdatePanel control and a Timer, a Label and a GridView controls inside UpdatePanel control.


    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" Interval="10000" ontick="Timer1_Tick"> </asp:Timer>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
     
    Interval property is set to 10000 miliseconds (10 seconds).  
     
  7. Write a function in code behind file to get data from database and bind this data in PageLoad method for first time binding.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = ProductsData();
            GridView1.DataBind();
        }
    }
    private DataTable ProductsData()
    {
        string MyConstring = "Data Source=Source;Initial Catalog=NORTHWND;Integrated Security=True";
        SqlConnection sqlConn = new SqlConnection(MyConstring);
        SqlCommand cmd = new SqlCommand("SELECT * FROM Products", sqlConn);
        sqlConn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        sqlConn.Close();
        return dt;
    }
     
    VB.NET
     
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            GridView1.DataSource = ProductsData()
            GridView1.DataBind()
        End If
    End Sub
    Private Function ProductsData() As DataTable
        Dim MyConstring As String = "Data Source=Source;Initial Catalog=NORTHWND;Integrated Security=True"
        Dim sqlConn As New SqlConnection(MyConstring)
        Dim cmd As New SqlCommand("SELECT * FROM Products", sqlConn)
        sqlConn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        sqlConn.Close()
        Return dt
    End Function
     
    NORTHWND database is used for this example and data is taken from Products table. ProductsData() function will get data in DataTable object using SQL connection and DataAdapter object.
     
  8. Add a Timer Tick event in code file and write below code in it

    C#
     
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        GridView1.DataSource = ProductsData();
        GridView1.DataBind();
     
        Label1.Text = DateTime.Now.ToString();
    }
     
    VB.NET
     
    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        GridView1.DataSource = ProductsData()
        GridView1.DataBind()
     
        Label1.Text = DateTime.Now.ToString()
    End Sub
     
    This event will fire after every 10 seconds and it will bind data to GridView.

     
  9. Now see the web site in browser

 

 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to upload file in folder and save file path in database using ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net