aspxgridview and checkbox problem
-
Quote:
hi, I am using an aspxgridview and two checkboxes(for header and dataitem).When I check header checkbox I want to check all checkbox.I am also using java script code.essentially, I can do this, but I must check header checkbox twice times.I didnt understand why it is happening.ı am writing this code checkbox_checked event and header checkbox's autopostpak is true. When I used normal gridview I didnt have any problem I could do this without problem., <script language="javascript" type="text/javascript"> function SelectAll(id) { var IsChecked = id.checked; var Chk = id; // Parent = document.getElementById('GrdPerliste'); Parent = document.getElementById('<% = GrdPerliste.ClientID%>'); var items = Parent.getElementsByTagName('input'); for (i = 0; i < items.length; i++) { var deneme = items[i].type; if (items[i].id != Chk && items[i].type == "checkbox") { if (items[i].checked != IsChecked) { items[i].click(); } } } } </script> protected void Checksec_CheckedChanged2(object sender, EventArgs e) { CheckBox chk = (CheckBox)sender; GridViewHeaderTemplateContainer bulundugucontainer = (GridViewHeaderTemplateContainer)chk.NamingContainer; ((CheckBox)bulundugucontainer.FindControl("Checksec")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)bulundugucontainer.FindControl("Checksec")).ClientID + "')"); }
-
Quote:
hi, I am using an aspxgridview and two checkboxes(for header and dataitem).When I check header checkbox I want to check all checkbox.I am also using java script code.essentially, I can do this, but I must check header checkbox twice times.I didnt understand why it is happening.ı am writing this code checkbox_checked event and header checkbox's autopostpak is true. When I used normal gridview I didnt have any problem I could do this without problem., <script language="javascript" type="text/javascript"> function SelectAll(id) { var IsChecked = id.checked; var Chk = id; // Parent = document.getElementById('GrdPerliste'); Parent = document.getElementById('<% = GrdPerliste.ClientID%>'); var items = Parent.getElementsByTagName('input'); for (i = 0; i < items.length; i++) { var deneme = items[i].type; if (items[i].id != Chk && items[i].type == "checkbox") { if (items[i].checked != IsChecked) { items[i].click(); } } } } </script> protected void Checksec_CheckedChanged2(object sender, EventArgs e) { CheckBox chk = (CheckBox)sender; GridViewHeaderTemplateContainer bulundugucontainer = (GridViewHeaderTemplateContainer)chk.NamingContainer; ((CheckBox)bulundugucontainer.FindControl("Checksec")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)bulundugucontainer.FindControl("Checksec")).ClientID + "')"); }
First, your issue/flow of why two clicks This happens as you add the onclick attribute in checkchanged event. So, for a fresh page, first click postbacks and you register javaScript code for checkbox. Second click uses this registered JS method and you get what you want. You can do it without any postback or server event - just using a Javascript. You need to attach the JS code to checkbox in Grid's RowDataBound event where very rowbind is called. Once this is done, single click would work. Have a look at these: Using JavaScript with ASP.Net GridView Control[^] Using jQuery without RowDataBound code[^]
Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]