Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically add validation controls

Posted on 2004-08-02
8
Medium Priority
?
818 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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