Building validators programatically

Discussion in 'ASP .Net Web Controls' started by Trillium, Feb 7, 2005.

  1. Trillium

    Trillium Guest

    I need to build controls and their associated validators dynamically,
    depending on requirements from a database query. The database provides the
    text of the questions, the type of control needed (textbox, radio buttons,
    etc), and validation for the control (required, number, string etc).

    I am going about this by checking the control requirements for each record,
    declaring a new control of the type needed, then adding it to a placeholder
    with .Controls.Add(input control). Then I check the validation for the
    control, and declare and create the appropriate validator type(s), and add
    them to the placeholder and also Validators with
    Page.Validators.Add(validator control).

    The controls build ok, and some of the validators work, but not all. I
    notice that, upon submit, the validators collection only has the ones which
    are hard-coded into the html. So maybe the Page.Validators.Add is not
    enough, and maybe this is part of the problem.

    I am new at .NET, so I may be going about this in the wrong way. What am I
    missing?
     
    Trillium, Feb 7, 2005
    #1
    1. Advertising

  2. Trillium

    Trillium Guest

    The first place I tried to add it was in Page_Load. Then I saw someone else
    was creating the validator in Page_Init, and .Add-ing it to the Form instead
    of the PlaceHolder. So I tried that, and the validator showed up in the
    validators collection upon submit, but always with IsValid = True. So I'm
    still not getting it.

    Any thoughts on that?

    "Peter Blum" wrote:

    > By creating a Validator and adding it to the PlaceHolder, it will
    > automatically add itself to the Page.Validators collection. However, if you
    > create it in the OnPreRender event, its too late for the validator to do its
    > job. So I suspect that you are creating these controls in OnPreRender and
    > should make them create earlier, like in Page_Load or a post back event
    > handler.
    >
    > Some background. The validators use the OnPreRender event to add themselves
    > to the client-side list called Page_Validators.
    >
    > --- Peter Blum
    > www.PeterBlum.com
    > Email:
    > Creator of "Professional Validation And More" at
    > http://www.peterblum.com/vam/home.aspx
    >
    > "Trillium" <> wrote in message
    > news:...
    > >I need to build controls and their associated validators dynamically,
    > > depending on requirements from a database query. The database provides
    > > the
    > > text of the questions, the type of control needed (textbox, radio buttons,
    > > etc), and validation for the control (required, number, string etc).
    > >
    > > I am going about this by checking the control requirements for each
    > > record,
    > > declaring a new control of the type needed, then adding it to a
    > > placeholder
    > > with .Controls.Add(input control). Then I check the validation for the
    > > control, and declare and create the appropriate validator type(s), and add
    > > them to the placeholder and also Validators with
    > > Page.Validators.Add(validator control).
    > >
    > > The controls build ok, and some of the validators work, but not all. I
    > > notice that, upon submit, the validators collection only has the ones
    > > which
    > > are hard-coded into the html. So maybe the Page.Validators.Add is not
    > > enough, and maybe this is part of the problem.
    > >
    > > I am new at .NET, so I may be going about this in the wrong way. What am
    > > I
    > > missing?

    >
    >
    >
     
    Trillium, Feb 7, 2005
    #2
    1. Advertising

  3. Trillium

    Trillium Guest

    I've just re-read your reply, and I think there is a clue there. Do I have
    this right?

    I have been trying to create the validators at the same time as I create the
    input controls: when the page is first called. Should I be waiting for when
    the post-back occurs and THEN create the validators, possibly in Page_Load?

    "Peter Blum" wrote:

    > By creating a Validator and adding it to the PlaceHolder, it will
    > automatically add itself to the Page.Validators collection. However, if you
    > create it in the OnPreRender event, its too late for the validator to do its
    > job. So I suspect that you are creating these controls in OnPreRender and
    > should make them create earlier, like in Page_Load or a post back event
    > handler.
    >
    > Some background. The validators use the OnPreRender event to add themselves
    > to the client-side list called Page_Validators.
    >
    > --- Peter Blum
    > www.PeterBlum.com
    > Email:
    > Creator of "Professional Validation And More" at
    > http://www.peterblum.com/vam/home.aspx
    >
    > "Trillium" <> wrote in message
    > news:...
    > >I need to build controls and their associated validators dynamically,
    > > depending on requirements from a database query. The database provides
    > > the
    > > text of the questions, the type of control needed (textbox, radio buttons,
    > > etc), and validation for the control (required, number, string etc).
    > >
    > > I am going about this by checking the control requirements for each
    > > record,
    > > declaring a new control of the type needed, then adding it to a
    > > placeholder
    > > with .Controls.Add(input control). Then I check the validation for the
    > > control, and declare and create the appropriate validator type(s), and add
    > > them to the placeholder and also Validators with
    > > Page.Validators.Add(validator control).
    > >
    > > The controls build ok, and some of the validators work, but not all. I
    > > notice that, upon submit, the validators collection only has the ones
    > > which
    > > are hard-coded into the html. So maybe the Page.Validators.Add is not
    > > enough, and maybe this is part of the problem.
    > >
    > > I am new at .NET, so I may be going about this in the wrong way. What am
    > > I
    > > missing?

    >
    >
    >
     
    Trillium, Feb 7, 2005
    #3
  4. Trillium

    Trillium Guest

    Thank You. This was exactly what I needed to help figure out what the
    problem was. I was calling Page.Validate() in Page_Load and I was not
    carefully setting an ID for each validator. (Plus, I had Type = "Date"...
    duh)

    "Peter Blum" wrote:

    > Sorry for the delayed response.
    >
    > You should always create the validators, whether on a new page or after post
    > back. After all, the validators need to be generated if you want client-side
    > validation before post back.
    >
    > You can create validators either in Page_Init or Page_Load. They are normal
    > web controls and conform to the rules of everything else. Just make sure you
    > assign their ID property and add them to a containing control that is in the
    > Page's control tree. The PlaceHolder usually is such a control but it can be
    > a UserControl, the form (which doesn't give you control over placement), a
    > tablecell, a panel, etc.
    >
    > All controls run a PreRender and Render phase so long as they are visible.
    > Validators contained within an invisible container will never operate.
    > During OnPreRender, each validator confirms its properties and outputs its
    > javascript for client-side validation. During Render, each validator creates
    > the <span> tag where the error message appears.
    >
    > All of this is normal and you don't worry about it since you created Visible
    > validators in Page_Load or Page_Init.
    >
    > Then there are features that need to be set to be sure validation runs:
    > 1. A button has CausesValidation=true
    > 2. A Validator as Enabled=true, EnableClientScript=true (for client-side
    > validation)
    > 3. Any post back event not associated with a Button, LinkButton or
    > ImageButton needs you to call Page.Validate() for server side validation.
    > 4. Your post back event must always test Page.IsValid is true before
    > attempting to save.
    > 5. Do not call Page.Validate() in Page_Load. It only belongs in post back
    > event handlers.
    >
    > --- Peter Blum
    > www.PeterBlum.com
    > Email:
    > Creator of "Professional Validation And More" at
    > http://www.peterblum.com/vam/home.aspx
    >
    > "Trillium" <> wrote in message
    > news:...
    > > I've just re-read your reply, and I think there is a clue there. Do I
    > > have
    > > this right?
    > >
    > > I have been trying to create the validators at the same time as I create
    > > the
    > > input controls: when the page is first called. Should I be waiting for
    > > when
    > > the post-back occurs and THEN create the validators, possibly in
    > > Page_Load?
    > >
    > > "Peter Blum" wrote:
    > >
    > >> By creating a Validator and adding it to the PlaceHolder, it will
    > >> automatically add itself to the Page.Validators collection. However, if
    > >> you
    > >> create it in the OnPreRender event, its too late for the validator to do
    > >> its
    > >> job. So I suspect that you are creating these controls in OnPreRender and
    > >> should make them create earlier, like in Page_Load or a post back event
    > >> handler.
    > >>
    > >> Some background. The validators use the OnPreRender event to add
    > >> themselves
    > >> to the client-side list called Page_Validators.
    > >>
    > >> --- Peter Blum
    > >> www.PeterBlum.com
    > >> Email:
    > >> Creator of "Professional Validation And More" at
    > >> http://www.peterblum.com/vam/home.aspx
    > >>
    > >> "Trillium" <> wrote in message
    > >> news:...
    > >> >I need to build controls and their associated validators dynamically,
    > >> > depending on requirements from a database query. The database provides
    > >> > the
    > >> > text of the questions, the type of control needed (textbox, radio
    > >> > buttons,
    > >> > etc), and validation for the control (required, number, string etc).
    > >> >
    > >> > I am going about this by checking the control requirements for each
    > >> > record,
    > >> > declaring a new control of the type needed, then adding it to a
    > >> > placeholder
    > >> > with .Controls.Add(input control). Then I check the validation for the
    > >> > control, and declare and create the appropriate validator type(s), and
    > >> > add
    > >> > them to the placeholder and also Validators with
    > >> > Page.Validators.Add(validator control).
    > >> >
    > >> > The controls build ok, and some of the validators work, but not all. I
    > >> > notice that, upon submit, the validators collection only has the ones
    > >> > which
    > >> > are hard-coded into the html. So maybe the Page.Validators.Add is not
    > >> > enough, and maybe this is part of the problem.
    > >> >
    > >> > I am new at .NET, so I may be going about this in the wrong way. What
    > >> > am
    > >> > I
    > >> > missing?
    > >>
    > >>
    > >>

    >
    >
    >
     
    Trillium, Feb 11, 2005
    #4
    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. William F. Robertson, Jr.

    Re: Validators

    William F. Robertson, Jr., Jun 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    856
    William F. Robertson, Jr.
    Jun 25, 2003
  2. Marina

    All validators stopped working

    Marina, Jun 27, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    437
    Rajesh Tiwari
    Jun 27, 2003
  3. Ratman
    Replies:
    0
    Views:
    664
    Ratman
    Sep 14, 2004
  4. =?Utf-8?B?U2hlbQ==?=

    Building the menu Control Programatically

    =?Utf-8?B?U2hlbQ==?=, Jan 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    =?Utf-8?B?U2hlbQ==?=
    Jan 23, 2006
  5. Midex
    Replies:
    24
    Views:
    921
Loading...

Share This Page