473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Check One CheckBox To Check All CheckBoxes

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?

Dec 4 '06 #1
10 5164
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!!
rn**@rediffmail.com wrote:
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?
Dec 4 '06 #2
nil

rn**@rediffmail.com wrote:
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!!
rn**@rediffmail.com wrote:
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?
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[i];
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[i];

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[i];

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></headand 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

Dec 4 '06 #3
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.
nil wrote:
rn**@rediffmail.com wrote:
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!!
rn**@rediffmail.com wrote:
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?

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[i];
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[i];

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[i];

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></headand 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
Dec 4 '06 #4
<rn**@rediffmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
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...
Dec 4 '06 #5
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?
Mark Rae wrote:
<rn**@rediffmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
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...
Dec 4 '06 #6
<rn**@rediffmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
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",
Dec 4 '06 #7
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 :-)
Mark Rae wrote:
<rn**@rediffmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
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",
Dec 4 '06 #8
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("chk Header")

& 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....
rn**@rediffmail.com wrote:
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 :-)
Mark Rae wrote:
<rn**@rediffmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
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",
Dec 5 '06 #9
nil

rn**@rediffmail.com wrote:
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("chk Header")

& 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....
rn**@rediffmail.com wrote:
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 :-)
Mark Rae wrote:
<rn**@rediffmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
>
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",

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

Dec 5 '06 #10
"nil" <Ni**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
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 <tdand the <tdis 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( );>
Dec 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: LRW | last post by:
I have no idea if this is more a PHP question or Javascript question, because my problem hinges equally on both. I have a PHP script that queries a database and creates a list of rows for each...
8
by: John Banta | last post by:
Hi, I have created a 12 month calendar where each day has a check box whereby the user can indicate if that day is available or not available for a certain event. The calendar is 'drawn' in a...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
2
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
7
by: Jaime Stuardo | last post by:
Hi all.. I have a DataGrid with checkboxes. In the header I have a "check all" checkbox. I'm wondering if there is an easy way to check all checkboxes using that checkbox. I could do it using...
4
by: chengsi | last post by:
Hi, I have a "continuous" subform which is linked to a table which has a checkbox field. I would like to create a Check All/Uncheck All checkbox control that both checks and disables the...
2
by: dkultasev | last post by:
Hello, I have small script which generates some listboxes. Their names are listXX (list01, list02, list03....). How to check if there are checked or not ? If I have 1 listbox and have it's name I...
1
Frinavale
by: Frinavale | last post by:
I'm working on an ASP.NET application using VB.NET for server side code. I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.