Solved

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

Posted on 2006-07-14
7
775 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

18 Experts available now in Live!

Get 1:1 Help Now