【问题标题】:Radio Button Losing Focus After Postback回发后单选按钮失去焦点
【发布时间】:2013-06-05 13:08:13
【问题描述】:

在单选按钮中选择选项后,我在回帖时遇到问题。 选择“是”后,必须回传才能加载下方的文本框;但是,如果选择 no,则文本框不得出现。

我在我的网站 http://www.codeproject.com/Articles/17571/Maintain-focus-between-postbacks-in-ASP-NET-2-0-al 上找到了其他回发问题的解决方案(我在“CurrentControl is”列表中添加了单选按钮),但由于我自己以外的原因,它似乎并不适用到单选按钮。

由于我刚刚创建了我的帐户,我还不能发布图片,但是当页面回传时,焦点会转移到最后一个被关注的文本框或下拉列表。

单选按钮的 Aspx:

<table class="dataentry">
     <tr>
          <td class="column1">
               <asp:Label ID="lblPrevEmployed" runat="server" Text="Have you ever been employed by our company?"meta:resourcekey="lblPrevEmployed"></asp:Label>
          </td>
          <td class="column2">
               <asp:RadioButton ID="rdoPrevEmployed_Yes" runat="server" GroupName="PrevEmployed" AutoPostBack="True" OnCheckedChanged="rdoPrevEmployed_OnCheckedChanged" Text="Yes" meta:resourcekey="rbPrevEmployedYes" /> &nbsp;
               <asp:RadioButton ID="rdoPrevEmployed_No" runat="server" GroupName="PrevEmployed" AutoPostBack="True" OnCheckedChanged="rdoPrevEmployed_OnCheckedChanged" Text="No" meta:resourcekey="rbPrevEmployedNo" />
               <asp:CustomValidator ID="cvPrevEmployed" runat="server" ErrorMessage="You must select either Yes or No." ValidationGroup="Group" OnServerValidate="cvPrevEmployed_ServerValidate" Display="Dynamic" meta:resourcekey="cvPrevEmployed"></asp:CustomValidator>
          </td>
     </tr>
</table>

【问题讨论】:

    标签: c# asp.net focus


    【解决方案1】:

    试试这样的: (我使用了 RadioButtonList 而不是单个 RadioButtons)

    一个 div 用于单选按钮列表,第二个 div 用于显示与所选单选按钮相关的描述。

    还会启用通知标签(默认情况下会禁用)并显示消息。

    [HTML]

    <asp:Label ID="NotifyLabel" runat="server" Text="" ForeColor="Red" Font-Bold="true"></asp:Label>
    <br />
    <div id="SelAccTypeSelect" runat="server" style="float: left; margin-right: 20px;">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
        <asp:ListItem Value="0" Text="Account Type 1" />
        <asp:ListItem Value="1" Text="Account Type 2" />
    </asp:RadioButtonList>
    </div>
    <div id="SelAccTypeDesc" runat="server" style="width: 920px;"></div>
    

    这是一个数组,其中包含与单选按钮的值相对应的可用描述。

    [代码隐藏]

    private void InitializeSelAccTypeDesc()
    {
    SelAcctDescription[0] = "<p>This account type is for users who want to do something like this.</p>";
    SelAcctDescription[1] = "<p>This account type is for other users who want to do something like that.</p>";
    }
    
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    // get the selected account type
    AccTypeByte = Convert.ToByte(RadioButtonList1.SelectedValue.ToString());
    
          // notify the user with the value of the selected radio button
    NotifyLabel.Visible = true;
    NotifyLabel.Text = string.Format("{0} was the value selected.   -   ", AccTypeByte);
    
    // replace the html in the div with the html from the selected array element
    SelAccTypeDesc.InnerHtml = SelAcctDescription[Convert.ToInt32(AccTypeByte)];
    }
    

    *注意:0 可能是“否”,1 可能是“是” 您可以在 NotifyLabel 周围添加 if 语句,仅根据 AccTypeByte 的值显示。

    【讨论】:

    • 有什么方法可以在上下文中查看您的代码吗?我觉得这可能适用于我正在做的事情,但我目前不清楚背后的代码真正为焦点做了什么。感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2012-03-06
    相关资源
    最近更新 更多