views:

67

answers:

2

I have a RadioButtonList which has AutoPostBack set to True and is being handled server-side:

<asp:radiobuttonlist ID="myRBL" OnSelectedIndexChanged="MyRBL_SelectedIndexChanged"
    RepeatDirection="Horizontal" AutoPostBack="True" runat="server">
    <asp:ListItem Selected="True">Choice 1</asp:ListItem>
    <asp:ListItem>Choice 2</asp:ListItem>
</asp:radiobuttonlist>

I want to "progressively enhance" this by adding a JavaScript function with the OnClick attribute. After adding OnClick="myFunction();", the serverside code is no longer called when JavaScript is disabled in the browser.

Has anyone got a way around this (do I have to implement the IPostBackEventHandler or am I missing something)?

UPDATE: 08-26-2010 4:31 p.m. Pacific

Apparently, the ASP.NET this control cannot submit with JavaScript disabled. Does anyone have a detailed solution to this?

+1  A: 

The only way to check if the user has javascript enabled is from the client side.

Your best bet then is to add the function on the client side, rather than in the server code. jQuery handles this quite well, with something like:

$(function() {
  $('input[type=radio]').click(myFunction);
});

If you're not using jquery you can do a document.getElementById() and whatnot to assign the click handlers.

This way if the client doesn't have javascript, the radiobuttons are completely untouched.

womp
If the user had JavaScript disabled, then this won't work?
IrishChieftain
@IrishChieftain - perhaps I misunderstood. Isn't that the point? That you don't want the onClick enhancement if javascript is disabled?
womp
I see what you mean... wire it up in the body onload?
IrishChieftain
@IrishChieftain - Yeah, or even better just add it in an external script. If the user doesn't have javascript enabled the script won't even be loaded.
womp
Marked as answer plus up vote, thanks :-)
IrishChieftain
Sorry womp. unmarked as answer since basic problem remains - cannot do a postback from this control when JS is disabled. Any ideas?
IrishChieftain
The onclick handler which is set by ASP.NET to do the automatic postback is replaced if you add an onclick handler yourself. In pure javascript it's quite cumbersome to add (instead of replace) an event handler. Read my answer again, I tried really hard to describe it there ;-)
Dave
+1  A: 

AutoPostBack uses JavaScript to post back after clicking a radio button. By setting the onclick attribute, this behaviour is overwritten, so you must add an additional event handler to the click event.

As far as I know, there's no simple way to do this in plain JavaScript, so I would recommend using jQuery:

$('#radioButtonId').click(myFunction);
Dave
Unfortunately, I cannot use JQuery here...
IrishChieftain
I'm sure, there are other ways to add multiple event handlers to the click event. Searching Google for [javascript add multiple event handlers](http://www.google.com/search?q=javascript+add+multiple+event+handlers) presents a bunch of articles about different ways to do this.
Dave
Yes but I'm wary of the browser differences and am a total JS newbie. Thanks for the link, +1 :-)
IrishChieftain
@Dave, I'm marking this as the answer because it directly addresses the core problem I'm facing.
IrishChieftain