【问题标题】:Retrieve value of dynamic radio button controls in asp:table在 asp:table 中检索动态单选按钮控件的值
【发布时间】:2017-04-22 00:10:10
【问题描述】:

我在 asp:table 中动态创建单选按钮。我想获取所选单选按钮的 id。

  1. 如何检查控件类型
  2. 如何获取其 ID。

        RadioButton radioButton = new RadioButton();
        radioButton.ID = id + "RadioButton";
        radioButton.ToolTip = text;
        radioButton.Attributes.Add("id", id);
        radioButton.GroupName = categoryID + "Questions";
        radioButton.EnableViewState = true;
        radioButton.AutoPostBack = true;
        radioButton.Checked = isSelected;
        radioButton.CssClass = style;
    
        TableCell cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Left;
        cell.Controls.Add(radioButton);
        TableRow row = new TableRow();
        row.Cells.Add(cell);
        table.Rows.Add(row);
    

【问题讨论】:

标签: c# asp.net radio-button findcontrol


【解决方案1】:

解决方案。我发布此解决方案是为了帮助其他人。

        RadioButton rb = new RadioButton();            
        foreach (TableRow tr in QuestionTable.Controls)
        {
            foreach (TableCell tc in tr.Controls)
            {
                if (tc.Controls[0] is RadioButton)
                {
                    rb = (RadioButton)tc.Controls[0];
                    if (rb.Checked)
                    {
                        string aa = rb.ID;
                    }
                    break;
                }
            }               
        }

【讨论】:

    【解决方案2】:

    你为什么不使用 jQuery。

    为单选按钮指定一个类

    使用 'each' 函数循环遍历类

    然后在 'each' 函数中,检查 (this) 对象是否被 'selected' 或 'checked' 属性修饰。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2017-11-19
      • 2018-10-14
      相关资源
      最近更新 更多