繁体   English   中英

用户单击单选按钮时触发事件

[英]Firing an event when a user clicks a radio button

我需要根据用户从单选按钮中所做的选择使某些控件可见或不可见,但是我需要在用户单击其中一个单选按钮后立即使这些控件可见或不可见。

我尝试在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...

}

您的代码的问题在于,代码永远不会回发到服务器(原因是ASP RadioButtonCheckBox控件之类的控件公开了缓存的事件,这意味着仅当发生实际的回发(例如通过按钮)时才会触发它们)。 只需将AutoPostBack属性设置为true即可强制回发:-

<asp:RadioButtonList ID="rblMeasurementSystem" runat="server" AutoPostBack="true" 
     OnSelectedIndexChanged="rblMeasurementSystem_SelectedIndexChanged">

此外,如果您只是显示/隐藏基于单选按钮选择的少量控件,则应在客户端使用JavascriptjQuery

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM