Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript 'onClick' event does not fire... 1

Status
Not open for further replies.

JCruz063

Programmer
Feb 21, 2003
716
US
Hi All,
I have an ASP.NET CheckBoxList control with only one checkbox in it. I need a javascript function of mine to run when the control is clicked, so I did the following:

[tt]...Attributes.Add("onclick", "myFunc();");[/tt]

The above works great, but it has one major flaw. The 'click' event is only fired when the user clicks on the checkbox itself. If the user clicks on the label that is next to the checkbox, the checkbox is checked/unchecked but the click event does not fire. Any ideas?

I'm using .NET 2002 with C# and IE 6.0.

Thanks.

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Can't you also use the "...Attributes.Add("onclick", "myFunc();");" with the label?


--
Not until I became a Network Administrator did the error message "See your Network Administrator for assistance" become petrifying.
 
Mike555,
Thanks for your reply.

There is no label. This is an ASP.NET CheckBoxList control. I'm adding the "onclick" to the whole control so clicking anywhere on it should fire the javascript code.

It looks like this:

[tt]myCheckBoxList.Attributes.Add...[/tt]

Something else I could do is add the "onclick" to the actual check box item in the checkboxlist, like this:

[tt]myCheckBoxList.Items[0].Attributes.Add...[/tt]

However, if I do it this way, the onclick is never fired.

Any other ideas?

Thanks.

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Couple of questions.

Have you checked the javascript onclick event is on the rendered page each time before you test it's firing or not?

Have you checked the function is available to the page?

Have you checked if the function is actually being fired but not doing what you expect?

I'm only asking as I have added a javascript onclick event to a checkbox in code behind on the same platform as you and have had no issues.

Rhys

""Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

My Home
 
Rhys,
Thanks for your reply.

I don't have a check box control. I have a CheckBoxList. Try this:

1 - Add a CheckBoxList control to your page.
2 - Add one item to its Items collection.
3 - Assign a javascript function to the "onclick" attribute of the CheckBoxList control.
4 - When the page is displayed, click on the label next to the checkbox.

You will notice that the javascript function does not run because no event is fired. However, the checkbox is still checked/unchecked. Now, how annoying is that?

If you click on the checkbox itself (not the label), you will notice that the event is fired and your javascript function runs fine.

JC


_________________________________________________
To get the best response to a question, read faq222-2244.
 
Had a quick five second check. When you add attributes to the CheckBoxList you're actually adding attributes to the rendered HtmlTable control that contains all of the checkboxes - if you look at your rendered Html you will see this.

To get around it all you need to do is, after you've added the list items to the CheckBoxList, iterate the child controls of the CheckBoxList, identify the Checkboxes and add the onclick attribute to each one as below;
Code:
			foreach(Control lControl in lCheckBoxList.Controls)
			{
				if (lControl is CheckBox)
				{
					((CheckBox)lControl).Attributes.Add("onClick","Javascript:alert('Testing!');");
				}
			}

When it comes to adding javascript as an attribute to controls it's always worth checking the rendered Html page to make sure the javascript function is wired up to the right rendered control.

Rhys

""Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

My Home
 
Rhys thanks again...

It works like a champ!

I had tried the following before:

[tt]MyCheckBoxList.Items[0].Attributes...[/tt]

That, however didn't work because it didn't render correctly on the page. I have read from some google articles that this is perhaps due to a bug in ASP.NET.

Your code does it, though. Thanks a lot!

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Just as a thought, I reckon MyCheckBoxList.Items[0].Attributes.Add won't work by design not a bug. The CheckBoxList Items collection contains a collection of ListItems, not checkboxes and you can't really add a client side attribute to a ListItem as a ListItem isn't rendered in the Html.

Just thought I'd mention it in case someone else finds it useful

Cheers :)

Rhys

""Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

My Home
 
Rhys666,
Does this also apply to RadioButtonList's?

Tx
maboo59
 
I believe it does. The Items collection is the collection of ListItems(Text - Value pairs), the Controls collection contains the child controls, (the RadioButton controls) rendered for each ListItem in the Items collection).

Rhys

""Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

My Home
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top