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

问题描述 投票:2回答:1

我想在动态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);

你能帮助我吗 ?

c# asp.net dynamic controls requiredfieldvalidator
1个回答
0
投票

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

    Panel p = new Panel();
    p.Controls.Add(txtBox1);
    p.Controls.Add(req);
    valueCell.Controls.Add(p);
© www.soinside.com 2019 - 2024. All rights reserved.