Solved

Dynamically add validation controls

Posted on 2004-08-02
8
800 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 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 200 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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