大家好,我有一个更新面板,里面有一个面板以及一些控件,如下所示
<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();
}
}
}
}
}
}
}
}
}
}
除了那个例外,一切都很好,
收藏已修改;枚举操作可能无法执行
有人能帮我吗?