Solved

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

Posted on 2006-07-14
7
778 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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