由于系统限制,我在运行时从代码隐藏向ASP.NET网页添加控件。我能够添加1个而且只能添加1个textboxwatermarkextender,但如果我尝试添加更多内容,则会出现JScript错误:对于控件和行为,值不能为空。参数名称:element。
我已经按照各种方式对代码进行了按摩,但无论我做什么,它都不会让我动态添加更多的textboxwatermark扩展器。以下是代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < fieldCodes.Rows.Count; i++)
{
string title = fieldCodes.Rows[i]["Title"].ToString();
TextBox tb = new TextBox();
tb.ID = string.Format("txt{0}", title);
tb.CssClass = "dynamicTextBox";
tb.MaxLength = 20;
pnlFilter.Controls.Add(tb);
Label lbl = new Label();
lbl.ID = string.Format("lbl{0}", title);
lbl.Text = title;
lbl.CssClass = "dynamicLabel clearLeft";
pnlFilter.Controls.Add(lbl);
TextBoxWatermarkExtender x = new TextBoxWatermarkExtender();
x.ID = "x" + title;
x.TargetControlID = string.Format("txt{0}", title);
x.WatermarkText = "All";
pnlFilter.Controls.Add(x);
}
}