Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Radiobutton oncheckedchanged event....

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mufasa - 26 Oct 2007 21:27 GMT
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.
Nathan Sokalski - 27 Oct 2007 04:03 GMT
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.
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

> 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
[quoted text clipped - 10 lines]
>
> TIA - Jeff.
Mark Rae [MVP] - 27 Oct 2007 12:57 GMT
> 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
[quoted text clipped - 8 lines]
>
> 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" />

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.