我想在动态asp:table中添加一些控件上的必需Validator(文本框作为例子)。
码:
foreach (KeyValuePair<string, string> o in Collection)
{
TableRow Row = new TableRow();
TableCell valueCell = new TableCell();
TableCell Cell = new TableCell();
TextBox txtBox = new TextBox();
TextBox txtBox1 = new TextBox();
txtBox1.ID = o.Key + o.Value;
if (o.Value.Contains("Mandatory"))
{
RequiredFieldValidator req = new RequiredFieldValidator();
req.ErrorMessage = "Required";
req.BorderColor = System.Drawing.Color.Red;
req.ControlToValidate = txtBox1.ID;
req.Enabled = true;
req.Display = ValidatorDisplay.Dynamic;
Cell.Controls.Add(req);
}
valueCell.Controls.Add(txtBox1);
Row.Cells.Add(valueCell);
Row.Cells.Add(Cell);
table.Rows.Add(Row);
}
但我得到这个错误:
“System.web.dll中的System.Web.UnhandledException”
在线Row.Cells.Add(Cell);
你能帮助我吗 ?
我用包含Text Box和RequireValidatorField的Panel解决了这个问题。
Panel p = new Panel();
p.Controls.Add(txtBox1);
p.Controls.Add(req);
valueCell.Controls.Add(p);