【发布时间】:2014-07-15 23:59:08
【问题描述】:
我在下拉选择的索引更改事件上生成动态文本框控件。
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Attributes attribute in getAllAttributes(Convert.ToInt32(ddlCategories.SelectedValue)))
{
Panel div = new Panel();
div.Attributes.Add("class", "form-group");
HtmlGenericControl lbl = new HtmlGenericControl("label");
lbl.Attributes.Add("class", "col-lg-2 control-label");
lbl.InnerText = attribute.Name;
Panel innerdiv = new Panel();
innerdiv.Attributes.Add("class", "col-lg-10");
TextBox txt = new TextBox();
txt.ID = attribute.ID.ToString();
txt.Attributes.Add("class", "form-control");
innerdiv.Controls.Add(txt);
div.Controls.Add(lbl);
div.Controls.Add(innerdiv);
CustomAttributes.Controls.Add(div);
}
}
现在,在用户填写表单中的值后,我想获取动态生成的控件的值。但是CustomAttributes.findControls("") 对我不起作用。它总是给 null。
我也试过了
var textBoxesInContainer = CustomAttributes.Controls.OfType<TextBox>();
但它也不起作用。
谁能告诉我这里出了什么问题。
谢谢
【问题讨论】:
-
我添加了标签。谢谢
-
CustomAttributes.Controls.FindControl() 不存在?
-
为什么
FindControl不适合你? -
您需要递归搜索,因为您的 TextBox 是
innerdiv的子代,而不是CustomAttributes直接的子代。 stackoverflow.com/questions/253937/… 或 stackoverflow.com/questions/4955769/…。如前所述,FindControl 应该可以工作,因为您也为 TextBox 提供了 ID -
@SajithA.K.它在那里,我试过了。它返回给我 null
标签: c# asp.net dynamic findcontrol