Check One CheckBox To Check All CheckBoxes

R

rn5a

All the rows in a DataGrid, including the Header, are accompanied with
a CheckBox. I want that when the CheckBox in the Header is checked,
then all the CheckBoxes should automatically get checked. I set the
AutoPostBack property of the CheckBox in the Header to True & am
invoking a sub named 'CheckAllRows' on the CheckedChanged event of this
CheckBox. The CheckBox in the Header exists within the HeaderTemplate
of a TemplateColumn in the DataGrid & the rest of the CheckBoxes reside
within the ItemTemplate of the same TemplateColumn. This is the code
(the ID of the CheckBox in the Header is 'chkHeader' & the IDs of the
rest of the CheckBoxes are 'chkItem'):

Sub CheckAllRows(ByVal obj As Object, ByVal ea As EventArgs)
Dim chkAllRows As CheckBox
Dim chkSelHeader As CheckBox
Dim dgItem As DataGridItem

For Each dgItem In dg1.Items
If (dgItem.ItemType = ListItemType.Header) Then
chkSelHeader = dgItem.FindControl("chkHeader")
Response.Write("hello<br>")
If (chkSelHeader.Checked) Then
If (dgItem.ItemType = ListItemType.Item Or
dgItem.ItemType = ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
chkAllRows.Checked = True
End If
End If
End If
Next
End Sub

Now when I check the CheckBox in the Header, then the page posts but
none of the CheckBoxes apart from the CheckBox in the Header get
checked. Also notice the Response.Write("hello<br>") line which is
within the first If condition. Even that line doesn't get executed
since after post back, the page doesn't display the text 'hello'.

What am I doing wrong here?
 
R

rn5a

What I find is if I get rid of the first If condition which checks
whether the ItemType is Header or not i.e. this line

If (dgItem.ItemType = ListItemType.Header) Then

& modify the For...Next loop in the sub named 'CheckAllRows' like this

On Error Resume Next
For Each dgItem In dgMarks.Items
chkSelHeader = dgItem.FindControl("chkHeader")
If (dgItem.ItemType = ListItemType.Item Or dgItem.ItemType =
ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
If (chkSelHeader.Checked) Then
Response.Write("checked<br>")
chkAllRows.Checked = True
Else
Response.Write("unchecked<br>")
chkAllRows.Checked = False
End If
End If
Next

then when the CheckBox in the Header is checked, all the CheckBoxes get
checked & assuming that the DataGrid has 10 rows, the text 'checked'
appears 10 items but when I uncheck the CheckBox in the Header so that
all the other CheckBoxes get unchecked, then the CheckBox in the header
gets unchecked but all the other CheckBoxes still remain checked & the
page still displays the text 'checked' (& not the text 'unchecked') 10
times!

What's causing this eccentric behavior?

Moreover, the 'On Error Resume Next' is a must else ASP.NET generates
the following error:

Object reference not set to an instance of an object.

pointing to the following If condition:

If (chkSelHeader.Checked) Then

Can someone please tell me what am I doing wrong? This is driving me
nuts!!
 
N

nil

What I find is if I get rid of the first If condition which checks
whether the ItemType is Header or not i.e. this line

If (dgItem.ItemType = ListItemType.Header) Then

& modify the For...Next loop in the sub named 'CheckAllRows' like this

On Error Resume Next
For Each dgItem In dgMarks.Items
chkSelHeader = dgItem.FindControl("chkHeader")
If (dgItem.ItemType = ListItemType.Item Or dgItem.ItemType =
ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
If (chkSelHeader.Checked) Then
Response.Write("checked<br>")
chkAllRows.Checked = True
Else
Response.Write("unchecked<br>")
chkAllRows.Checked = False
End If
End If
Next

then when the CheckBox in the Header is checked, all the CheckBoxes get
checked & assuming that the DataGrid has 10 rows, the text 'checked'
appears 10 items but when I uncheck the CheckBox in the Header so that
all the other CheckBoxes get unchecked, then the CheckBox in the header
gets unchecked but all the other CheckBoxes still remain checked & the
page still displays the text 'checked' (& not the text 'unchecked') 10
times!

What's causing this eccentric behavior?

Moreover, the 'On Error Resume Next' is a must else ASP.NET generates
the following error:

Object reference not set to an instance of an object.

pointing to the following If condition:

If (chkSelHeader.Checked) Then

Can someone please tell me what am I doing wrong? This is driving me
nuts!!

hi....use this java script and you'll have what you want...in this if
you check header checkbox then all the checkbox of the grid will be
checked and when you uncheck the header checkbox then have all the
checkboxes unchecked in the grid
<script language="JavaScript">

This function checks all the checkbox on the state of the header
checkbox
function CheckAll( checkAllBox )
{
var frm = document.Form1;
var ChkState=checkAllBox.checked;

for(i=0;i< frm.length;i++)
{
e=frm.elements;
if(e.type=='checkbox' && e.name.indexOf('Id') != -1)
e.checked= ChkState ;
}

}

This function checks the header checkbox on the basis of the state of
all the checkboxes ...if all checkboxes are checked then the header
checkbox is checked
function CheckChanged()
{
var frm = document.Form1;
var boolAllChecked;
boolAllChecked=true;

for(i=0;i< frm.length;i++)
{
e=frm.elements;

if ( e.type=='checkbox' && e.name.indexOf('Id') != -1 )

if(e.checked== false)
{
boolAllChecked=false;
break;
}
}

for(i=0;i< frm.length;i++)
{
e=frm.elements;

if ( e.type=='checkbox' && e.name.indexOf('checkAll') != -1 )
{
if( boolAllChecked==false)
e.checked= false ;
else
e.checked= true;
break;
}
}
}
</script>

write this within <head></head> and call the function from the html
code and use the html checkbox in the grid and use
onclick="checkchanged" and so on...and you must have the postback
property false of the all checkboxes
 
R

rn5a

Yeah Nil....I know this can be done using JavaScript but I did prefer
doing it using ASP.NET. Let me see......if I finally fail to do it
using ASP.NET, I will revert back to JavaScript to add that
functionality.

What I find is if I get rid of the first If condition which checks
whether the ItemType is Header or not i.e. this line

If (dgItem.ItemType = ListItemType.Header) Then

& modify the For...Next loop in the sub named 'CheckAllRows' like this

On Error Resume Next
For Each dgItem In dgMarks.Items
chkSelHeader = dgItem.FindControl("chkHeader")
If (dgItem.ItemType = ListItemType.Item Or dgItem.ItemType =
ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
If (chkSelHeader.Checked) Then
Response.Write("checked<br>")
chkAllRows.Checked = True
Else
Response.Write("unchecked<br>")
chkAllRows.Checked = False
End If
End If
Next

then when the CheckBox in the Header is checked, all the CheckBoxes get
checked & assuming that the DataGrid has 10 rows, the text 'checked'
appears 10 items but when I uncheck the CheckBox in the Header so that
all the other CheckBoxes get unchecked, then the CheckBox in the header
gets unchecked but all the other CheckBoxes still remain checked & the
page still displays the text 'checked' (& not the text 'unchecked') 10
times!

What's causing this eccentric behavior?

Moreover, the 'On Error Resume Next' is a must else ASP.NET generates
the following error:

Object reference not set to an instance of an object.

pointing to the following If condition:

If (chkSelHeader.Checked) Then

Can someone please tell me what am I doing wrong? This is driving me
nuts!!

hi....use this java script and you'll have what you want...in this if
you check header checkbox then all the checkbox of the grid will be
checked and when you uncheck the header checkbox then have all the
checkboxes unchecked in the grid
<script language="JavaScript">

This function checks all the checkbox on the state of the header
checkbox
function CheckAll( checkAllBox )
{
var frm = document.Form1;
var ChkState=checkAllBox.checked;

for(i=0;i< frm.length;i++)
{
e=frm.elements;
if(e.type=='checkbox' && e.name.indexOf('Id') != -1)
e.checked= ChkState ;
}

}

This function checks the header checkbox on the basis of the state of
all the checkboxes ...if all checkboxes are checked then the header
checkbox is checked
function CheckChanged()
{
var frm = document.Form1;
var boolAllChecked;
boolAllChecked=true;

for(i=0;i< frm.length;i++)
{
e=frm.elements;

if ( e.type=='checkbox' && e.name.indexOf('Id') != -1 )

if(e.checked== false)
{
boolAllChecked=false;
break;
}
}

for(i=0;i< frm.length;i++)
{
e=frm.elements;

if ( e.type=='checkbox' && e.name.indexOf('checkAll') != -1 )
{
if( boolAllChecked==false)
e.checked= false ;
else
e.checked= true;
break;
}
}
}
</script>

write this within <head></head> and call the function from the html
code and use the html checkbox in the grid and use
onclick="checkchanged" and so on...and you must have the postback
property false of the all checkboxes
 
M

Mark Rae

Yeah Nil....I know this can be done using JavaScript but I did prefer
doing it using ASP.NET.

I'm really interested in why you prefer this, since it involves a completely
unnecessary round-trip to the server and back for absolutely no benefit
whatsoever...
 
R

rn5a

Mark, it's not exactly a question of preference...it's got more to do
it with learning ASP.NET. I know how to do it using JavaScript but am
unable to do the same using ASP.NET. It seemed to me that the problem
would be a petty one (why doesn't the code in post #1 doesn't work
especially the first If condition in the sub named 'CheckAllRows'?) but
I guess I was wrong.

Moreover there are many things in ASP.NET that can easily be done using
JavaScript (or any other client-side script). For e.g. creating
cookies, validating Form fields etc. but I have come across a number of
posts & articles where authors have stressed more on using ASP.NET
rather than JavaScript - one of the most common reasons being what if a
user has disabled JavaScript in his browser though 99.99% don't do it.
Another reason being JavaScript behaves differently in different
browsers. For e.g. a JavaScript code might work in IE but not in, say,
Netscape or FireFox or Mozilla or Opera.

As far as preferring ASP.NET over JavaScript is concerned, using
ASP.NET would save users from an extra mouse click since checking the
CheckBox in the Header would not only check all the CheckBoxes but will
also post the page at the same time to do some work. Using JavaScript
would mean having an additional Button which, in turn, would mean
checking the CheckBox in the Header first to check all the CheckBoxes
residing in the DataGrid & then clicking the Button to do the same
work. & believe me, I have come across numerous articles & books since
my ASP days where authors have preferred one method over the other just
to save users from that extra mouse click!

Lastly, as already said at the beginning of this post, I am in the
learning process & am inquisitive to know why doesn't the code snippet
in post #1 doesn't work although if the CheckBox is moved from the
Header to any row within the DataGrid, it works perfectly. What's wrong
with the first If condition which happens to be

If (dgItems.ItemType = ListItemType.Header)

Why isn't ASP.NET equating the above If condition to True? What makes
ASP.NET equate the above If condition to False when the DataGrid does
have Headers?
 
M

Mark Rae

Mark, it's not exactly a question of preference...it's got more to do
it with learning ASP.NET. I know how to do it using JavaScript but am
unable to do the same using ASP.NET.

You really must take one step back here - ASP.NET is not a replacement for
JavaScript. The main function of ASP.NET is to stream HTML markup to the
client browser, and that includes JavaScript. Of course, ASP.NET *can* do
much more than that because, being a server-side technology, it interfaces
with the .NET Framework and all that comes with that.
Moreover there are many things in ASP.NET that can easily be done using
JavaScript (or any other client-side script). For e.g. creating
cookies, validating Form fields etc.

That's absolutely right. Client-side validation is a good example.
but I have come across a number of
posts & articles where authors have stressed more on using ASP.NET
rather than JavaScript - one of the most common reasons being what if a
user has disabled JavaScript in his browser though 99.99% don't do it.

That's true - some people are still running Windows 98...
Another reason being JavaScript behaves differently in different
browsers. For e.g. a JavaScript code might work in IE but not in, say,
Netscape or FireFox or Mozilla or Opera.

Again, that's also true, but if something works in IE but not in other
browsers, you can pretty much guarantee that it's because IE is not
following the industry standards...
As far as preferring ASP.NET over JavaScript is concerned, using
ASP.NET would save users from an extra mouse click since checking the
CheckBox in the Header would not only check all the CheckBoxes but will
also post the page at the same time to do some work.

Ah, but that's different! You didn't say that :) Clearly, if you *need* a
round-trip to the server, then doing everything server-side ceases to be a
consideration...
Using JavaScript would mean having an additional Button which, in turn,
would mean
checking the CheckBox in the Header first to check all the CheckBoxes
residing in the DataGrid & then clicking the Button to do the same
work.

No it wouldn't - not at all. It's a simple enough thing to cause a piece of
JavaScript to run before a postback - do a Google search for
Attributes.Add("onclick",
 
R

rn5a

Again, that's also true, but if something works in IE but not in other
browsers, you can pretty much guarantee that it's because IE is not
following the industry standards...

Sorry but I contradict you - it is IE alone who is following the
industry standards, the rest aren't......I am a hardcore Microsoft fan
:)

No it wouldn't - not at all. It's a simple enough thing to cause a piece of
JavaScript to run before a postback - do a Google search for
Attributes.Add("onclick",

Yeah....you are right......the Attributes.Add thing didn't come to my
mind....

BTW, any ideas/suggestions/advices/solutions on why doesn't ASP.NET
evaluate the If condition

If (dgItems.ItemType = ListItemType.Header)

to True in the 'CheckAllRows' sub in post #1?

I don' think I am the first ASP.NET programmer in this world to try
this; I am sure somebody definitely must have done it.....I guess my
thread is not catching the right persons' eyes

Someone please HELP.....my receding hairline has already started
receding further just because of this :)
 
R

rn5a

Mark, I guess you will be happy to hear that I have finally solved the
mystery.

Only items bound to the data source are contained in the DataGrid's
Items collection. Header, Footer & Separator are not included in this
collection. This was the reason why

For Each dgItem In DataGrid1.Items
If (dgItem.ItemType = ListItemType.Header)
chkSelHeader = dgItem.FindControl("chkHeader")

was refusing to find the CheckBox named 'chkHeader' in the
HeaderTemplate of the DataGrid (dgItem is of type DataGridItem).
'chkHeader' can be found using

chkSelHeader =
DataGrid1.Controls(0).Controls(0).FindControl("chkHeader")

& to check/uncheck the rest of the CheckBoxes when the CheckBox in the
Header is checked/unchecked respectively, the following code would do
the needful:

Dim dgItem As DataGridItem
Dim chkAllRows As CheckBox

For Each dgItem In DataGrid1.Items
If (dgItem.ItemType = ListItemType.Item Or dgItem.ItemType =
ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
If (chkSelHeader.Checked) Then
chkAllRows.Checked = True
Else
chkAllRows.Checked = False
End If
End If
Next

That's it! Had to struggle a lot to resolve the issue....
 
N

nil

Mark, I guess you will be happy to hear that I have finally solved the
mystery.

Only items bound to the data source are contained in the DataGrid's
Items collection. Header, Footer & Separator are not included in this
collection. This was the reason why

For Each dgItem In DataGrid1.Items
If (dgItem.ItemType = ListItemType.Header)
chkSelHeader = dgItem.FindControl("chkHeader")

was refusing to find the CheckBox named 'chkHeader' in the
HeaderTemplate of the DataGrid (dgItem is of type DataGridItem).
'chkHeader' can be found using

chkSelHeader =
DataGrid1.Controls(0).Controls(0).FindControl("chkHeader")

& to check/uncheck the rest of the CheckBoxes when the CheckBox in the
Header is checked/unchecked respectively, the following code would do
the needful:

Dim dgItem As DataGridItem
Dim chkAllRows As CheckBox

For Each dgItem In DataGrid1.Items
If (dgItem.ItemType = ListItemType.Item Or dgItem.ItemType =
ListItemType.AlternatingItem) Then
chkAllRows = dgItem.FindControl("chkItem")
If (chkSelHeader.Checked) Then
chkAllRows.Checked = True
Else
chkAllRows.Checked = False
End If
End If
Next

That's it! Had to struggle a lot to resolve the issue....


hi..can anyone from you tell me how can i change the backcolor of row
that has checked checkbox using javascript?... and suppose there is
only one textbox in the form and one command button...
can anyone tell me how can i setfocus of textbox when page is load?
should i do that by javascript?plz send me coding how to do that?thanks
a lot...in advance
 
M

Mark Rae

hi..can anyone from you tell me how can i change the backcolor of row
that has checked checkbox using javascript?...

Assuming the checkbox is in a <td> and the <td> is in a <tr>...

if (this.checked)
{
this.parent.parent.bgColor = "red";
}
else
{
this.parent.parent.bgColor = "white";
}
and suppose there is
only one textbox in the form and one command button...
can anyone tell me how can i setfocus of textbox when page is load?

<body onload=document.getElementById('MyTextBox').focus();>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top