【问题标题】:Removing validators of dynamically added controls删除动态添加控件的验证器
【发布时间】:2016-08-01 09:26:57
【问题描述】:

您好,我遇到了这个问题。

我的表格在第一个单元格中填充了动态生成的控件 (TextBox),在第二个单元格中填充了按钮 (MyCustomButton),在最后一个单元格中填充了验证控件 (RegularExpressionValidator)。验证控制器检查 TextBox 中的数据是否正确。按钮的作用是删除包含该按钮、文本框和验证器的行。

我的问题是当我单击该按钮删除它属于异常的行时会弹出说“无法找到由'ControlToValidate'引用的控件ID'MyTextBoxId'”。。 p>

这里的问题是验证器找不到要验证的文本框,因为它已被删除并且弹出此异常。我尝试首先删除此验证器,然后在该行的其余部分之后,清除 TextBox 中的不正确数据,关闭该 TextBox 的验证,但即使删除了验证器,我仍然会收到此异常。

从表中删除方法

    protected void DeleteMemberRow_Click(object sender, EventArgs e)
    {
        //Find row to remove
        TableRow row = (TableRow)((MyCustomButton)sender).Parent.Parent;

        //Custom list of controls - works fine
        ControlsList.RemoveAll(x => x.id == row.ID.Replace("row", ""));

        //MyTable is basic Table type
        MyTable.Rows.Remove(row);             
    }

添加表格行

        Guid guid = Guid.NewGuid();

        TextBox txt = new TextBox();
        MyCustomButton btn = new MyCustomButton();
        btn.Click += new System.EventHandler(DeleteMemberRow_Click);
        btn.ID = "TeamMember" + guid + "btn";
        txt.ID = "TeamMember" + guid;
        RegularExpressionValidator validate = new RegularExpressionValidator();
        validate.ValidationExpression = @"(\d{5}, ?)*\d{5}";
        validate.ErrorMessage = "My error message";
        validate.Attributes.Add("runat", "server");
        validate.ControlToValidate = "TeamMember" + guid;
        validate.Attributes.Add("Display", "none");

        TableRow tRow = new TableRow();
        tRow.ID = "Teammember" + guid + "row";
        TableCell tCell2 = new TableCell();
        TableCell tCell = new TableCell();
        TableCell tCell1 = new TableCell();

        tCell2.Controls.Add(validate);
        tCell1.Controls.Add(btn);
        tCell.Controls.Add(txt);
        tRow.Cells.Add(tCell);
        tRow.Cells.Add(tCell1);
        tRow.Cells.Add(tCell2);

        MyTable.Rows.Add(tRow);

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你能告诉我你的页面加载事件吗?

标签: c# asp.net .net validation webforms


【解决方案1】:

好的,它已修复,代码没有问题,但对我来说。我没有注意到我有另一个验证器检查同一个 TextBox,所以在我删除它之后另一个验证器找不到要验证的 TextBox。

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 2019-11-24
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多