views:

243

answers:

9

In button click event how can I check all check boxes in gridview? I dont need header checkbox.

Please provide your knowledge

awaiting your response....

Thanks

+1  A: 

C# Let's say you have a check all button

<asp:CheckBox ID="chkSelectAll" runat="server" Text="SelectAll" 
AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />

and in that click event you would do something like:

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
   CheckBox chk;   //assuming your gridview id=GridView1

   foreach (GridViewRow rowItem in GridView1.Rows) 
  {   
     chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1")); 
     chk.Checked =((CheckBox)sender).Checked;
   }
}

javascript approach:

<script language="javascript">

function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox= (spanChk.type=="checkbox") ? 
    spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;

for(i=0;i<elm.length;i++)
  if(elm[i].type=="checkbox" && 
          elm[i].id!=theBox.id)
  {
    //elm[i].click();
   if(elm[i].checked!=xState)
     elm[i].click();
   //elm[i].checked=xState;
  }
 }
</script>

Checkbox field as so:

<asp:CheckBox ID="chkAll" runat="server" Text="SelectAll" 
onclick="javascript:SelectAllCheckboxes(this);" />
TStamper
just a note: chk1 in c# code is your checkbox Id
TStamper
ya its working whwn the check box is in outside the grid but inside it doesnt work
Domnic
which one did you use?javascript or c#?
TStamper
+2  A: 
<input id="btncheckall" type="button" value="select all" />

add click event handler to button above (with jQuery)

<script type="text/javascript">
  $(function(){

    $("#btncheckall").click(function(){

      $("#gridview input:checkbox").attr("checked","checked");

    });

  });
</script>

or you can use checkbox. this is a checkbox outside gridview

<input id="checkall" type="checkbox" />

add change event handler to checkbox above (with jQuery)

<script type="text/javascript">
  $(function(){

    $("#checkall").change(function(){

      $("#gridview input:checkbox").val( $(this).val() );

    });

  });
</script>
Anwar Chandra
A: 

If you're using jquery you could use the $('input:checkbox') selector so something like

<script type="text/javascript">
  $(function() {
    $('#NameOfButtonToSelectAll').click( function() {
      $('input:checkbox').each( function() {
        this.checked = !this.checked;
      });
    });
  });
</script>
David G
i need javascript
Domnic
how can i call this function in client side
Domnic
+1  A: 

Jquery can make this easier. Hook into the external boxes onslected event, and inside there iterate the grid boxes selecting them all.

This is a great example of the evils of asp.net and how it's use by new developers really cripples them into thinking that all processing and interaction takes place server side, and all sorts of crazy hacks take place to maintain this illusion. It's backwards and insane.

Pierreten
im new in jquery pls explain or give solution ...can we do it by using javascript?
Domnic
Jquery is built on JavaScript
Pierreten
+1  A: 

Try this:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate><asp:CheckBox ID="SelectUnSelectAllCheckBox" runat="server" /></HeaderTemplate>
            <ItemTemplate><asp:CheckBox ID="SelectCheckBox" runat="server" /></ItemTemplate>
        </asp:TemplateField>
        <!-- Other columns are omitted -->
    </Columns>
</asp:GridView>


<script type="text/javascript">
    $(document).ready(function(e) {
        $("input[id$='SelectUnSelectAllCheckBox']").change(function() {
            $("input[id$='SelectCheckBox']").attr("checked", this.checked);
        });
    });
</script>
Mehdi Golchin
i didnt used header templateoutside the grid?
Domnic
It's possible to put `SelectUnSelectAllCheckBox` on everywhere you want.
Mehdi Golchin
how to call this script in client side
Domnic
@Mehdi- this would definitely work, but should add alternative to check all to deselect all if already checked,@Domnic- this is a jquery solution, just a note: so you would have to include the jquery plugin
TStamper
@TStamper, you are right thanks.
Mehdi Golchin
A: 

Kindly check it out and let me know when you got it worked.

Using Javascript :

http://wiki.asp.net/page.aspx/281/check-uncheck-checkboxes-in-gridview-using-javascript/

Using Serverside Script: (VB.Net)

http://aspnet.4guysfromrolla.com/articles/052406-1.aspx

solairaja
A: 

Using jQuery:

$('#SelectAll').click(function(){
  var checked = $(this).is(':checked');
  var allCheckboxes = $('table input:checkbox');
  if(checked)
      allCheckboxes.attr('checked','checked');
  else
      allCheckboxes.removeAttr('checked');
});

You probably want to change the selectors, assuming you have a class for your grid and checkbox.

Kobi
how ca i call this function in my checkbox id="chk1"gridview checkbox id="gridchk"
Domnic
You'll have easier time working with a CSSClass - those IDs are changed on the client side. You can probably do `$("[id$='chk1']")`, but again, working with a class is simpler.
Kobi
A: 

Assign a class to all your grid row check boxes and use the below script to get them all.

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
        }
    }
    return classElements;
}

And you've to call it this way:

var messages = getElementsByClass("childbox");

Assign a class childbox to grid row child box.

document.getElementById("parentbox").onclick  = function() {
for(var index=0; index < messages.length; index++) {
 // prompt the content of the div
 //message[index].checked = (message[index].checked) ? false : true;
}
}

you'll assign the parentbox class to the parent checkbox which is in grid header.

You don't need to define them - parentbox and childbox.

Ramiz Uddin