回传后单选按钮丢失焦点

时间:2013-06-05 13:08:14

标签: c# asp.net focus

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

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

1 个答案:

答案 0 :(得分:0)

尝试这样的事情: (我使用了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>

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

[CODE BEHIND]

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的值显示。