简体   繁体   中英

ASP.NET not seeing Radio Button value change

I have a form with some radio buttons that are disabled by default.

When a value gets entered into a text box, the radio buttons are enabled via javascript. The user then selects one of the radio buttons and clicks on a submit button which posts back to the server.

When I get back to the server, the radio button that user clicked is not showing as checked. I'll use 'rbSolid' as the radio button I'm focusing on.

I handle the 'onclick' event of the radio buttons, but I don't have the function doing anything yet other than firing:

Me.rbSolid.Attributes.Add("onclick", "styleLookupChanged(this);")

On the client, this enables the radio button when the textbox value is changed:

document.getElementById("ctl00_MainLayoutContent_WebPanel4_rbSolid").disabled = false;

I then click the radio button then post back via a button, but back on the server this is always false:

If Me.rbSolid.Checked Then...

If I have the radio button enabled by default, it shows as checked correctly.

Thanks for any help!

This has to do with how ASP.NET postback data. If a control is disabled control.enabled = false when the page is rendered than the values will not be posted back to the server. How I have solved it in the past is to set the disabled flag using attributes tags instead of using the Enabled property. So instead of control.enabled = false , you use control.attributes.add("disabled", "disabled") .

This isn't working for me.

I added:

Me.rbSolid.Style.Add("background-color", "red")

Me.rbSolid.Style.Add("disabled", "true")

and the background style works but the disabled did not. It's still editable when the form renders.

Here's the source after load:

<span style="font-family:Verdana;font-size:8pt;background-color:red;Disabled:true;">
  <input id="ctl00_MainLayoutContent_WebPanel4_rbSolid" type="radio" name="ctl00$MainLayoutContent$WebPanel4$DetailType" value="rbSolid" onclick="styleLookupChanged(this);"/>
  <label for="ctl00_MainLayoutContent_WebPanel4_rbSolid">Solid</label>
</span>

apparently disabled is an html attribute not a css attribute. if you disable a radio button on the server side in asp.net and then check the rendered html, the radio button is embedded in a span tag with its disabled attribute set to true. You might try targeting the span of the button instead (parent element, it has no id) and setting disabled=false in your javascript to see if that works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM