473,382 Members | 1,420 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,382 software developers and data experts.

Radiobutton oncheckedchanged event....

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.
Oct 26 '07 #1
2 12950
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Mufasa" <jb@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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.


Oct 27 '07 #2
"Mufasa" <jb@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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" />
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 27 '07 #3

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

Similar topics

0
by: rodrigo | last post by:
I have a Asp.net table control that I dynamically add rows from a SQL database. Inside the table, I add radiobuttons and they all have different ID numbers according to BindChain() Looking In...
2
by: Homa | last post by:
Hi, I have a Datagrid that uses as a shopping cart and have a checkbox template column in it. I can't add both OnCheckedChanged Event and onclick client script for it. I want to do two...
3
by: dave | last post by:
I have half a dozen web form radio buttons on a web form. Each of them is set to postback=true. However, if for instance radiobutton1 is already selected and the user selects it again, it performs...
0
by: Alan Samet | last post by:
If you need to get in touch with me, be sure to remove the "newsgroups" from my email address. I've developed a quick-and-dirty workaround for putting System.Web.UI.WebControls.RadioButton...
5
by: tshad | last post by:
Is there a way to allow a user to press a radio button if it is already selected? There is an onCheckedChanged event that fires when the person presses the button and it is isn't selected...
1
by: Neo | last post by:
Hello All, This one has me stumped, but before going to write a code, i want to be sure of the solution. I have a asp:radiobutton group on a page, and on click of any of the radiobutton, i...
3
by: LCAdeveloper | last post by:
Help! A trawl through the archives couldn't shed any light on this, so is there a way to handle DoubleClick events for RadioButtons in vb.NET? I'm recoding a VB4 application, which used the...
0
by: Dan Beanweed | last post by:
Greetings all. I have looked far and wide but found nothing to help me with this. My datagrid has a hidden column with an id number bound to it. Then there is a column of radio buttons. (Then more...
0
by: Emma Middlebrook | last post by:
Hi there, I'm having problems in an ASP.NET page that is dynamically creating and displaying radio buttons. I have setup a checkchanged event and specified AutoPostBack to true. When the page...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.