我需要根据用户在单选按钮上的选择来使某些控件可见或不可见,但我需要在用户单击后立即使这些控件可见或不可见单选按钮。
我尝试在ASP端添加OnSelectedIndexChanged,但它似乎没有做任何事情。单击其中一个单选按钮后,还有其他方法可以触发代码吗?
目前我有:
<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>
在代码隐藏中,我有:
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...
}
答案 0 :(得分:3)
您的代码存在的问题是,代码永远不会回发到服务器(原因是像ASP RadioButton
这样的控件,CheckBox
控件会公开缓存事件,这意味着它们只会在实际回发时触发例如通过按钮发生)。只需将AutoPostBack
属性设置为true即可强制回发: -
<asp:RadioButtonList ID="rblMeasurementSystem" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="rblMeasurementSystem_SelectedIndexChanged">
此外,如果您只是根据单选按钮选择显示\隐藏一些控件,则应使用Javascript
或jQuery
在客户端执行此操作。