0

I have an ASP.NET app and I am using jQuery to toggle the enablement of some fields. Here's a rough diagram of the UI:

__ checkbox1
o radio1
o radio2
text1
text2

When checkbox1 is checked, I enable radio1 and radio2. When radio2 is selected, I enable text1 and text2.

What is happening in my case is after the user checks the checkbox, and the radio buttons are enabled, if the user selects radio2 and submits the form, the selection automatically goes back to radio1.

This problem does not occur if I don't dynamically enable the radio buttons using jQuery. So I am wondering if something with how the radio buttons are enabled doesn't play nicely with how ASP.NET works with radio buttons.

When the checkbox value changes, I call the following helper method in my code. Since ASP.NET will add a around a disabled radio button, I make sure to turn off the disabled flag on both the span and the input.

function setRadioEnabled(id, enabled) {
    var radio = jQuery(id);
    var parentSpan = radio.parent();
    if (enabled) {
        parentSpan.removeAttr('disabled');
        radio.removeAttr('disabled');
    } else {
        radio.attr('disabled', 'disabled');
        parentSpan.attr('disabled', 'disabled');
    }
}

The radio buttons are defined as ASP:RadioButton tags and they share a GroupName even though they are in different table rows. This results in two input type=radio instances with a matching name value.

I confirmed that my radio was being reset as the page was submitted by putting a breakpoint in the postback method and seeing the value of radio1.Checked was true and radio2.Checked was false. What could be causing the page to switch the value of my radio buttons?

Thanks in advance, David

David Whiteman
  • 110
  • 1
  • 10
  • David, I think you're running into the problem that the "Enabled" property of the radio button is part of the viewstate, so just removing the attribute doesn't actually enable the radio button in code behind. Here is a similar post with a solution you could try: http://stackoverflow.com/questions/6995738/asp-javascript-radiobutton-enable-disable-not-included-in-postback-ajax. If that doesn't work, please post more of your code, specifically any other JavaScript involved and your code behind and I can look closer at it. – Jeremy Jul 07 '13 at 02:13
  • Thanks for your post, Jeremy! I think I have fixed the problem by removing the code from the code behind that initializes the enabled state of the controls (except for the main checkbox). It seems this is another case where ASP.NET's way of setting the Enabled state of a control does not mesh with how it is done in Javascript. And the server side initialization turned out to be redundant anyway since I was already computing the enablement on the client. – David Whiteman Jul 07 '13 at 02:54
  • Sounds like a great solution! – Jeremy Jul 07 '13 at 04:42

0 Answers0