Click here to Skip to main content
11,703,632 members (71,205 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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 11-May-13 1:32am
siva575581
Edited 11-May-13 2:51am
v7
Comments
Bikash Prakash Dash at 11-May-13 7:58am
   
Please make it clear what you want ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<asp:gridview id="grid" runat="server" xmlns:asp="#unknown">

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


<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>


<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>
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

here is a sample i've used check it and modify as per your requirement

<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
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;
           }
 
       }
   }
  Permalink  

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

  Print Answers RSS
0 Maciej Los 570
1 OriginalGriff 500
2 Sergey Alexandrovich Kryukov 410
3 F-ES Sitecore 165
4 Andy Lanng 159
0 OriginalGriff 8,519
1 Sergey Alexandrovich Kryukov 7,667
2 CPallini 4,987
3 Maciej Los 4,686
4 Mika Wendelius 3,566


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 21 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100