P
Pham Nguyen
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.
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.