By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Custom Textbox and Validator Control

P: n/a
Has anyone seen an example of a textbox server control that has
built-in client-side validation? I'd like to build a server control
that extends the System.Web.UI.WebControls.TextBox class to allow
javascript checks for things like valid e-mail addresses or phone
numbers (without having to add a separate control for validation).

One idea I did some work on was having the control implement the
IValidator interface and basically recreating ASP.NET's implementation
of its various validation controls inside of my new textbox control. I
have a separate javascript library of validation functions that
basically mirror the ones in WebValidation.js that ASP.NET uses for
its validators. I register the javascript on the page and manually
emit the html for the validator inside of the Render() method.
However, this seems like a lot of duplication of code that has already
been written for the ASP.NET validators. Plus, there is some
awkwardness in how ASP.NET calls the validation on the client-side
that I couldn't make go away (the submit button's onclick() event
always calls Page_ClientValidate, which is defined in WebValidation.js
-- what if I want it to call my own custom function? I could name it
Page_ClientValidate, as well, but then what if I want to use an
ASP.NET validator somewhere else on the page?).

The second approach that I am considering is creating an appropriate
ASP.NET validator inside of the control's OnInit() function and
attaching it to the page next to the control. There are a few
questions I have about this:

1) How to locate where on the page the validator should be added? It
should render right next to the textbox. Would I need to access the
control's Parent object and add the validator to its collection of
Controls? Is the Parent guaranteed to be available inside of OnInit()?

2) Will dynamically adding the validator ever possibly corrupt the
ViewState? I've read that when controls are inserted dynamically in
between other controls that the ViewState can become out of sync
(something about controls that are added at design time being created
before dynamic controls). I assume this depends on when the control is
added (i.e., OnInit() or OnLoad()).

3) Will server-side validation still take place? When does the Page
object iterate through its collection of validators?

The control must extend System.Web.UI.Textbox, it can't be a .ascx
file or a composite control.

Has anyone done anything similar? I'd like to use the second approach,
but I'm unsure of how solid it is. Thanks in advance.
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
When I read this initially, I thought "a user control will do very well
here". All you need to do is expose properties on the TextBox control and
even the TextBox itself as properties of the User Control.
Then I read that you didn't want a UserControl. Can you clarify why not? If
you have an incorrect assumption, I'd like to correct it if it will save you
time.

Please don't rewrite Microsoft's validator code. Certainly feel free to
create new controls either from System.Web.UI.WebControls.BaseValidator or
implementing IValidator. I have already spend months creating an extensive
replacement to Microsoft's validation framework. You can check out
"Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx.

It does not merge a textbox with any validators within one control because
that imposes too many limitations on the user:
- there are many validators that the user wants
- there are all kinds of logic involved with each of the validators. Do you
want to expose all of the properties of the RangeValidator, a
CompareValidator, etc.?
- each validator has its own error message. Often the error messages need to
be customized to the page, such as including the field label.
- the formatting is an important aspect of validation. While you may want to
put the validators to the right of your textbox, some pages may require that
the validator goes below or elsewhere on the page.

Note that the TextBox control doesn't know how to handle having child
controls. It simply renders its on HTML for <input type=text>. You
definitely can add child controls to it. Use the CreateChildControls()
method or OnPreRender() method to add. However, since those controls aren't
rendered in the Render() method, you must override RenderControl. It should
call the ancestor RenderControl to handle the <input type=text> and then
call RenderControl on each of the elements in the Controls collection.

The ViewState only breaks if the list of controls created when the page was
first output changes as the page is recreated on post back. This is unlikely
in your case.

If you create Microsoft validators (or even use mine) within your control,
so long as they are added to the Controls collection of your control, they
will be initialized properly for client and server side validation.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com

"Pham Nguyen" <sh*********@yahoo.com> wrote in message
news:f6**************************@posting.google.c om...
Has anyone seen an example of a textbox server control that has
built-in client-side validation? I'd like to build a server control
that extends the System.Web.UI.WebControls.TextBox class to allow
javascript checks for things like valid e-mail addresses or phone
numbers (without having to add a separate control for validation).

