Adding CustomValidator dynamically

Discussion in 'ASP .Net Web Controls' started by sebastien.varoteaux@gmail.com, Apr 10, 2006.

  1. Guest

    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
     
    , Apr 10, 2006
    #1
    1. Advertising

  2. Guest

    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"));
     
    , Apr 10, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edmilson

    Errormessage in CustomValidator

    Edmilson, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    Edmilson
    Jul 10, 2003
  2. Wayne MJ

    CustomValidator limitation?

    Wayne MJ, Jul 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    350
    Wayne MJ
    Jul 21, 2003
  3. George Ter-Saakov

    CustomValidator controls

    George Ter-Saakov, Jul 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    George Ter-Saakov
    Jul 21, 2003
  4. Dan Sikorsky
    Replies:
    1
    Views:
    5,860
    Dan Sikorsky
    Feb 17, 2005
  5. =?Utf-8?B?THVrZSBNb3JlaGVhZA==?=

    CustomValidator not adding error to ValidationSummary

    =?Utf-8?B?THVrZSBNb3JlaGVhZA==?=, Aug 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,894
    Peter Blum
    Aug 22, 2005
Loading...

Share This Page