有人可以帮助我了解为什么选择RadioButton时我的TextBox占位符没有改变吗?

时间:2018-12-21 13:37:23

标签: c# html asp.net

我的RadioButtonList的HTML源代码:

<asp:RadioButtonList id="rbMemberType" runat="server">
    <asp:ListItem Value="Customer" Selected >Customer</asp:ListItem> 
    <asp:ListItem Value="Employee">Employee</asp:ListItem> 
    <asp:ListItem Value="Supplier">Supplier</asp:ListItem>
    <asp:ListItem Value="Administrator">Administrator</asp:ListItem> 
</asp:RadioButtonList>

我的RadioButtonList SelectedIndexChanged事件的C#代码:

 protected void rbMemberType_SelectedIndexChanged(object sender, EventArgs e)
 {
    if (rbMemberType.SelectedValue == "Customer")
    {
        txtUsername.Attributes.Add("placeholder", "Your Customer's Username  *");
        txtPassword.Attributes.Add("placeholder", "Your Customer's Password  *");
    }
    else if (rbMemberType.SelectedValue == "Employee")
    {
        txtUsername.Attributes.Add("placeholder", "Your Employee's Username  *");
        txtPassword.Attributes.Add("placeholder", "Your Employee's Password  *");
    }
    else if (rbMemberType.SelectedValue == "Supplier")
    {
        txtUsername.Attributes.Add("placeholder", "Your Supplier's Username  *");
        txtPassword.Attributes.Add("placeholder", "Your Supplier's Password  *");
    }
    else
    {
        txtUsername.Attributes.Add("placeholder", "Your Administrator's Username  *");
        txtPassword.Attributes.Add("placeholder", "Your Administrator's Password  *");
    }

}

我想做的是例如。如果选择了“雇员”单选按钮,则TxtUsername的占位符将为“您的雇员的用户名”,而TxtPassword的占位符将为“您的雇员的密码”。

我的两个文本框:

<div class="form-group">
    <asp:TextBox class="form-control" runat="server" id="txtUsername"/>
</div>
<div class="form-group">
    <asp:TextBox class="form-control"  runat="server" id="txtPassword" 
    autocomplete="new-password"/> 
</div>

2 个答案:

答案 0 :(得分:1)

要“立即”进行更改,您需要启用AutoPostback,并同时声明OnSelectedIndexChanged。使用该设置,单击单选按钮并触发SelectedIndexChanged时,您将获得回发。

<asp:RadioButtonList 
    id="rbMemberType" 
    runat="server" 
    OnSelectedIndexChanged="rbMemberType_SelectedIndexChanged" 
    AutoPostback="true">

答案 1 :(得分:0)

用以下代码段替换单选按钮

 <asp:RadioButtonList ID="rbMemberType" runat="server" OnSelectedIndexChanged="rbMemberType_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem Value="Customer" Selected>Customer</asp:ListItem>
                <asp:ListItem Value="Employee">Employee</asp:ListItem>
                <asp:ListItem Value="Supplier">Supplier</asp:ListItem>
                <asp:ListItem Value="Administrator">Administrator</asp:ListItem>
            </asp:RadioButtonList>

您丢失了

OnSelectedIndexChanged="rbMemberType_SelectedIndexChanged" AutoPostBack="true"