Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamically add validation controls

Posted on 2004-08-02
8
Medium Priority
?
821 Views
Last Modified: 2008-03-17
Hi Experts,

I've got a form with few controls which are added in the design time.. Based on the user input, I add few more controls dynamically which works fine.

What I need is to add validation controls to these dynamic controls. I tried doing this but it doesn't work..

RequiredFieldValidator ReqVal = new RequiredFieldValidator();
ReqVal.ControlToValidate = FieldID;
ReqVal.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
ReqVal.ErrorMessage = FieldName + " is Mandatory!";

I reckon when I see the source of the page once loaded, I should see these validation controls across the element.. which I'm not getting.. What is that I'm missing? Pls help..

cheers,
kaykay!
0
Comment
Question by:skaykay
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:crescendo
ID: 11694088
It looks like you're not adding the new validator to the collection of existing validators. I think it's

Validators.Add(ReqVal)
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11694394
A better idea, to make sure the validator appears where you want it to appear, add a placeholder at the correct place. The following code works for a textbox called TextBox1 and a placeholder called PlaceHolder1.

        Dim val As New RequiredFieldValidator
        val.ControlToValidate = TextBox1.ID
        val.ErrorMessage = "This field is required"
        val.Text = "This field is required"
        PlaceHolder1.Controls.Add(val)
0
 
LVL 4

Author Comment

by:skaykay
ID: 11694418
I couldn't find any collection Validators.. Is it something else?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:boulder_bum
ID: 11695913
RequiredFieldValidator ReqVal = new RequiredFieldValidator();
ReqVal.ControlToValidate = FieldID;
ReqVal.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
ReqVal.ErrorMessage = FieldName + " is Mandatory!";

As crescendo pointed out, you don't seem to add your validator to the page at all. The simple way to add (assuming you have a validation summary control and don't care where the validator is located) is to add via Page.Controls.Add( ReqVal ). Otherwise, you'd need to add it in whatever container you want to contain your controls.
0
 
LVL 4

Author Comment

by:skaykay
ID: 11700636
Thanks for yoru responses. Well, yes I have a validation summary control and hence don't use a placeholder for this.

I have a function in a class which dynamically adds these controls and validation controls too... Now, how will I add the dynamically added controls to the calling web page? Is there any way I can pass the page reference to the class and add it to that? Or is there any other way? I hope I'm clear with my point..

cheers,
kaykay!
0
 
LVL 4

Author Comment

by:skaykay
ID: 11700876
Also, when I tried Page.Controls.Add, I get an error like this..

Control of type 'RequiredFieldValidator' must be placed inside a form tag with runat=server

I couldn't fine any property in the RequiredFieldValidator object for RunAt? How will I specify this.. and also I already have some elements inside a form. I add few elements outside.

kaykay!
0
 
LVL 9

Accepted Solution

by:
crescendo earned 800 total points
ID: 11701565
Pass the control that you want to validate, and add the validator to its controls collection, like so

    Public Sub AddValidator(myControl as Control)
        dim ReqVal as New RequiredFieldValidator
        ReqVal.ControlToValidate = myControl.ID
        ReqVal.Message = myControl.ID + " is mandatory!"
        myControls.Controls.Add(ReqVal)
    End Sub

0
 
LVL 4

Author Comment

by:skaykay
ID: 11712653
Well, I'm partially through with this. I created a HTMLForm instance and then added the validation controls to it.. Then it worked..

thanks all for your responses.

cheers,
kaykay!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

963 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