【发布时间】: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