【问题标题】:Add Required Validator on Dynamic Asp:net Table在动态 Asp:net 表上添加必需的验证器
【发布时间】:2018-06-01 19:54:10
【问题描述】:

我想在动态 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.dll 中的 System.Web.UnhandledException”

上线Row.Cells.Add(Cell);

你能帮帮我吗?

【问题讨论】:

  • 请提供异常的详细信息 - 至少是消息。
  • 如何获得有关此异常的一些详细信息?因为 VS 没有打开标准的异常窗口。
  • 作为一种快速解决方案,您可以将代码包装在 try / catch 块中,并在 catch 块中放置断点。
  • 哪一行抛出异常?
  • System.Web.HttpUnhandledException 发生消息:抛出异常:System.Web.dll 中的“System.Web.HttpUnhandledException” 表达式计算器中的内部错误。此行导致错误:Row.Cells.Add(Cell);

标签: c# asp.net dynamic controls requiredfieldvalidator


【解决方案1】:

我使用包含 Texbox 和 RequireValidatorField 的面板来解决问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2017-09-22
    • 2018-08-11
    • 2012-09-29
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多