Friday, 15 July 2016

Auto Refresh Update GridView In Asp.Net Ajax With Timer.

refresh a Grid View automatically at regular intervals
Description:

In this example we explain that how to refresh a Grid View automatically at regular intervals in asp.net.or refresh grid view after every 5 second in asp.net using C#.

Here we use we use Timer control to achieve this requirements. Simply we add the Grid view in the AJAX Update Panel control and set the timer interval in which we have to refresh the grid view automatically.

Below the code that demonstrate auto refresh grid view after specified interval in asp.net.

AutoRefreshGridview.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoRefreshGridview.aspx.cs"
    Inherits="AutoRefreshGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to refresh an ASP.NET GridView automatically at regular intervals</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" Interval="3600" OnTick="Timer1_Tick">
            </asp:Timer>
            <asp:GridView ID="grd_Employee" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                AllowPaging="True" AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="EmpID" HeaderText="EmpID" />
                    <asp:BoundField DataField="EmpName" HeaderText="EmpName" />
                    <asp:BoundField DataField="Salary" HeaderText="Salary" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>


AutoRefreshGridview.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AutoRefreshGridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

        grd_Employee.DataBind();

    }
}


0 comments:

Post a Comment