单选按钮在回发后失去焦点

本文关键字:失去 焦点 单选按钮 | 更新日期: 2023-09-27 18:17:07

在单选按钮中选择一个选项后,我遇到了一个问题。选择"yes"后,它必须返回以加载下面的文本框;但是,如果选择no,则文本框不能出现。

我已经找到了一个解决方案,通过我的网站http://www.codeproject.com/Articles/17571/Maintain-focus-between-postbacks-in-ASP-NET-2-0-al(我添加了单选按钮的"CurrentControl is"的列表),但由于一个原因超出了我自己,它似乎并不适用于单选按钮。

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

Aspx for Radio Button:

<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>

单选按钮在回发后失去焦点

试试这样做:(我已经使用了一个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的值显示。