Solved

Dynamically add validation controls

Posted on 2004-08-02
8
788 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

21 Experts available now in Live!

Get 1:1 Help Now