Hi ,
Check this Example
It work Fine Hope it help
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function SelectAll(id) {
var frm = document.forms[0];
for (i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].type == "checkbox") {
if (frm.elements[i].disabled == true) {
frm.elements[i].checked = false;
}
else {
frm.elements[i].checked = document.getElementById(id).checked;
}
}
}
}
function unCheck(id) {
var frm = document.forms[0];
var paren = document.getElementById(id);
var flag = true;
for (i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].type == "checkbox") {
if (!frm.elements[i].checked) {
paren.checked = false;
if (frm.elements[i] != paren)
flag = false;
}
}
}
if (flag == true) paren.checked = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridview1" runat="server"
AutoGenerateColumns="False"
OnRowDataBound="Gridview1_RowDataBound" AllowPaging="True" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server"/>
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="Chk1" runat="server" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="test">
<ItemTemplate>
<asp:Label ID="lbl_Val" runat="server" Text='<%# Eval("name") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="HF2" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
DataClassesDataContext db = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var result = from x in db.test1s
select new { x.name };
Gridview1.DataSource = result;
Gridview1.DataBind();
}
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.Header))
{
HF2.Value = ((CheckBox)e.Row.FindControl("Chk1")).ClientID;
((CheckBox)e.Row.FindControl("Chk1")).Attributes.Add("onclick",
"javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("Chk1")).ClientID + "')");
}
else if ((e.Row.RowType == DataControlRowType.DataRow))
{
((CheckBox)e.Row.FindControl("Chk")).Attributes.Add("onclick",
"javascript:unCheck('" + HF2.Value + "','" + ((CheckBox)e.Row.FindControl("Chk")).ClientID + "')");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
if ((CheckBox)row.FindControl("Chk") is CheckBox)
{
if (((CheckBox)row.FindControl("Chk")).Checked == true)
{
Response.Write("<script>alert('" + ((Label)row.FindControl("lbl_Val")).Text + "')</script>");
}
}
}
}
Best Regards
M.Mitwalli