集合已修改;在更新面板中向面板添加控件时,可能无法执行枚举操作
本文关键字:操作 枚举 执行 添加 修改 更新 集合 控件 | 更新日期: 2023-09-27 18:33:54
大家好,我有一个更新面板,里面有一个面板以及一些控件,如下所示
<asp:UpdatePanel ID="upPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" AssociatedControlID="txt1">
</asp:Label>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
对于文本框,我正在根据该列的数据库值动态验证,在页面加载中是强制性的,如下所示
RequiredFieldValidator rqrdColumn = new RequiredFieldValidator();
rqrdColumn.Display = ValidatorDisplay.None;
rqrdColumn.ID = "rqrd1";
rqrdColumn.ControlToValidate = txt1.ID;
rqrdColumn.ErrorMessage = "Can not be blank";
rqrdColumn.ValidationGroup = "vg";
ValidatorCalloutExtender vceColumn = new ValidatorCalloutExtender();
vceColumn.ID = "vce";
vceColumn.TargetControlID = rqrdColumn.ID;
panel.Controls.Add(rqrdColumn);
panel.Controls.Add(vceColumn);
updatepanel.ContentTemplateContainer.Controls.Add(panel);
updatepanel.Update();
foreach (Control c in page.Form.Controls)
{
if (c is UpdatePanel)
{
UpdatePanel up = c as UpdatePanel;
ControlCollection lcoll = c.Controls;
foreach (Control lc in lcoll)
{
if (lc is System.Web.UI.Control)
{
ControlCollection luCol = lc.Controls;
foreach (Control lu in luCol)
{
if (lu is Panel)
{
Panel p = lu as Panel;
ControlCollection lcol = p.Controls;
foreach (Control c2 in lcol.Tol)
{
if (c2 is Label)
{
Label aspxlbl = c2 as Label;
Control associatedColumn = p.FindControl(aspxlbl.AssociatedControlID);
if (associatedColumn != null)
{
if (associatedColumn is TextBox)
{
TextBox txtColumnName = associatedColumn as TextBox;
RequiredFieldValidator rqrdColumn = new RequiredFieldValidator();
rqrdColumn.Display = ValidatorDisplay.None;
rqrdColumn.ID = "rqrd1";
rqrdColumn.ControlToValidate = txtColumnName.ID;
rqrdColumn.ErrorMessage = "Can not be blank";
rqrdColumn.ValidationGroup = "vg";
ValidatorCalloutExtender vceColumn = new ValidatorCalloutExtender();
vceColumn.ID = "vce";
vceColumn.TargetControlID = rqrdColumn.ID;
p.Controls.Add(rqrdColumn);
p.Controls.Add(vceColumn);
up.ContentTemplateContainer.Controls.Add(p);
up.Update();
}
}
}
}
}
}
}
}
}
}
除了那个例外,一切都很好,
集合已修改;枚举操作可能无法执行
有人可以帮助我吗?
你的问题在于你使用 foreach 的方式。
所以代替:
foreach (Control c2 in lcol)
{...}
用
foreach(Control c2 in lcol.OfType<Control>().ToList())
{...}
它应该有效。
当你在它重构
代码时,它更容易阅读:foreach (UpdatePanel updatePanel in page.Form.Controls.OfType<UpdatePanel>().ToList())
{
foreach (Control control in updatePanel.Controls.OfType<Control>().ToList())
{
foreach (Panel panel in control.Controls.OfType<Panel>().ToList())
{
foreach (Label label in panel.Controls.OfType<Label>().ToList())
{
TextBox txtColumnName = panel.FindControl(label.AssociatedControlID) as TextBox;
if (txtColumnName != null)
{
RequiredFieldValidator rqrdColumn = new RequiredFieldValidator();
rqrdColumn.Display = ValidatorDisplay.None;
rqrdColumn.ID = "rqrd1";
rqrdColumn.ControlToValidate = txtColumnName.ID;
rqrdColumn.ErrorMessage = "Can not be blank";
rqrdColumn.ValidationGroup = "vg";
ValidatorCalloutExtender vceColumn = new ValidatorCalloutExtender();
vceColumn.ID = "vce";
vceColumn.TargetControlID = rqrdColumn.ID;
panel.Controls.Add(rqrdColumn);
panel.Controls.Add(vceColumn);
updatePanel.ContentTemplateContainer.Controls.Add(p);
updatePanel.Update();
}
}
}
}
}