在Dynamic Asp:net Table上添加必需的Validator

时间:2017-12-19 11:26:04

标签: c# asp.net dynamic controls requiredfieldvalidator

我想在动态asp:table中添加一些控件上的必需Validator(例如Textbox)。

代码:

   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);
}

但是我收到了这个错误:

  

&#34; System.web.dll中的System.Web.UnhandledException&#34;

Row.Cells.Add(Cell);

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

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