简体   繁体   English

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

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

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. 我尝试在ASP端添加OnSelectedIndexChanged,但似乎没有任何作用。 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...

}

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

<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 . 此外,如果您只是显示/隐藏基于单选按钮选择的少量控件,则应在客户端使用JavascriptjQuery

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

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