Click here to Skip to main content
15,915,763 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone

I have a GridView in that i'm using CheckBoxes,i want select All checkBoxes at a time for that i wrote code like this

C#
protected void chkSelectAllTo_CheckedChanged(object sender, EventArgs e)
      {
         CheckBox chk;
          foreach (GridViewRow rowItem in gvTo.Rows)
          {
              chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
              chk.Checked =((CheckBox)sender).Checked;
          }

      }


C#
<asp:CheckBox ID="chkSelectAllTo" runat="server" Text="Select All"  AutoPostBack="true" OnCheckedChanged="chkSelectAllTo_CheckedChanged"  />


it's working perfectly,but i don't want postback on everytime,for that i remove AutoPostBack property.But it's not working can anyone plz help me to do this.

Thank u,
Posted
Comments
vangapally Naveen Kumar 14-Aug-12 0:48am    
are You tried with ajax update panel...

This problem can be solve using client side script.

Now Your Checkbox will look like
<pre lang="HTML">
<asp:checkbox id="chkSelectAllTo" runat="server" text="Select All" onclick="SelectAll(this)" xmlns:asp="#unknown" />

and to handle this 'SelectAll' event write following script.
<pre lang="HTML">
<script type='text/javascript'>
function SelectAll(chkBxObj)
{
	var gridObj = document.getElementById('GridObjID');
	for(var index = 0 ; index < gridObj.rows.length; index++)
	{
		//find here your checkbox objects in Grid Row, 
		var checkBoxObj = gridObj.rows[index].children[0].children[0]; // You have to mention here exact depth of your check box Control in Grid View Row.
		//now set check box check/Uncheck
		checkBoxObj.checked = chkBxObj.checked;
		
	}
	return false
}
</script>
 
Share this answer
 
Use Jquery for doing this

First add reference of jquery library

JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script></script>


Then give some class name for your grid view checkbox for eg
JavaScript
class="myCheckbox"


Then Add this script

JavaScript
<script type="text/javascript">
$('#chkSelectAllTo').change(function () {
                if ($(this).attr('checked') == 'checked') {
                    $('.myCheckbox').attr('checked','checked');
                }
                else {
                    $('.myCheckbox').removeAttr('checked');
                }
            });
</script>
 
Share this answer
 
u want to perform client side coding???
 
Share this answer
 
ASP.NET
Use below code.. for avoiding postback 
<asp:scriptmanager id="ScriptManager" runat="server" xmlns:asp="#unknown" />
<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
    <contenttemplate>
your code.....
</contenttemplate>
</asp:updatepanel> 
 
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