Radiobuttonlist onclick event

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'm binding a datasource to a Radiobuttonlist like this:

rbtnLocations.DataSource = MyDataReader
rbtnLocations.DataTextField = "ID"
rbtnLocations.DataValueField = "LocationName"
rbtnLocations.DataBind()

I want to add an onclick event that will acquire the currently selected
radio button.

Something as simple as popping a javascript alert like this:

rbtnLocations.Attributes.Add("onClick", "alert('You selected - '" &
rbtnLocations.ClientID & ".SelectedItem')")

This doesn't work - any ideas anyone?

Thanks, Dan.
 
There is a bug in the framework but not related to this question.

In this case, the onclick event is attached to the RadioButtonList object,
not a child object (ListItem).

When you use Attributes.Add(), it adds the attribute to the outermost HTML
tag for the web control. Look at the HTML generated on the page and you will
see that your radiobuttons are unique elements contained within a wrapper
like a <span> or <table>. Your onclick is assigned to that wrapper. To hook
up this code, write some javascript that assigns to the onclick event as the
page is loaded. Something like this:

Page.RegisterStartupScript("alert", "<script language='javascript'>[code
here]</script>")

[code here is]
var vDone = false;
for (var vI = 0; !vDone; vI++)
{
var vFld = document.getElementById("[RadioButtonList.ClientID]" + "_ "+
vI);
if (vFld != null)
vFld.onclick = "[your code]";
else
vDone = true;
}

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

Chris Jackson said:
That is a bug in the framework.

http://support.microsoft.com/default.aspx?scid=kb;[LN];309338

You'll need to switch to using <input type="Radio"> if you need client side
attributes written.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

Dan said:
I'm binding a datasource to a Radiobuttonlist like this:

rbtnLocations.DataSource = MyDataReader
rbtnLocations.DataTextField = "ID"
rbtnLocations.DataValueField = "LocationName"
rbtnLocations.DataBind()

I want to add an onclick event that will acquire the currently selected
radio button.

Something as simple as popping a javascript alert like this:

rbtnLocations.Attributes.Add("onClick", "alert('You selected - '" &
rbtnLocations.ClientID & ".SelectedItem')")

This doesn't work - any ideas anyone?

Thanks, Dan.
 
Thanks very much for both your responses. It's resolved my issue.

Thanks, Dan

Peter Blum said:
There is a bug in the framework but not related to this question.

In this case, the onclick event is attached to the RadioButtonList object,
not a child object (ListItem).

When you use Attributes.Add(), it adds the attribute to the outermost HTML
tag for the web control. Look at the HTML generated on the page and you will
see that your radiobuttons are unique elements contained within a wrapper
like a <span> or <table>. Your onclick is assigned to that wrapper. To hook
up this code, write some javascript that assigns to the onclick event as the
page is loaded. Something like this:

Page.RegisterStartupScript("alert", "<script language='javascript'>[code
here]</script>")

[code here is]
var vDone = false;
for (var vI = 0; !vDone; vI++)
{
var vFld = document.getElementById("[RadioButtonList.ClientID]" + "_ "+
vI);
if (vFld != null)
vFld.onclick = "[your code]";
else
vDone = true;
}

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

Chris Jackson said:
That is a bug in the framework.

http://support.microsoft.com/default.aspx?scid=kb;[LN];309338

You'll need to switch to using <input type="Radio"> if you need client side
attributes written.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

Dan said:
I'm binding a datasource to a Radiobuttonlist like this:

rbtnLocations.DataSource = MyDataReader
rbtnLocations.DataTextField = "ID"
rbtnLocations.DataValueField = "LocationName"
rbtnLocations.DataBind()

I want to add an onclick event that will acquire the currently selected
radio button.

Something as simple as popping a javascript alert like this:

rbtnLocations.Attributes.Add("onClick", "alert('You selected - '" &
rbtnLocations.ClientID & ".SelectedItem')")

This doesn't work - any ideas anyone?

Thanks, Dan.
 
Back
Top