Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here not working in all check box why?

jquery


<script type="text/javascript" language="javascript">

function CheckAll(Checkbox) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[3].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;
}
}

</script>

ASP.Code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="624px" CssClass="grid"
AllowPaging="True" AllowSorting="True" BackColor="White" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize = "5"
OnRowUpdating="GridView1_RowUpdating" DataKeyNames="id">

<columns> <asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" onclick="CheckAll(this)"/>
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox ID="chkchild" runat="server" />


<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="updatedby" HeaderText="updatedby" SortExpression="updatedby" />
<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />

<asp:BoundField DataField="imagename" HeaderText="imagename" SortExpression="imagename" />


<asp:ImageField DataImageUrlField="uploadimage" HeaderText="uploadimage" ControlStyle-Width = "80" ControlStyle-Height = "100">
<controlstyle height="100px" width="80px">


<asp:CommandField ShowEditButton="True" />



<footerstyle backcolor="White" forecolor="#000066">
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<pagerstyle backcolor="White" forecolor="#000066" horizontalalign="Left">
<rowstyle forecolor="#000066">
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<sortedascendingcellstyle backcolor="#F1F1F1">
<sortedascendingheaderstyle backcolor="#007DBB">
<sorteddescendingcellstyle backcolor="#CAC9C9">
<sorteddescendingheaderstyle backcolor="#00547E">
Posted
Comments
Robert Welliever 1-Nov-14 3:37am    
That's not JQuery, that's just plain JavaScript.
Member 10918596 1-Nov-14 3:40am    
ya sorry that s javasript
Robert Welliever 1-Nov-14 3:40am    
Also I see you don't have the variable i instantiated correctly.
Instead of:
for (i = 1; i < GridView1.rows.length; i++)
use:
for (var i = 1; i < GridView1.rows.length; i++)
DamithSL 1-Nov-14 4:17am    
Robert, this is javascript. we can directly assign value and continue using it.
Robert Welliever 1-Nov-14 3:46am    
One other thing. It is very difficult to debug JavaScript (as you have noticed). If you debug in Chrome, you can right click the page and then select "Inspect Element" from the context menu. You will see a little red circled X if there are JavaScript errors, and it will explain the errors. Internet Explorer will not give you that information as easily.

1 solution

you need to use proper index of GridView column.

In your case the index should be 0.

Change this line :
C#
GridView1.rows[i].cells[3].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;



with this

C#
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;


let me know if it works for you!
 
Share this answer
 
Comments
Member 10918596 1-Nov-14 4:00am    
thanks it s working
Member 10918596 1-Nov-14 4:02am    
pls explain this row
Member 10918596 1-Nov-14 4:08am    
i solved my self robert see above code
abhijeetgupta1988 1-Nov-14 4:08am    
In your javascript GridView1 collection holds the array of rows.
You iterated through all rows
i.e. GridView1.rows[i]
and in each row's 1 cell i.e. cells[0] ,first input tags value i.e. getElementsByTagName("INPUT")[0].checked is set by DOM operation.
abhijeetgupta1988 1-Nov-14 4:16am    
@Robert This code has worked for him! I also used this in several sites.
I know its javascript so will not work in JS disabled browser,will take lot of time if no. of rows are in thousands.

kindly share your view on its other bugs and please suggest some alternates so we can also benefit!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900