单击文本框,它应该选择c#中的单选按钮

时间:2018-01-09 08:33:46

标签: c# radio-button

我有一个Radiobuttonlist。在我添加的项目中使用一个文本框指定您自己的值。我点击该文本框单选按钮应该选择默认值。

 <td style="text-align:left" class="contract_value_bg" width="50%">
            <asp:RadioButtonList ID="rblDocumentstType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblDocumentstType_SelectedIndexChanged" RepeatColumns="1">

                 </asp:RadioButtonList> 
             <asp:TextBox ID="txtRFP" runat="server"  AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120"  />
            </td>

enter image description here

1 个答案:

答案 0 :(得分:0)

如果在RadioButtonList中点击了“指定你自己的值”,你想在文本框上设置焦点吗?或者,如果用户点击了RadioButtonList,您想在TextBox中选择该项?

这是针对前一种情况:

protected void rblDocumentstType_SelectedIndexChanged(Object sender, EventArgs e)
{
    RadioButtonList rblDocumentstType = (RadioButtonList) sender;
    if(rblDocumentstType.SelectedIndex == 1)
    {
        txtRFP.Focus();
    }
}

如果要在RadioButtonList获得焦点时选择第二个TextBox项,则应通过使用javascript(或jQuery)处理onfocus事件来在客户端执行此操作:< / p>

<asp:TextBox ID="txtRFP" runat="server" onfocus="selectSpecifyYourOwn()"  AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120"  />

function selectSpecifyYourOwn() {
    var rbID = '<%=rblDocumentstType.ClientID %>';
    var rb = document.getElementById(rbID);
    var items = rb.getElementsByTagName("input");
    items[1].checked = true;      
}