简体   繁体   English

ASP.NET 看不到单选按钮值的变化

[英]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.当一个值被输入到文本框中时,单选按钮通过 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.我将使用“rbSolid”作为我关注的单选按钮。

I handle the 'onclick' event of the radio buttons, but I don't have the function doing anything yet other than firing:我处理单选按钮的“onclick”事件,但除了触发之外我还没有做任何事情:

Me.rbSolid.Attributes.Add("onclick", "styleLookupChanged(this);") 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; 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...如果 Me.rbSolid.Checked 那么...

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.这与 ASP.NET 回发数据的方式有关。 If a control is disabled control.enabled = false when the page is rendered than the values will not be posted back to the server.如果在呈现页面时禁用control.enabled = false ,则值将不会回发到服务器。 How I have solved it in the past is to set the disabled flag using attributes tags instead of using the Enabled property.我过去解决它的方法是使用属性标签而不是使用 Enabled 属性设置禁用标志。 So instead of control.enabled = false , you use control.attributes.add("disabled", "disabled") .因此,您使用control.attributes.add("disabled", "disabled")而不是control.enabled = false

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.并且背景样式有效,但disabled没有。 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.显然禁用是 html 属性而不是 css 属性。 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.如果在 asp.net 中禁用服务器端的单选按钮,然后检查呈现的 html,单选按钮将嵌入 span 标记中,其禁用属性设置为 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您可以尝试改为定位按钮的跨度(父元素,它没有 id)并在您的 javascript 中设置 disabled=false 以查看是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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