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. Advertisements

  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. Advertisements

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. John Wallace
    Replies:
    0
    Views:
    436
    John Wallace
    Aug 6, 2003
  2. brw
    Replies:
    2
    Views:
    2,726
  3. Guest

    dynamically adding to HEAD tag

    Guest, Aug 11, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    895
    Jacob Yang [MSFT]
    Aug 17, 2003
  4. Felipe Garcia
    Replies:
    1
    Views:
    6,049
    ElmoWatson
    Aug 28, 2003
  5. ravi sankar

    Dynamically Adding User Control

    ravi sankar, Aug 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    561
    Ram sundar
    Aug 29, 2003
  6. vtreddy
    Replies:
    0
    Views:
    2,235
    vtreddy
    Nov 6, 2003
  7. Dan Sikorsky
    Replies:
    1
    Views:
    6,040
    Dan Sikorsky
    Feb 17, 2005
  8. =?Utf-8?B?THVrZSBNb3JlaGVhZA==?=

    CustomValidator not adding error to ValidationSummary

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