Adding CustomValidator dynamically

  • Thread starter sebastien.varoteaux
  • Start date
S

sebastien.varoteaux

Hi all,

I have a form on which i add n * 2 TextBox dynamically.
I would like to validate them with a CustomValidator.

I do it like that (calling from OnInit method):

parentControl.Controls.Add(new LiteralControl("<label for=\"" +
"tbFirstname" + counter + "\">Firstname</label>"));
TextBox aChildBox = new TextBox();
aChildBox.ID = "tbFirstname" + counter;
parentControl.Controls.Add(aChildBox);
CustomValidator aCustomValidator = new CustomValidator();
aCustomValidator.EnableClientScript = false;
aCustomValidator.ControlToValidate = aChildBox.ID;
aCustomValidator.Display = ValidatorDisplay.Dynamic;
aCustomValidator.ServerValidate += new
System.Web.UI.WebControls.ServerValidateEventHandler(this.name_validator);
aCustomValidator.Enabled = true;
this.Validators.Add(aCustomValidator);

When i click my submit button, validation go on. But...
I have the following error:

NullReferenceException: La référence d'objet n'est pas définie à
une instance d'un objet.]

System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String
name, String propertyName)
System.Web.UI.WebControls.CustomValidator.ControlPropertiesValid()
System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()
System.Web.UI.Page.Validate()

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()



I do not want to valid these fields manually because i am sure that
it's possible!
Is somebody could help me?

Best regards
 
S

sebastien.varoteaux

I found a solution.
I create a user control (ascx) with textboxes and associated
customValidator.
Then, i load the ascx from my aspx webpage.

parentControl.Controls.Add(LoadControl("file.ascx"));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top