【问题标题】:ASP.Net c# Which radio button in a given GroupName is selected?ASP.Net c# 选择给定 GroupName 中的哪个单选按钮?
【发布时间】:2011-05-21 07:14:37
【问题描述】:

我有 30 个单独的 RadioButton。我不能使用 RadioButtonList。有 3 组按钮。每个组都有一个唯一的 GroupName。一切都在网络浏览器中正常工作。我如何在回帖中知道在每个给定的 GroupsNames 中选择了哪个按钮?

编辑:我使用的功能

private string getRadioValue(ControlCollection clts, string groupName)
{
    string ret = "";
    foreach (Control ctl in clts)
    {
        if (ctl.Controls.Count != 0)
        {
            if (ret == "")
                ret = getRadioValue(ctl.Controls, groupName);
        }

        if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
        {
            RadioButton rb = (RadioButton)ctl;
            if (rb.GroupName == groupName && rb.Checked == true)
                ret = rb.Attributes["Value"];
        }
    }
    return ret;
}

【问题讨论】:

    标签: c# asp.net radio-button


    【解决方案1】:

    您必须检查所有单选按钮的选中属性。
    没有简单的方法可以通过 groupName 进行检查。 (您可以编写方法来扫描某个控件容器中的所有单选按钮并返回对 groupName 的列表,控件已检查,但更容易扫描所有 rb)

    【讨论】:

      【解决方案2】:

      将相同的处理程序附加到每个 RadioButton。然后检查您要查找的属性。将启用回发设置为 true。

      protected void RadioButton1_30_CheckedChanged(object sender, EventArgs e)
      {
          RadioButton rb = (RadioButton)sender;        
          string txtVal = rb.Text;
          string groupName= rb.GroupName;
          int tmpInt;
          Int32.TryParse(txtVal, out tmpInt);
      
      }
      

      【讨论】:

      • 这将需要在用户每次更改收音机时刷新页面。
      【解决方案3】:

      你可以使用 Request.Form("groupName")

      【讨论】:

      • 这不起作用,因为 ASP.NET 将 crud 添加到 name 属性。例如:ctl00$cphBody$Mthree GroupName 是 Mthree,如果我要使用 Request.Form,我需要知道 crud 部分,因为无法访问 GroupName。
      • 好吧,如果您的页面设置方式需要修改,您通常可以使用其中一个控件的 ClientID 属性来找出杂乱无章的部分。
      • ID 和 Name 的 crud 部分是否相同?我没有看到 ClientName 参数。
      • 要么相同,要么有细微差别——比如一个使用 $ 而另一个使用 _ 或 :,因此字符串替换通常可以解决问题。
      猜你喜欢
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      相关资源
      最近更新 更多