dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersLaxmikant
baskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » ASP.NET »

How to select all check boxes wheni click header check box then even if i uncheck header


Posted Date: 30 Jun 2012      Posted By:: sunny     Member Level: Bronze    Member Rank: 0     Points: 5   Responses: 3



I have a gridview with a header row checkbox for Select All. When I check the checkbox, all items in the gridview get checked. When I uncheck the checkbox, all items in the gridview get unchecked. Now I want to take it a step further.

I would like it so that if ANY item in the gridview list get unchecked after the Select All has checked it, then the Select All checkbox in the header should change to unchecked.

Is there a way to do this on the using C# code for asp.net Can someone offer some code suggestions?url reference with same matching.





Responses

#678246    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 30/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

Your question itself a answer, when you uncheck any item you need to uncheck the header checkbox too.
but when you check any item then you need to loop through all checkboxes for set flag if all checks are dont.
if all checks are done then you can check the header check box


//while check
set flag=false
for each control
{
if(control is checked)
set flag=true
}

if flag=true
check header checkbox


hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
#678263    Author: SonyMadhu      Member Level: Gold      Member Rank: 45     Date: 30/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,


Refer the belowww...

<script type="text/javascript">
function SelectAllChkboxes(spanChk) {
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) {
if (elm[i].checked != xState)
elm[i].click();
}
}
</script>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" DataKeyNames ="AccNo">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllChkboxes(this);" runat="server"
type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>





Hope this will help you.

Regards,
Madhu
Be so hapy wen others look at you,they become hapy too



 
#678414    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 02/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">



function SelectAllCheckboxesMoreSpecific(varspanChkbox)

{

var varIsChecked = varspanChkbox.checked;

var vChk = varspanChkbox;

Parent = document.getElementById('GridView1');

for(i=0;i< Parent.rows.length;i++)

{

var vtr = Parent.rows[i];

var vtd = vtr.firstChild;

var vitem = vtd.firstChild;

if(vitem.id != vChk && vitem.type=="checkbox")

{

if(vitem.checked!= varIsChecked)

{

vitem.click();

}

}

}

}


</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px">

<Columns>

<asp:TemplateField HeaderText="Roles">

<HeaderTemplate>

<asp:CheckBox ID="chkSelectAll" onclick="SelectAllCheckboxesMoreSpecific(this);" runat="server" />

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="EmpID" HeaderText="Emp ID" ReadOnly="True" />

<asp:BoundField DataField="EmpName" HeaderText="Emp Name" ReadOnly="True" />



</Columns>



</asp:GridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataRow dtrow;
DataTable dtNewTable;
dtNewTable = new DataTable();
DataColumn dtcol, dtcol1;
dtcol = new DataColumn();
dtcol1 = new DataColumn();
dtcol.DataType = System.Type.GetType("System.String");
dtcol.ColumnName = "EmpID";
dtNewTable.Columns.Add(dtcol);
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "EmpName";
dtNewTable.Columns.Add(dtcol1);
int i;
for (i = 0; i < 4; i++)
{


dtrow = dtNewTable.NewRow();
dtrow["EmpID"] = "";
dtrow["EmpName"] = "";
dtNewTable.Rows.Add(dtrow);

GridView1.DataSource = dtNewTable;
GridView1.DataBind();
}

}

}

}





Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/






 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Pls send the all gridview scenario's with examples?
Previous : Plz help it is very urgent
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.