dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersNanda Kishore
Ultimaterengan
subashini
Amruta
Jivani
Rohit Rathod
Ranjith Kumar
naveensanagasetti
Phagu Mahato
Sans QA
Padma
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » .NET »

Checked or unchecked when i click selectall checkbox


Posted Date: 02 Jul 2012      Posted By:: Lawrence     Member Level: Gold    Member Rank: 317     Points: 3   Responses: 4



Hi Everyone,
I bound some values in gridview... 1st column is student name and 2nd and 3rd is fully checkbox.... In header template also i used checkbox... When i click the header template checkbox in 2nd column then all the checkbox can be checked if suppose i checked some values in middle that checkbox not checked.... How to do....

Thanks In Advanced




Responses

#678402    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 02/Jul/2012   Rating: 2 out of 52 out of 5     Points: 2

Try this.


foreach(GridViewRow gvRow in gvTest.Rows)
{
CheckBox rowChkBox = ((CheckBox)gvRow.FindControl("CHK"));

rowChkBox.Checked = true;
}



Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#678404    Author: Lawrence      Member Level: Gold      Member Rank: 317     Date: 02/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

I attached my screen shot.... Kindly find and tell me...How to do...

I have 3 columns in gridview like....

StudentName SelectAll(CheckBox) SelectAll(CheckBox)

Lawrence ChkBox ChkBox

Grace ChkBox ChkBox

If i click first selectall header template checkbox and checked all the columns in that checkbox but if i checked in middle that checkbox should be unchecked....same process in second selectall header template checkbox....How to do...Just tell the solution....



 
#678405    Author: sudhajosyula      Member Level: Gold      Member Rank: 46     Date: 02/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi Lawrence,
use the below code and see if it meets your requirement

<html>
<head>
<script type="text/javascript" language="javascript">
function checkAll(objRef)
{
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i=0;i<inputList.length;i++)
{
var row = inputList[i].parentNode.parentNode;
if(inputList[i].type == "checkbox" && objRef != inputList[i])
{
if (objRef.checked)
inputList[i].checked=true;
else
inputList[i].checked=false;
}
}

function Check_Click(objRef)
{
var row = objRef.parentNode.parentNode;
var GridView = row.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i=0;i<inputList.length;i++)
{
var headerCheckBox = inputList[0];
var checked = true;
if(inputList[i].type == "checkbox" && inputList[i] != headerCheckBox)
{
if(!inputList[i].checked)
{
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;
}
</script>
</head>
<body>
<form id="frm" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" OnRowDataBound = "RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick = "Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country"/>
<asp:BoundField DataField="PostalCode" HeaderText= "PostalCode"/>
</Columns>
</asp:GridView>
</form>
</body>
</html>






 
#678406    Author: Ultimaterengan   Online     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 : Product code generation
Previous : How to run the solution in dotnet using team foundation server
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.