Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have grid with two item templates

template1 with header [select all] check box
and item template consists of check boxes

template2 with header [select all2] check box
and item template consists of check boxes2

i need a java script

when i select [select all] check box it needs to select all check boxs in that item template
and when i select [select all2] check box it needs to select all check boxs in that item template2


thank q
Posted
Updated 11-May-13 2:51am
v7
Comments
Bikash Prakash Dash 11-May-13 7:58am    
Please make it clear what you want ?

<asp:gridview id="grid" runat="server" xmlns:asp="#unknown">
<columns> <asp:templatefield>
<headertemplate>
<asp:checkbox id="OneCheckBoxAll" runat="server" text="[Check All] Previously Assigned">
onclick="checkAll('oneCheckBox',this);" />
</headertemplate>
<itemtemplate>

<asp:checkbox id="oneCheckBox" runat="server" text="<%#Eval("one")%>" />


<asp:templatefield>
<headertemplate>
<asp:checkbox id="TwoCheckBoxAll" runat="server" text="[Check All] Currently Assigned">
onclick="checkAll('TwoCheckBox',this);" />
</headertemplate>
<itemtemplate>

<asp:checkbox id="TwoCheckBox" runat="server" text="<%#Eval("two")%>" />






above is grid with multiple checkboxes

javascript is



<script type="text/javascript">
function checkAll(fieldsname,field)
{
var n=aspnetForm.elements.length;
for (var i=0;i<n;i++)>
{
if (aspnetForm.elements[i].id.indexOf(fieldsname) !=-1)
{
aspnetForm.elements[i].checked = field.checked;
}
}
}
</script>
 
Share this answer
 
v2
here is a sample i've used check it and modify as per your requirement

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="empid" DataSourceID="ObjectDataSource1">

            <Columns>

                <asp:TemplateField HeaderText="empid" SortExpression="empid">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("empid") %>'></asp:Label>
                    </EditItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
                            oncheckedchanged="CheckBox1_CheckedChanged" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox2" runat="server" Text='<%# Eval("empid") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="empname" SortExpression="empname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("empname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox4" runat="server" AutoPostBack="True"
                            oncheckedchanged="CheckBox4_CheckedChanged" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox3" runat="server" Text='<%# Eval("empname") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            </Columns>
        </asp:GridView>


C# code for that
C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox cb1selall = GridView1.HeaderRow.FindControl("CheckBox1") as CheckBox;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           CheckBox cb1 = GridView1.Rows[i].FindControl("CheckBox2") as CheckBox;
           if (cb1selall.Checked)
           {
               cb1.Checked = true;
           }
           else
           {
               cb1.Checked = false;
           }

       }
   }
   protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox cb2selall = GridView1.HeaderRow.FindControl("CheckBox4") as CheckBox;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           CheckBox cb1 = GridView1.Rows[i].FindControl("CheckBox3") as CheckBox;
           if (cb2selall.Checked)
           {
               cb1.Checked = true;
           }
           else
           {
               cb1.Checked = false;
           }

       }
   }
 
Share this answer
 

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