Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-14
7
Medium Priority
?
781 Views
Last Modified: 2010-08-05
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"
'valFirstName.Validate()

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

Thanks!
0
Comment
Question by:graphicodyssey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
pallosp earned 2000 total points
ID: 17106721
You should add the validator to the page, e.g. Form.Controls.Add(valFirstName);
0
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 17107696
graphicodyssey,
>>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.

HTH
0
 
LVL 1

Author Comment

by:graphicodyssey
ID: 17113349
pallosp-

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?

Thanks,
Ken
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:graphicodyssey
ID: 17113389
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?
0
 
LVL 1

Author Comment

by:graphicodyssey
ID: 17113487
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!
0
 
LVL 9

Expert Comment

by:pallosp
ID: 17113963
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.
0
 
LVL 1

Author Comment

by:graphicodyssey
ID: 17115735
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question