在动态 Asp:net Table 上添加必需的验证器

Add Required Validator on Dynamic Asp:net Table

我想在动态 asp:table.

控件(例如文本框)上添加一些必需的验证器

代码:

   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.UnhandledException in System.web.dll"

上线Row.Cells.Add(Cell);

你能帮帮我吗?

我用包含 Texbox 和 RequireValidatorField 的面板解决了这个问题。

    Panel p = new Panel();
    p.Controls.Add(txtBox1);
    p.Controls.Add(req);
    valueCell.Controls.Add(p);