Home  »  Articles  »  ASP.Net  »

How to Make AutoRefresh of a gridview in asp.net?

 Posted By : Manoranjan SahooPosted Date : 15/11/2010 02:36:31 PM Category : ASP.Net Points : 8 (Rs 3.00)

Sometimes we need to make the gridview as auto refresh within some time gap. We can achieve this with the help of ASP.Net AJAX.

1. First put the grid view within the updatepanel and one ajax timer.

2. attach one trigger to that update panel which will fire with the timer control's tick event.


check below code :

in aspx page make the code like below :

<head runat="server">
  <title>Untitled Page</title>
  <script type="text/javascript">
    function pageLoad() {
  <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Timer ID="Timer1" runat="server" Interval="1000">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers >
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    <ContentTemplate >
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
  <Columns >
     <asp:TemplateField  HeaderText="<font color='red'>Name</font>">
      <ItemTemplate >
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("column1") %>'></asp:Label>
     <asp:TemplateField  HeaderText="<font color='red'>Link Name</font>">
      <ItemTemplate >
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("column2") %>'></asp:Label>

Now in page.aspx.cs make code like below :

SqlConnection cn;
protected void Page_Load(object sender, EventArgs e)
  cn = new SqlConnection(" put your connection string");
void bindData()
  string strsql = "Select column1,column2 from Table1 order by column1";
  SqlDataAdapter dacontent = new SqlDataAdapter(strsql, cn);
  DataSet dscontent = new DataSet();
  string content = "";
  dacontent.Fill(dscontent, "TableData");
  GridView1.DataSource = dscontent.Tables[0];
  Label1.Text = "Grid Refreshed at :"+ DateTime.Now .ToString ();
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.