ASP.NET - quickly Auto Refresh Only Gridview in c#

Asked By Anil Gajera on 19-Apr-11 12:20 AM
How to quickly auto refresh  only gridview in a c# , asp.net page ?
Ravi S replied to Anil Gajera on 19-Apr-11 12:25 AM
HI Anil

HI

Add the timer
<asp:Timer id="Timer1" runat="server" Interval="2000">
link the above  id to gridview.

Hope it helps you
Ravi S replied to Anil Gajera on 19-Apr-11 12:25 AM
HI Anil


Add the timer
<asp:Timer id="Timer1" runat="server" Interval="2000">
link the above  id to gridview.

Hope it helps you
Jitendra Faye replied to Anil Gajera on 19-Apr-11 12:31 AM

If you want to refresh your GridView, then you can use Timer control with Updatepanel,

For that you have to take one UpdatePanel and in that panel you have to GridView and Timer control.

Now set Timer enable property to true and set Interval for that timer,

Use following code-

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Enabled="true" Interval="1000">
        </asp:Timer>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" HorizontalAlign="Right">
          <Columns>
            <asp:BoundField HeaderText="Message" DataField="Empid" Visible="false" ReadOnly="true" />
            <asp:TemplateField HeaderText="EmpName">
              <ItemTemplate>
                <asp:Label ID="lblMessage" runat="server" Text=' <%# Eval("EmpName") %>'></asp:Label>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Salary">
              <ItemTemplate>
                <asp:Label ID="lblSalary" runat="server" Text=' <%# Eval("Salary") %>'></asp:Label>
              </ItemTemplate>
            </asp:TemplateField>
          </Columns>
        </asp:GridView>
      </ContentTemplate>
    </asp:UpdatePanel>



NOW CODE TO BIND DATA TO GRIDVIEW CONTROL IN TICK EVENT OF TIMER.

protected void Timer1_Tick(object sender, EventArgs e)

{

//CODE FOR BINDING GRIDVIEW

 

}


Now after every 1000 ms gridview will be refreshed().

USE THIS CODE AND LET ME KNOW.





Rajiv Kumar replied to Anil Gajera on 19-Apr-11 12:58 AM
hi
If you want to refresh the webpage then
you have couple of options.

1. You can add meta tag to your webpage so that
it refreshes automatically without any extra
effort on your part.

Just add the below given tag inside your html's
head tag.

<meta http-equiv="refresh" content="300">


2.if you want to refresh only few controls you can use ajax update pannel.

try this code-
<form id="form1" runat="server">
    <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="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>
<br /><br />
<asp:DataGrid ID="GridData" runat="server" Width="100%" GridLines="Both" HeaderStyle-BackColor="#999999" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="pictures" HeaderText="pictures"></asp:BoundColumn>
<asp:BoundColumn DataField="path" HeaderText="path"></asp:BoundColumn>

</Columns>
</asp:DataGrid>
</ContentTemplate>
</asp:UpdatePanel>
    </form>


in code behind
  public void BindData()
    {
      con = new SqlConnection("Data Source=RAJIV;Initial Catalog=employee;Integrated Security=True");
      //cmd.CommandText = "select * from UserFilestbl";
      //cmd.Connection = con;
      //con.Open();
      da = new SqlDataAdapter("select * from UserFilestbl",con);
      da.Fill(ds);
      //cmd.ExecuteNonQuery();
      GridData.DataSource = ds;
      GridData.DataBind();
    }


    protected void Timer1_Tick(object sender, EventArgs e)
    {
      BindData();
      Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
    }


this code will reresh you gridview after every 30 second.
i hope it will give better solution.
Nikhil Mahajan replied to Anil Gajera on 19-Apr-11 01:12 AM
hi anil,

Hi Check this one  

 

 Code behind page :

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();
}


protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
}



Here is HTML desing code:

<form id="form1" runat="server">
<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="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> 
<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>