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() {
    }
   
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Timer ID="Timer1" runat="server" Interval="1000">
    </asp:Timer>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers >
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <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>
      </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField  HeaderText="<font color='red'>Link Name</font>">
      <ItemTemplate >
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("column2") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
         
  </Columns>
  </asp:GridView>
    </ContentTemplate>
    </asp:UpdatePanel>
  </div>
  </form>
</body>
</html>


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");
 
  bindData();
 
}
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];
  GridView1.DataBind();
  Label1.Text = "Grid Refreshed at :"+ DateTime.Now .ToString ();
}
Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.