C# Can';t将RegularExpressionValidator添加到面板

C# Can';t将RegularExpressionValidator添加到面板,c#,asp.net,.net,C#,Asp.net,.net,我在以编程方式向任何容器控件(面板、占位符等)添加RegularExpressionValidator时遇到问题。这是我的密码: // Get the path of the file on the server string page = Page.Request.FilePath; int managementCompanyId = Convert.ToInt32(Session["ManagementCompanyId_AddResident"].ToString().Trim());

我在以编程方式向任何容器控件(面板、占位符等)添加
RegularExpressionValidator
时遇到问题。这是我的密码:

// Get the path of the file on the server
string page = Page.Request.FilePath;
int managementCompanyId = Convert.ToInt32(Session["ManagementCompanyId_AddResident"].ToString().Trim());

// Get field validation details
Collection<ExportFieldValidation> details = ValidationBL.GetValidationDetails(managementCompanyId, page);

ContentPlaceHolder body = Page.Form.FindControl("ContentBody") as ContentPlaceHolder;

foreach (ExportFieldValidation detailItem in details)
{
    // Check if the control exists on the page
    TextBox control = body.FindControl(detailItem.FieldToValidate) as TextBox;

    if (control != null)
    {
        RegularExpressionValidator regex = new RegularExpressionValidator()
        {
            ControlToValidate = control.UniqueID.ToString(),
            ID = detailItem.ValidatorFieldName,
            ValidationExpression = detailItem.RegularExpression,
            Page = this,
            SetFocusOnError = true,
            Text = detailItem.ErrorMessage,
            Enabled = true,
            EnableViewState = true,
            CssClass = "Error"
        };

        Panel validationPanel = body.FindControl("PanelAddResident") as Panel;

        validationPanel.Controls.Add(regex);
    }
}
//获取服务器上文件的路径
字符串page=page.Request.FilePath;
int managementCompanyId=Convert.ToInt32(会话[“managementCompanyId_AddResident”].ToString().Trim());
//获取字段验证详细信息
Collection details=ValidationBL.GetValidationDetails(managementCompanyId,第页);
ContentPlaceHolder body=Page.Form.FindControl(“ContentBody”)作为ContentPlaceHolder;
foreach(ExportFieldValidation detailItem详细信息)
{
//检查该页上是否存在该控件
TextBox control=body.FindControl(detailItem.FieldToValidate)作为TextBox;
if(控件!=null)
{
RegularExpressionValidator regex=新的RegularExpressionValidator()
{
ControlToValidate=control.UniqueID.ToString(),
ID=detailItem.ValidatorFieldName,
ValidationExpression=detailItem.RegularExpression,
Page=这个,
SetFocusOneError=true,
Text=detailItem.ErrorMessage,
启用=真,
EnableViewState=true,
CssClass=“错误”
};
Panel validationPanel=body.FindControl(“PanelAddResident”)作为面板;
validationPanel.Controls.Add(regex);
}
}
当我转到页面时,我得到一个错误
无法找到由'RegularExpressionValidatorResidentId'
的'ControlToValidate'属性引用的控件id'myControl',其中我的控件是上面的
控件.UniqueID.ToString()
,我们将其存储在数据库中,并且肯定是正确的,因为我重复了-,三重和四重检查值

但是,如果我替换
validationPanel.Controls.Add(regex)
Page.Form.Controls.Add(regex)一起使用一切都很完美

有没有办法将我的验证器添加到容器中?我确信我只是做错了什么,或者错过了中间的一步。任何帮助都将不胜感激。

此部分错误:

ControlToValidate = control.UniqueID.ToString()
您需要使用以下选项:

ControlToValidate = control.ID;
之前,您必须为控件提供
ID

UniqueID
是组件在客户端中的名称,但验证程序控件使用服务器端控件名称来完成此操作。

太好了!我发誓我已经试过了,但我想没有。我想那是个小东西。谢谢您需要使用
UniqueID
进行javascript操作,因为您无法预测生成的控件将具有的奇怪名称有道理,但是你能给我一个当我将验证程序直接添加到页面时使用UniqueID工作的原因吗?我不知道确切原因,但取决于你的标记,你的UniqueID和ID最终可能是相同的(如果你使用的是ASP.NET 4)。它是.NET 3.5。我不得不做一些奇怪的事情。这是数据库驱动的,所以我需要说明面板不在页面上的情况,并将验证器添加到表单中。在这种情况下,我不能使用
control.ID
。它给了我与上面相同的错误。当绑定到页面时,我必须使用UniqueID。绑定到面板时,只需ID。