简体   繁体   English

使用RequiredFieldValidator验证服务器控件中的文本框

[英]Validating textbox in Server Control using RequiredFieldValidator

I have custom server control, which contains three textboxes. 我有自定义服务器控件,其中包含三个文本框。 I need to validate its content using ASP.NET field validators. 我需要使用ASP.NET字段验证器来验证其内容。

I set ' ControlToValidate ' property of RequiredFieldValidator to the ID property of my textbox, but it seems that the validator is unable to find this textbox. 我将RequiredFieldValidator的' ControlToValidate '属性设置为我的文本框的ID属性,但是似乎验证程序无法找到此文本框。 Here is simplified code of what I'm trying to do (fields and properties that are not currently in use are omitted): 这是我正在尝试做的简化代码(省略了当前未使用的字段和属性):

[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl runat=server></{0}:ServerControl>")]
public class CommentServerControl : WebControl, IPostBackDataHandler
{
    private TextBox _textBoxName;
    private RequiredFieldValidator _requiredName;

    protected override void OnInit(EventArgs e)
    {
        var idName = "Name-" + UniqueID;
        _textBoxName = new TextBox();
        _textBoxName.ID = idName;
        _textBoxName.ClientIDMode = System.Web.UI.ClientIDMode.Static;
        _textBoxName.ValidationGroup = ValidationGroup; // is set up via mark up
        Controls.Add(_textBoxName);
        _requiredName = new RequiredFieldValidator();
        _requiredName.ID = "RequiredName-" + UniqueID;
        _requiredName.ValidationGroup = ValidationGroup;
        _requiredName.ErrorMessage = RequiredMessage; // is set up via mark up
        _requiredName.ControlToValidate = _textBoxName.ID;
        Controls.Add(_requiredName);
    }

    public string ValidationGroup
    {
        get
        {
            var s = (string)ViewState["ValidationGroup"];
            return (s ?? string.Empty);
        }
        set
        {
            ViewState["ValidationGroup"] = value;
        }
    }

    public string RequiredMessage
    {
        get
        {
            var s = (string)ViewState["RequiredMessage"];
            return (s ?? string.Empty);
        }
        set
        {
            ViewState["RequiredMessage"] = value;
        }
    }
}

Running this code, I receive an error ' Unable to find control id 'Name-ctl00$MainContent$ctl00' referenced by the 'ControlToValidate' property of 'RequiredName'. 运行此代码,我收到一个错误' 无法找到由'RequiredName'的'ControlToValidate'属性引用的控件ID'Name-ctl00 $ MainContent $ ctl00'。 '

I have read that this might occur because controls have different NamingContainer, but in this case references to NamingContainer are the same. 我已经读到可能会发生这种情况,因为控件具有不同的NamingContainer,但是在这种情况下,对NamingContainer的引用是相同的。 However, FindControl(string id) method can't find my textbox control in OnInit method. 但是, FindControl(string id)方法无法在OnInit方法中找到我的文本框控件。 What am I doing wrong? 我究竟做错了什么? Thanks in advance. 提前致谢。

Note: using UniqueID for creation of ID is used to prevent collisions in case of multiple use of my control. 注意:在多次使用我的控件的情况下,使用UniqueID创建ID可以防止发生冲突。

On_Init为时过早,因为此时尚未创建控件。

I've found out where is the problem. 我发现了问题所在。 I don't know yet why, but ID property of control must not contain '$' character. 我还不知道为什么,但是控件的ID属性不能包含“ $”字符。 In my case, UniqueID property returned string like ' ctl00$MainContent$ctl00 ', so complete ID of control was ' Name-ctl00$MainContent$ctl00 '. 在我的情况下,UniqueID属性返回的字符串如“ ctl00 $ MainContent $ ctl00 ”,因此控件的完整ID为“ Name-ctl00 $ MainContent $ ctl00 ”。 Replacing '$' with other symbol (to preserve uniqueness) solves the problem. 用其他符号替换“ $”(以保持唯一性)可以解决此问题。 By the way, FindControl method also began to work. 顺便说一下, FindControl方法也开始起作用。

_textBoxName.ID = "Name_" + UniqueID.Replace('$', '_')

Also note, that symbol '-' as validator's ID will cause syntax error in JS (as ASP.NET will try to create JS objects with the same name), so it is not recommended to use it either. 另请注意,符号“-”作为验证者的ID将导致JS中的语法错误(因为ASP.NET将尝试创建具有相同名称的JS对象),因此也不建议使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM