Advertisement
In this article I will explain Auto
Refresh Gridview Ajax with Timer in asp.net.
I am creating a database table in Sql
server
USE
[chinnu]
GO
/******
Object: Table [dbo].[EmployeeInfo] Script Date: 10/31/2013
22:39:45 ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
CREATE
TABLE
[dbo].[EmployeeInfo](
[EmpId]
[nvarchar](50)
NOT
NULL,
[EmpName]
[nvarchar](50)
NOT
NULL,
[EmpAddress]
[nvarchar](50)
NOT
NULL,
[EmpDesignation]
[nvarchar](50)
NOT
NULL
)
ON
[PRIMARY]
GO
I am created an empty solution with
name AutoRefresh.
Now I am adding a webform
GridRefresh.aspx.
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeBehind="GridRefresh.aspx.cs"
Inherits="AutoRefresh.GridRefresh"
%>
<!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></title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:UpdatePanel
ID="UpdatePanel1"
runat="server">
<ContentTemplate>
<asp:Timer
ID="Timer1"
Interval
=
"2000"
OnTick
="AutoRefreshGrid"
runat="server">
</asp:Timer>
<asp:ScriptManager
ID="ScriptManager1"
runat="server">
</asp:ScriptManager>
<asp:GridView
ID="gridview"
runat="server"
AutoGenerateColumns="False"
>
<Columns>
<asp:BoundField
DataField="EmpId"
HeaderText="EmployeeId"
InsertVisible="False"
/>
<asp:BoundField
DataField="EmpName"
HeaderText="EmployeeName"
InsertVisible="False"
/>
<asp:BoundField
DataField="EmpAddress"
HeaderText="EmployeeAddress"
/>
<asp:BoundField
DataField="EmpDesignation"
HeaderText="EmployeeDesignation"
/>
</Columns>
</asp:GridView>
<asp:Label
ID="Label1"
runat="server"
Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Now
I am writing the logic in aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;
using
System.Configuration;
namespace
AutoRefresh
{
public
partial
class
GridRefresh
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(!IsPostBack)
{
BindGridData();
}
}
private
void
BindGridData()
{
try
{
using
(SqlConnection
con = new
SqlConnection(ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString))
{
SqlCommand
command = new
SqlCommand("SELECT
* from EmployeeInfo",
con);
SqlDataAdapter
da = new
SqlDataAdapter(command);
DataTable
dt = new
DataTable();
da.Fill(dt);
gridview.DataSource
= dt;
gridview.DataBind();
}
}
catch
(Exception
ex)
{
}
}
protected
void
AutoRefreshGrid(object
sender, EventArgs
e)
{
BindGridData();
Label1.Text
= DateTime.Now
+ "AutoRefreshing
";
}
}
}
Now I am adding the web.config
connection string
<connectionStrings>
<add
name="Connstring"
connectionString="Data
Source=DotNetSharePoint;Database=Chinnu;User
Id=sa;Password=password123;"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
finally
Press F5,we can the outPut.
Looking for the best IVF doctor in Delhi? Renowned for cutting-edge fertility treatments, Delhi is home to leading IVF specialists offering personalized care and advanced techniques. These experts provide services like IVF, ICSI, egg freezing, and fertility counseling, helping couples achieve their dream of parenthood.
ReplyDeleteEquipped with state-of-the-art facilities and high success rates, they ensure ethical practices and compassionate support throughout the journey. Whether you face male or female infertility issues, consult a trusted IVF doctor in Delhi to receive tailored solutions and expert guidance.