CheckBox:Select All CheckBoxes On Click Event in Asp.net C# Using Datalist.

HTML Markup

Step 1: Select CheckBox

  1. <asp:Label ID="Label1" runat="server" Text="Label">Select All</asp:Label>   
  2. <asp:CheckBox ID="chkHeader" runat="server" AutoPostBack="true" OnCheckedChanged="CheckAll" />   
Step 2: Select DataList and use another CheckBox control for checking all checkboxes.
  1. <asp:DataList ID="dlforms" runat="server" RepeatColumns="6" Width="1800px">  
  2.     <ItemTemplate>  
  3.         <table>  
  4.             <tr>  
  5.                 <td>  
  6.                     <asp:Label ID="lblformid" Visible="false" runat="server" Text='<%#Eval("Form_id")%>'></asp:Label>////Bind From Database  
  7.                     <asp:CheckBox ID="chkRow" runat="server" Font-Bold="true" Text='<%#Eval("Form_name")%>' ////Bind From Database OnCheckedChanged="check" /> </td>  
  8.             </tr>  
  9.         </table>  
  10.     </ItemTemplate>  
  11. </asp:DataList>  
C#

Step 3: Fill DataList from DataBase.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if(!IsPostBack)  
  4.     {  
  5.         Fill_grid();  
  6.     }  
  7. }  
  8. protected void Fill_grid()  
  9. {  
  10.     string dt = "select * from tbl_frm";  
  11.     DataTable dr = c1.filldt(dt);  
  12.     dlforms.DataSource = dr;  
  13.     dlforms.DataBind();  
  14. }  
Step 4: OnCheckedChanged="CheckAll" Event Code.
  1. protected void CheckAll(object sender, EventArgs e)  
  2. {  
  3.     CheckBox btnComplain = sender as CheckBox;  
  4.     if(btnComplain.Checked == true)  
  5.     {  
  6.         foreach(DataListItem grd in dlforms.Items)  
  7.         {  
  8.             CheckBox chb = (CheckBox) dlforms.Items[grd.ItemIndex].FindControl("chkRow");  
  9.             chb.Checked = true;  
  10.         }  
  11.     }  
  12.     else  
  13.     {  
  14.         foreach(DataListItem grd in dlforms.Items)  
  15.         {  
  16.             CheckBox chb = (CheckBox) dlforms.Items[grd.ItemIndex].FindControl("chkRow");  
  17.             chb.Checked = false;  
  18.         }  
  19.     }  
  20. }  
Step 5: OnCheckedChanged="check" Event Code.
  1. protected void check(object sender, EventArgs e)   
  2. {   
  3.   
  4. }