A fully selected solution in the asp.net GridView control

  • 2020-05-07 19:32:47
  • OfStack

The first kind: using the client side control implementation
JS:
 
<script type="text/javascript"> 
function checkAll() 
{ 
var checklist=document.getElementsByTagName("input"); 
for(var i=0;i<checklist.length;i++) 
{ 
if(checklist[i].type=="checkbox") 
{ 
checklist[i].checked=document.form1.ck.checked; 
} 
} 
} 
</script> 

GridView controls:
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > 
<Columns> 
<asp:BoundField DataField="ProductID" HeaderText=" Product number " /> 
<asp:TemplateField> 
<HeaderTemplate> 
<input id="ck" type="checkbox" onclick="checkAll();" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="checkbox1" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 
            

Type 2: implementation using server-side controls
 
protected void  Future generations _CheckedChanged(object sender, EventArgs e) 
{ 
if ( Future generations .Checked == true) 
{ 
for (int i = 0; i < GridView1.Rows.Count; i++) 
{ 
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox; 
if (ck!=null) 
{ 
ck.Checked = true; 
} 
} 
} 
else 
{ 
for (int i = 0; i < GridView1.Rows.Count; i++) 
{ 
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox; 
if (ck != null) 
{ 
ck.Checked = false; 
} 
} 
} 
} 

Related articles: