ASP.NET按部分ID查找控件

时间:2015-03-15 13:40:52

标签: c# html asp.net webforms

我的html中有这个div

<div class="myValue" runat="server" id="myValueDiv">
            </div>

我将我的单选按钮动态添加到该div ,如下所示:

HtmlGenericControl div = null;
foreach (DataRow row in results.Rows)
            {
numberOfReasons++;

                if ((numberOfReasons % 4) == 1)
                {
                    div = new HtmlGenericControl("div");
                    div.Attributes.Add("class", "oneLine");
                }
                RadioButton radioButton = new RadioButton();
                radioButton.ID = "reason_" + row["reasonName"].ToString();
                radioButton.GroupName = "reason";
                radioButton.Text = row["reasonName"].ToString();

div.Controls.Add(radioButton);
                if (numberOfReasons % 4 == 0)
                {
                    myValueDiv.Controls.Add(div);
                }
                else if (numberOfReasons == results.Rows.Count)
                {
                    myValueDiv.Controls.Add(div);
                }
             }   

生成的HTML是:

<div id="myValueDiv" class="myValue">
            <div class="oneLine"><input id="reason_romaroma2" type="radio" name="reason" value="reason_romaroma2"><label for="reason_romaroma2">romaroma2</label><input id="reason_ss" type="radio" name="reason" value="reason_ss"><label for="reason_ss">ss</label><input id="reason_superroma" type="radio" name="reason" value="reason_superroma"><label for="reason_superroma">superroma</label><input id="reason_asdfasdf" type="radio" name="reason" value="reason_asdfasdf"><label for="reason_asdfasdf">asdfasdf</label></div><div class="oneLine"><input id="reason_asdfa" type="radio" name="reason" value="reason_asdfa"><label for="reason_asdfa">asdfa</label><input id="reason_weqe" type="radio" name="reason" value="reason_weqe"><label for="reason_weqe">weqe</label></div></div>

当我点击一个按钮时,我想知道用户是否选择了任何按钮,如果是,该按钮的文本是什么

我尝试了什么:

var checkedRadioButtons = myValueDiv.Controls.OfType<RadioButton>().Where(radButton=> radButton.Checked).ToList();
            if (checkedRadioButtons.Count>0)

但计数的结果始终为zero

我想念的是什么?

注意:

所有单选按钮的ID都以:

开头

原因_

2 个答案:

答案 0 :(得分:0)

首先,您应该在init事件中动态添加控件

您可以使用此方法查找并获取容器控件中的复选框列表,您可以对其进行编辑以获取任何类型的控件

private List<CheckBox> FindCheckControls<T>(Control control, bool recurse)
    {
        List<CheckBox> found = new List<CheckBox>();
        Action<Control> search = null;
        search = ctrl =>
        {
            foreach (Control child in ctrl.Controls)
            {
                if (typeof(CheckBox).IsAssignableFrom(child.GetType()))
                {
                    found.Add((CheckBox)child);
                }
                if (recurse)
                {
                    search(child);
                }
            }
        };
        search(control);
        return found;
    }

答案 1 :(得分:0)

尝试使用以下代码获取所选单选按钮的 TEXT

var divcontrols = myValueDiv.Controls.OfType<HtmlGenericControl>();
    foreach(HtmlGenericControl loHTML in divcontrols)
    {
        var checkedRadioButtons = loHTML.Controls.OfType<RadioButton>().Where(radButton => radButton.Checked).ToList();
        foreach (RadioButton lobtn in checkedRadioButtons)
        {
            Response.Write(lobtn.Text);
        }
    }