psychicspies
psychicspies

Reputation: 31

Can someone help me understand why my TextBox's placeholder doesn't change when RadioButton selected?

My HTML source code for my RadioButtonList :

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

My C# code for my RadioButtonList SelectedIndexChanged event:

 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  *");
    }

}

What I am trying to do, is eg. If the Employee RadioButton is selected, TxtUsername's placeholder will be "Your Employee's Username" and the TxtPassword's placeholder will be "Your Employee's Password".

My two TextBoxes:

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

Upvotes: 1

Views: 124

Answers (2)

hardkoded
hardkoded

Reputation: 21597

To get that change "immediately" you need to enable AutoPostback, and also declare the OnSelectedIndexChanged even. With that set, you will get a postback when a radio button is clicked and SelectedIndexChanged will be triggered.

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

Upvotes: 1

Shadow Walker
Shadow Walker

Reputation: 206

Replace your radio button with the following snippet

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

your are missing

OnSelectedIndexChanged="rbMemberType_SelectedIndexChanged" AutoPostBack="true"

Upvotes: 0

Related Questions