Radiobutton oncheckedchanged event....

M

Mufasa

How can I make an asp:radiobutton where when the user selects it it runs
javascript code? I've tried doing the following but it doesn't seem to work:

arbLogoAttached.Attributes.Add("OnCheckedChanged",
"javascript:RadioClicked();");

But that doesn't seem to work. And it doesn't seem to put the onclick or
oncheckedchanged on the input itself - it's putting it on a span around the
object.

Anybody have any thoughts?

TIA - Jeff.
 
N

Nathan Sokalski

I agree that this is a very frustrating issue, which happens with many
Controls. The only solution I can come up with would be to add JavaScript
that runs when the page loads that adds the eventhandler. This is often a
little more complicated, but the best way to do it would be to first look at
the source in the browser to see what the generated ID's for the input tag
is (this is usually a modification of the RadioButton.ClientId property),
and then write the JavaScript code that adds the eventhandler. You will
probably want to use the Page.ClientScript.RegisterStartupScript method so
that the code is only executed once. I do not know of a better way to do
this, and I think that for controls like the RadioButton there should be a
two sets of Attributes; one for the input and one for the text.
 
M

Mark Rae [MVP]

How can I make an asp:radiobutton where when the user selects it it runs
javascript code? I've tried doing the following but it doesn't seem to
work:

arbLogoAttached.Attributes.Add("OnCheckedChanged",
"javascript:RadioClicked();");

But that doesn't seem to work. And it doesn't seem to put the onclick or
oncheckedchanged on the input itself - it's putting it on a span around
the object.

Anybody have any thoughts?

When you add attributes to controls which will render as client-side HTML,
you can only add those attributes which actually exist for the type of
control to which you're trying to add them. Specifically, in this case, an
<asp:RadioButton ...> webcontrol renders as an <input type="radio".....>
object, which doesn't have an "OnCheckedChanged" method, so trying to wire
one up server-side will do nothing at all, as you've discovered.

Fortunately, what you're looking for is extremely simple:

protected void Page_Load(object sender, EventArgs e)
{
opt1.Attributes.Add("onclick", "radioButtons(this);");
opt2.Attributes.Add("onclick", "radioButtons(this);");
opt3.Attributes.Add("onclick", "radioButtons(this);");
}


<head>
<script type="text/javascript">
var optSelected;
function radioButtons(pobjButton)
{
if (pobjButton.id == optSelected) {return;}
optSelected = pobjButton.id;
//alert('You selected ' + pobjButton.value);
// your code goes here
}
</script>
</head>


<asp:RadioButton ID="opt1" runat="server" Text="Option 1"
GroupName="grpTest" /><br />
<asp:RadioButton ID="opt2" runat="server" Text="Option 2"
GroupName="grpTest" /><br />
<asp:RadioButton ID="opt3" runat="server" Text="Option 3"
GroupName="grpTest" />
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top