Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9283129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:33:04+00:00 2026-06-18T18:33:04+00:00

I have custom server control, which contains three textboxes. I need to validate its

  • 0

I have custom server control, which contains three textboxes. I need to validate its content using ASP.NET field validators.

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. 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’.‘

I have read that this might occur because controls have different NamingContainer, but in this case references to NamingContainer are the same. However, FindControl(string id) method can’t find my textbox control in OnInit method. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:33:06+00:00Added an answer on June 18, 2026 at 6:33 pm

    I’ve found out where is the problem. I don’t know yet why, but ID property of control must not contain ‘$’ character. In my case, UniqueID property returned string like ‘ctl00$MainContent$ctl00‘, so complete ID of control was ‘Name-ctl00$MainContent$ctl00‘. Replacing ‘$’ with other symbol (to preserve uniqueness) solves the problem. By the way, FindControl method also began to work.

    _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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom asp.net server control to display images.What I need now is
I'm developing a custom server control in Asp.NET (.NET 3.5) which inherits the CompositeControl
I have created a simple Asp.Net custom control which automatically combines all the correct
I have an Asp.Net repeater, which contains a textbox and a checkbox. I need
I have a custom server control which wraps a RadEditor (basically a textarea). I
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have custom server control contains templatefield when an out button clicked the field
If I have a custom user control (say, MyCustomControl.ascx) which contains a number of
I have a custom control which inherits from System.Web.UI.Control and some of its properties
I have a custom server control which renders some HTML on the aspx page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.