【问题标题】:Select radio buttons according to dropdown list in asp.net根据asp.net中的下拉列表选择单选按钮
【发布时间】:2012-09-11 16:30:59
【问题描述】:

我基本上是在尝试将下拉列表与单选按钮绑定。下拉菜单中有 4 个选项,应根据这些选项选择单选按钮。 对于前两个选项,单选按钮应该是活动的,而对于下拉菜单中的其余两个对象,单选按钮应该是非活动的。

这是我的下拉前端代码:

<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px">
<asp:ListItem Value="Prof">Professional</asp:ListItem>
<asp:ListItem>Enterprise</asp:ListItem>
<asp:ListItem>Maintanence</asp:ListItem>
<asp:ListItem>Reporting</asp:ListItem>
</asp:DropDownList>

这是我的单选按钮代码:

<asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" >
<asp:ListItem Value="1" Text="Yes" />
<asp:ListItem Value="0" Text="No" />                        
</asp:RadioButtonList>

如果我们选择专业,则单选按钮应处于活动状态,并且选中“是”选项。 对于企业,这两个按钮都应该处于活动状态但未选中。 进行维护和报告时,按钮应变为非活动状态。

【问题讨论】:

  • 您可以使用 jquery 轻松实现。
  • 用 asp.net 本身尝试我的新答案。
  • 当有可用的客户端设备时,我们为什么要给服务器负载并让客户端等待响应,只是为了一些 css 属性(启用/禁用),可以通过 jquery 更改/javascript?

标签: c# asp.net drop-down-menu radio-button


【解决方案1】:

首先,您必须将名为 AutoPostBack 的下拉列表的属性设置为 true。您可以通过简单地选择下拉列表并从属性窗口设置 AutoPostBack = true 来做到这一点。 然后去代码后面的文件写这些代码:

 protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        if (ddLType.SelectedValue == "Professional")
        {
            rdoMeapSupport.Enabled = true;
            rdoMeapSupport.SelectedValue = "Yes";
        }
    }


}

在您的单选按钮列表“SelectedIndexChanged”的设置事件之后 并将这段代码粘贴到里面

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddLType.SelectedValue == "Professional")
    {
        rdoMeapSupport.Enabled = true;
        rdoMeapSupport.SelectedValue = "Yes";
    }

    if (ddLType.SelectedValue == "Enterprise")
    {
        rdoMeapSupport.SelectedValue = null;
        rdoMeapSupport.Enabled = true;
    }

    if ((ddLType.SelectedValue == "Maintanence") || (ddLType.SelectedValue == "Reporting"))
    {
        rdoMeapSupport.SelectedValue = null;
        rdoMeapSupport.Enabled = false;
    }

}

【讨论】:

  • 嘿。虽然它第一次完美运行,但第二次它给了我一个错误:'rdoMeapSupport' 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:value 说明:当前web请求执行过程中发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.ArgumentOutOfRangeException:'rdoMeapSupport' 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值
  • 对不起,迟到的评论。我已经弄清楚了你的错误。发生此错误是因为您已分别将值 1 和 0 赋予“是”和“否”(单选按钮的列表项)。也为这些列表项设置值“是”和“否”。
【解决方案2】:

将 AutoPostBack="true" 属性应用于下拉菜单并在选定的索引更改事件中执行以下逻辑。

<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px" 
            onselectedindexchanged="ddLType_SelectedIndexChanged" AutoPostBack="true" >
            <asp:ListItem Value="Prof">Professional</asp:ListItem>
            <asp:ListItem>Enterprise</asp:ListItem>
            <asp:ListItem>Maintanence</asp:ListItem>
            <asp:ListItem>Reporting</asp:ListItem>            
        </asp:DropDownList>
        <asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server"
            AutoPostBack="True">
            <asp:ListItem Value="1" Text="Yes" />
            <asp:ListItem Value="0" Text="No" />
        </asp:RadioButtonList>


protected void ddLType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddLType.SelectedIndex == 0 || ddLType.SelectedIndex == 1)
            {
                rdoMeapSupport.Enabled = true;
            }
            else
            { rdoMeapSupport.Enabled = false; }
        }

【讨论】:

    【解决方案3】:

    我认为激活单选按钮,但不允许选择是没有用的,最好禁用它。
    您可以为此使用 jquery:

    <script>
      $(function () {
        $("# <%# ddLType.ClientID %>").change(function () {
          var selVal = $(this).val();
          if(selVal == "Prof"){
            $('#rb_Statusno').removeAttr('disabled');
          else
            $('#rb_Statusno').attr('disabled', true);
          }  
    </script>
    

    如需更多帮助,您可以通过this 发帖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多