Checkbox List in Gridview:
Description:
Description: If the gridview is having checkbox list as a template column and for the
checkbox list, header item is(first item) "ALL", then if you select "ALL" in
checkboxlist, it will all items in the checkboxlist. If you have any scenario,then you can use this code.
Javascript:
<script type="text/javascript"> function chkboxlistchecking(clientids,checkedval) { var a = document.getElementById(clientids); var ar = a.getElementsByTagName("input"); var cnt=0; var notchecked=0; for(i=0;i<ar.length;i++) { var arc = a.getElementsByTagName("label");
for(k=1;k<ar.length;k++) { if(ar[k].checked) cnt = parseInt(cnt,10)+1; else notchecked = parseInt(notchecked,10)+1; } if(ar[i].checked == true) { if(arc[i].innerText == 'All' && parseInt(cnt,10) == 0) { for(k=1;k<ar.length;k++) { ar[k].checked = true; } flag="true"; flag1="false"; } else if(arc[i].innerText == 'All' && parseInt(notchecked,10) > 0 && flag1=="false") { ar[0].checked = false; flag1="true"; flag="false"; } else if(arc[i].innerText == 'All' && parseInt(notchecked,10) > 0 && flag1=="true") { for(f=0;f<ar.length;f++) { ar[f].checked = true; } flag1="false"; flag="true"; } } else if(ar[i].checked == false) { if(arc[i].innerText == 'All' && (parseInt(cnt,10) == ar.length-1) && flag=="false") { ar[0].checked = true; flag="true"; flag1="false"; } else if(arc[i].innerText == 'All' && flag=="true") { for(k=0;k<ar.length;k++) { ar[k].checked = false; } flag="false"; flag1="false"; } } } } </script>
Design View:
<asp:GridView id="GridviewBatch" runat="server" Width="248px" BorderWidth="0px" PageSize="2" Height="250px" CellPadding="0" AutoGenerateColumns="False" onrowdatabound="GridviewBatch_RowDataBound"> <Columns> <asp:BoundField DataField="BatchID" HeaderText="Batch" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"> <HeaderStyle HorizontalAlign="Left"></HeaderStyle> <ItemStyle HorizontalAlign="Left"></ItemStyle> </asp:BoundField> <asp:TemplateField HeaderText="UserGroupName" HeaderStyle-HorizontalAlign="Left"> <ItemTemplate> <dIV class="scrollbars" id="divChk" style="OVERFLOW: auto;height:85px; width:105px"> <asp:checkboxlist id="cblstMenuItems" runat="server" BorderWidth="1px" Visible="True" BorderColor="Black" AutoPostBack="False" Height="40px"></asp:checkboxlist> </dIV> </ItemTemplate> <HeaderStyle HorizontalAlign="Left"></HeaderStyle> </asp:TemplateField> </Columns> </asp:GridView>
Code Behind:
//Connetion string from web.config
string sDsn = ConfigurationSettings.AppSettings["ConnectionString"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGrid(); } } private void BindGrid() { //Binding Gridview with some data DataSet ds = SqlHelper.ExecuteDataset(sDsn, "Admiral_getBatchDetails");
if (ds.Tables.Count > 0) { if (ds.Tables[0].Rows.Count > 0) { GridviewBatch.DataSource = ds.Tables[0]; GridviewBatch.DataBind(); } } } protected void GridviewBatch_RowDataBound(object sender, GridViewRowEventArgs e) { //Binding checkbox list to Gridview if (e.Row.RowIndex != -1 && e.Row.DataItem != null) { //Itendifying checkboxlist in gridview template column CheckBoxList chklist = (CheckBoxList)e.Row.Cells[1].FindControl("cblstMenuItems"); DataSet bpds = SqlHelper.ExecuteDataset(sDsn, "PS_Admiral_GetUsrGroups"); if (bpds.Tables.Count > 0) { if (bpds.Tables[0].Rows.Count > 0) { //Assigning DataSource to checkboxlist chklist.DataSource = bpds.Tables[0]; chklist.DataTextField = "usergroupname"; chklist.DataValueField = "usergroupid"; chklist.DataBind(); chklist.Items.Insert(0, new ListItem("All", "All")); } } chklist.Attributes.Add("onclick", "chkboxlistchecking('" + chklist.ClientID.ToString() + "','" + chklist.Items.Count + "')"); } }
Attachments


|
| Author: Miss Meetu Choudhary 28 Jul 2009 | Member Level: Diamond Points : 2 |
please use the space or ";" after &.lt to display the tag properly ++ Thanks and Regards. Meetu Choudhary. Site Coordinator.
|