Ans:-
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>ChkBx wth GrdVw Operations</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeaderAll" runat="server" AutoPostBack="true" oncheckedchanged="chkAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sid">
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%# Bind("sid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("sid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sname">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("sname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("sname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<br />
</center>
</div>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection cn;
static SqlCommand cmd;
static SqlDataAdapter da;
static DataTable dt;
static string connStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDatatoGv();
}
}
void GetDatatoGv()
{
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spGetStateData";
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
cn.Close();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)Gridview1.HeaderRow.FindControl("chkHeaderAll");
foreach (GridViewRow row in Gridview1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkChild");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}
}
}
web.config:-
<connectionStrings>
<add name="conStr" connectionString="server=.;database=subsdb;user id=sa;pwd=123"/>
</connectionStrings>
Create Table & SP:-
use subsdb
create table state(sid int constraints p_key primary key,sname varchar(30))
create proc spGetStateData
as
begin
select sname,sid from state
end
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>ChkBx wth GrdVw Operations</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeaderAll" runat="server" AutoPostBack="true" oncheckedchanged="chkAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sid">
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%# Bind("sid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("sid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sname">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("sname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("sname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<br />
</center>
</div>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection cn;
static SqlCommand cmd;
static SqlDataAdapter da;
static DataTable dt;
static string connStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDatatoGv();
}
}
void GetDatatoGv()
{
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spGetStateData";
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
cn.Close();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)Gridview1.HeaderRow.FindControl("chkHeaderAll");
foreach (GridViewRow row in Gridview1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkChild");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}
}
}
web.config:-
<connectionStrings>
<add name="conStr" connectionString="server=.;database=subsdb;user id=sa;pwd=123"/>
</connectionStrings>
Create Table & SP:-
use subsdb
create table state(sid int constraints p_key primary key,sname varchar(30))
create proc spGetStateData
as
begin
select sname,sid from state
end
No comments:
Post a Comment