Is it possible to create validation controls in a code behind file?

I want to use ASP.NET 2.0 validation controls. However, I don't want to place the validation tags (ie: <asp:RequiredFieldValidator ID="valFirstName" runat="server"></asp:RequiredFieldValidator>) in my aspx file. I want to create validator objects in my code behind file to keep my validation seperate and tidy.

I've tried placing the following code in the page load event, but the Validate() method throws an error (System.NullReferenceException: Object reference not set to an instance of an object.).

Dim valFirstName As New UI.WebControls.RequiredFieldValidator()
valFirstName.EnableClientScript = False
valFirstName.ControlToValidate = "txtFirstName"

Is this possible? How can I do it? Or am I stuck placing the asp tags in my aspx file?

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

pallospConnect With a Mentor Commented:
You should add the validator to the page, e.g. Form.Controls.Add(valFirstName);
>>I want to create validator objects in my code behind file to keep my validation seperate and tidy.
Any control you create in code behind will have to render its HTML, there is no way to have it seperate since all controls have to be added to aspx page.
what you can do is write code "No Validatation controls" and validate on the server side.

graphicodysseyAuthor Commented:

That worked like a charm.

I see that if I enable client-side scripting, it still creates the client-side script and validates fine. I'm wondering if there's any way to bind the validation control to say a text control to display the error message via the JavaScript. Is this possible?

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

graphicodysseyAuthor Commented:
By the way, in the rendered HTML I see that it creates the span/div tag, but it places it right before the closing </form> tag. I'm assuming it does this since it doesn't know where else to go. So... is there any way to tell it to go somewhere else?
graphicodysseyAuthor Commented:
I was able to answer my own question. I added a PlaceHolder control to the page and then was able to add the validation control to the placeholder control programmatically, thus allowing me to place the validation text anywhere on the page. If there's a better way to do this, please let me know, otherwise thanks for your help!
You don't need a PlaceHolder. The validator can be inserted at a particular position by a Form.Controls.AddAt() call, if you know the exact position.
However it's easier to determine the position of the message by a PlaceHolder.
graphicodysseyAuthor Commented:
Even better! Thank you for pointing that out.

I create a subroutine to find the index of the field I wanted to validate and then used the AddAt method to place the validator. This is EXACTLY what I wanted to do. Thanks again.
All Courses

From novice to tech pro — start learning today.