[英]Removing validators of dynamically added controls
您好,我遇到了这个问题。
我的表中的行充满了动态生成的控件(TextBox)在第一个单元格中,按钮(MyCustomButton)在第二个单元格中,而验证控件在最后一个单元格(RegularExpressionValidator)中。 验证控制器检查TextBox中的数据是否正确。 该按钮的功能是删除包含该按钮,文本框和验证器的行。
我的问题是,当我单击该按钮删除属于异常的行时,将弹出说“ 无法找到由'ControlToValidate'引用的控件ID'MyTextBoxId' ”。
这里的问题是验证器找不到TextBox进行验证,因为它已被删除并且此异常弹出。 我尝试先删除此验证器,并在该行的其余部分之后,清除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);
任何帮助,将不胜感激。 谢谢
好的,它解决了代码问题,但我却没有。 我没有注意到我得到了另一个验证器来检查相同的TextBox,所以在删除它之后,另一个验证器无法找到要验证的TextBox。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.