Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Building validators programatically

Reply
Thread Tools

Building validators programatically

 
 
Trillium
Guest
Posts: n/a
 
      02-07-2005
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?
 
Reply With Quote
 
 
 
 
Trillium
Guest
Posts: n/a
 
      02-07-2005
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
> Creator of "Professional Validation And More" at
> http://www.peterblum.com/vam/home.aspx
>
> "Trillium" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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?

>
>
>

 
Reply With Quote
 
 
 
 
Trillium
Guest
Posts: n/a
 
      02-07-2005
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: (E-Mail Removed)
> Creator of "Professional Validation And More" at
> http://www.peterblum.com/vam/home.aspx
>
> "Trillium" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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?

>
>
>

 
Reply With Quote
 
Trillium
Guest
Posts: n/a
 
      02-11-2005
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: (E-Mail Removed)
> Creator of "Professional Validation And More" at
> http://www.peterblum.com/vam/home.aspx
>
> "Trillium" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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: (E-Mail Removed)
> >> Creator of "Professional Validation And More" at
> >> http://www.peterblum.com/vam/home.aspx
> >>
> >> "Trillium" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> >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?
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefighters at the site of WTC7 "Move away the building is going to blow up, get back the building is going to blow up." Midex Python 24 05-07-2007 04:23 AM
Wireless building-to-building 101 Tim Jacob Wireless Networking 2 02-17-2006 09:46 AM
Building the menu Control Programatically =?Utf-8?B?U2hlbQ==?= ASP .Net 0 01-23-2006 08:49 PM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM
Building to Building wireless Patriot Cisco 2 11-04-2003 05:07 PM



Advertisments