One idea I did some work on was having the control implement the
IValidator interface and basically recreating ASP.NET's implementation
of its various validation controls inside of my new textbox control. I
have a separate javascript library of validation functions that
basically mirror the ones in WebValidation.js that ASP.NET uses for
its validators. I register the javascript on the page and manually
emit the html for the validator inside of the Render() method.
However, this seems like a lot of duplication of code that has already
been written for the ASP.NET validators. Plus, there is some
awkwardness in how ASP.NET calls the validation on the client-side
that I couldn't make go away (the submit button's onclick() event
always calls Page_ClientValidate, which is defined in WebValidation.js
-- what if I want it to call my own custom function? I could name it
Page_ClientValidate, as well, but then what if I want to use an
ASP.NET validator somewhere else on the page?).

The second approach that I am considering is creating an appropriate
ASP.NET validator inside of the control's OnInit() function and
attaching it to the page next to the control. There are a few
questions I have about this:

1) How to locate where on the page the validator should be added? It
should render right next to the textbox. Would I need to access the
control's Parent object and add the validator to its collection of
Controls? Is the Parent guaranteed to be available inside of OnInit()?

2) Will dynamically adding the validator ever possibly corrupt the
ViewState? I've read that when controls are inserted dynamically in
between other controls that the ViewState can become out of sync
(something about controls that are added at design time being created
before dynamic controls). I assume this depends on when the control is
added (i.e., OnInit() or OnLoad()).

3) Will server-side validation still take place? When does the Page
object iterate through its collection of validators?

The control must extend System.Web.UI.Textbox, it can't be a .ascx
file or a composite control.

Has anyone done anything similar? I'd like to use the second approach,
but I'm unsure of how solid it is. Thanks in advance.

Nov 18 '05 #2

P: n/a
Thanks for the response, Peter. The reason I don't want to use a
UserControl is because this control is going to be part of a library
of custom server controls the developers at our small shop are going
to be using. The idea was to provide our own set of TextBox,
DropDownList, Button, etc. controls that we would use instead of the
..NET versions. Our controls would extend their .NET counterparts with
properties specific to our development environment, like for instance
the names of the Css classes we use. I was planning on building a
toolbox add-in for VS.NET with all of these controls in it.

Is adding the validator in the OnInit() method a bad idea? I've seen
code that creates run-time controls use OnInit(). Is there a reason to
use CreateChildControls() or OnPreRender() instead? Doesn't
server-side validation occur before the PreRender event?

I see your point about the possible drawbacks of folding validation
into the control. I suspect we'll be having more discussions about
this.

"Peter Blum" <PL****@Blum.info> wrote in message news:<eD**************@tk2msftngp13.phx.gbl>...
When I read this initially, I thought "a user control will do very well
here". All you need to do is expose properties on the TextBox control and
even the TextBox itself as properties of the User Control.
Then I read that you didn't want a UserControl. Can you clarify why not? If
you have an incorrect assumption, I'd like to correct it if it will save you
time.

Please don't rewrite Microsoft's validator code. Certainly feel free to
create new controls either from System.Web.UI.WebControls.BaseValidator or
implementing IValidator. I have already spend months creating an extensive
replacement to Microsoft's validation framework. You can check out
"Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx.

It does not merge a textbox with any validators within one control because
that imposes too many limitations on the user:
- there are many validators that the user wants
- there are all kinds of logic involved with each of the validators. Do you
want to expose all of the properties of the RangeValidator, a
CompareValidator, etc.?
- each validator has its own error message. Often the error messages need to
be customized to the page, such as including the field label.
- the formatting is an important aspect of validation. While you may want to
put the validators to the right of your textbox, some pages may require that
the validator goes below or elsewhere on the page.

Note that the TextBox control doesn't know how to handle having child
controls. It simply renders its on HTML for <input type=text>. You
definitely can add child controls to it. Use the CreateChildControls()
method or OnPreRender() method to add. However, since those controls aren't
rendered in the Render() method, you must override RenderControl. It should
call the ancestor RenderControl to handle the <input type=text> and then
call RenderControl on each of the elements in the Controls collection.

The ViewState only breaks if the list of controls created when the page was
first output changes as the page is recreated on post back. This is unlikely
in your case.

If you create Microsoft validators (or even use mine) within your control,
so long as they are added to the Controls collection of your control, they
will be initialized properly for client and server side validation.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.