简体   繁体   English

当 Checked="true" 时未选择 asp RadioButton

[英]asp RadioButton not selected when Checked="true"

I have two radio buttons "Yes" and "No" and I want the "No" radio button to be selected by default.我有两个单选按钮“是”和“否”,我希望默认选择“否”单选按钮。 However, I can't get it be be selected through the asp code or by the code behind.但是,我无法通过 asp 代码或后面的代码来选择它。 Am I doing this completely wrong or did I just miss something.我这样做是完全错误的还是我只是错过了什么。

ASP code: ASP代码:

<div class="query_header" runat="server" id="ScanOnStartup">
    <div class="formlabel" style="width: 300px">Perform Scan on startup</div>
    <div class="formfield" style="line-height: 10px">
        <asp:RadioButton ID="scanOnStartupYes" Text="Yes" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false"  Checked="false"/>&nbsp
        <asp:RadioButton ID="sanOnStartupNo" Text="No" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false"  Checked="true"/>
    </div>
    <div class="formdivider"></div>
</div>

Code behind:代码背后:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {               
                this.scanOnStartupNo.Checked = true;

        }

If you need to make a radio button checked by default on page load , you can do one of the above如果您需要在页面加载时默认选中单选按钮,您可以执行上述操作之一

From the front end , you can add the property从前端,您可以添加属性

Checked="checked"

From the code behind, you can write it as从后面的代码,你可以把它写成

scanOnStartupNo.Checked=true

When I checked your code , You have made a spelling mistake in declaring the ID of NO button.当我检查您的代码时,您在声明 NO 按钮的 ID 时犯了拼写错误。 That was why the code behind code piece was not working这就是代码片段背后的代码不起作用的原因

从第一个 RadioButton 中删除 Checked="False"(是)

Okay aside from the typo "sanOnStartupNo", I know where the problem is coming from.好的,除了拼写错误“sanOnStartupNo”之外,我知道问题出在哪里。 There was actually another control from my code behind that renders the page and that is where the selection for the radio button is being set.实际上,我的代码背后还有另一个控件来呈现页面,这就是设置单选按钮选择的地方。 I added in code for my radio button and it works now.我为我的单选按钮添加了代码,现在可以使用了。

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

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