Solved

Dynamically add validation controls

Posted on 2004-08-02
8
787 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

24 Experts available now in Live!

Get 1:1 Help Now