Reputation: 8402
I need to make certain controls visible or invisible based on a user's choice from a radio button, but I need to make those controls visible or invisible as soon as the user clicks one of the radio buttons.
I tried adding OnSelectedIndexChanged on the ASP side, but it doesn't seem to do anything. Is there some other way to trigger the code as soon as you click one of the radio buttons?
At the moment I have:
<asp:RadioButtonList ID="rblMeasurementSystem" runat="server" OnSelectedIndexChanged="rblMeasurementSystem_SelectedIndexChanged">
<asp:ListItem Text="Leads" Value="1" />
<asp:ListItem Text="Audit Thresholds" Value="2" />
<asp:ListItem Text="Root Cause" Value="3" />
<asp:ListItem Text="Pend Reasons" Value="4" />
<asp:ListItem Text="AA Review" Value="5" />
</asp:RadioButtonList>
And in the code-behind I have:
protected void rblMeasurementSystem_SelectedIndexChanged(object sender, EventArgs e)
{
string value = rblMeasurementSystem.SelectedItem.Value.ToString();
if (value == "1")
{
lblLabel1.Visible = true;
txtText1.Visible = true;
lblLabel2.Visible = false;
txtText2.Visible = false;
lblLabel3.Visible = false;
txtText3.Visible = false;
lblLabel4.Visible = false;
txtText4.Visible = false;
lblLabel5.Visible = false;
txtText5.Visible = false;
}
if (value == "2")
{
lblLabel1.Visible = false;
txtText1.Visible = false;
lblLabel2.Visible = true;
txtText2.Visible = true;
lblLabel3.Visible = false;
txtText3.Visible = false;
lblLabel4.Visible = false;
txtText4.Visible = false;
lblLabel5.Visible = false;
txtText5.Visible = false;
}
if (value == "3")
{
lblLabel1.Visible = false;
txtText1.Visible = false;
lblLabel2.Visible = false;
txtText2.Visible = false;
lblLabel3.Visible = true;
txtText3.Visible = true;
lblLabel4.Visible = false;
txtText4.Visible = false;
lblLabel5.Visible = false;
txtText5.Visible = false;
}
etc...
}
Upvotes: 0
Views: 1112
Reputation: 21795
The problem with your code is that, the code is never posting back to the server (Reason being controls like ASP RadioButton
,CheckBox
controls expose cached events meaning they will be triggered only when actual postback happens for example by a button). Simply set the AutoPostBack
property to true to force postback:-
<asp:RadioButtonList ID="rblMeasurementSystem" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="rblMeasurementSystem_SelectedIndexChanged">
Also, if you are just showing\ hiding few controls based on radio button selection you should do this at client side using Javascript
or jQuery
.
Upvotes: 3