1

大家好,我有一个更新面板,里面有一个面板以及一些控件,如下所示

<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();
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}

除了那个例外,一切都很好,

收藏已修改;枚举操作可能无法执行

有人能帮我吗?

4

1 回答 1

3

您的问题在于您使用 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();
                }
            }
        }
    }
}
于 2013-10-03T10:52:49.747 回答