Solved

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

Posted on 2006-07-14
7
776 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
  • 4
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
pallosp earned 500 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now