Many of the users had asked for on the fly validation on a dynamically loaded asp.net form in a multi-tenet application. This is accomplished using custom validator and javascript. Pretty neat architecture so I decided to post it for the benefit of my colleagues
and friends.
Problem -
In the donations management application at Aidmatrix, we use dynamic controls on the asp.net forms. It did not support on-the-fly validations based on the user input. This effects the usability eg. the user has to enter all the required data fields even though
it is not applicable based on his input. The other downside is this results in incorrect data since validation did not happen at runtime - eg. departure date for a travel is later than the
return date.
Background of the architecture that dynamically loads the controls on the page -
The control definition is all stored in the database and a class library with an interface handle the display of all type of controls on the asp.net page.
Database definition includes layout (caption, row, col, span, visibility etc), css, control type(text, dropdown, check box etc), databindings, validations (required, range, date) and various attributes, extender controls etc. Asp.net page retrieves the defition
of the controls on the page based on the role and certain other business criteria. All the css, layout and validations are applied on the controls as it would happen at design time but this is at run time.
When the user submits the form, data in the controls is collected from the properties using reflection and entered in the database. Key thing here is to use the unique ID of the control and hence it is very important to name the controls. we use prefixes to
handle the form name, section name, type of control (tb for text box, cb for combo box) etc. The more detailed the better for troubleshooting etc.
Now, how the dynamic custom validation works?
Step 1 - add a non-visual custom validator control
A new custom validator control is added to the asp.net form (defined in the database). This would be a non-visual control. The new custom
validator is implemented in the class library by setting up the required attributes defined in the database. This mainly includes controlToValidate,
clientValidationFunction, Error Message, ValidationGroup etc.
Step 2 - register the client javascript at run-time Container.Page.ClientScript.RegisterClientScriptBlock(Page.GetType,
"genxClient",
Field.ValidationMessage_en)
Working solution -
Departure date and a return date can be compared
now to make sure they are valid. Any field can be made mandatory based on the
user’s input in another field on the same form at run time.
I'd like to know your actual solution on adding the "non-visual" custom validator control to an asp.net page that has dynamically added controls. I am facing the same situation where I am able to dynamically add custom controls of different kinds based on
configuration settings on the database side to an asp.net page but not able to come up with a good solution to add validation controls to these dynamically added server controls.
It would be really great if you could post your solution on adding custom validation controls to work with dynamically added controls.
How did you implement the dynamic custom validator? I am trying to do something similar (load the validation error messages from the database and create the validation controls at runtime).
Please provide me additional details on how you implemented it or please email me at
kprashu@hotmail.com
teamdotnet
0 Points
1 Post
Dynamic custom validation in ASP.NET
Nov 09, 2011 07:22 PM|LINK
Many of the users had asked for on the fly validation on a dynamically loaded asp.net form in a multi-tenet application. This is accomplished using custom validator and javascript. Pretty neat architecture so I decided to post it for the benefit of my colleagues and friends.
Problem -
In the donations management application at Aidmatrix, we use dynamic controls on the asp.net forms. It did not support on-the-fly validations based on the user input. This effects the usability eg. the user has to enter all the required data fields even though it is not applicable based on his input. The other downside is this results in incorrect data since validation did not happen at runtime - eg. departure date for a travel is later than the
return date.
Background of the architecture that dynamically loads the controls on the page -
The control definition is all stored in the database and a class library with an interface handle the display of all type of controls on the asp.net page.
Database definition includes layout (caption, row, col, span, visibility etc), css, control type(text, dropdown, check box etc), databindings, validations (required, range, date) and various attributes, extender controls etc. Asp.net page retrieves the defition of the controls on the page based on the role and certain other business criteria. All the css, layout and validations are applied on the controls as it would happen at design time but this is at run time.
When the user submits the form, data in the controls is collected from the properties using reflection and entered in the database. Key thing here is to use the unique ID of the control and hence it is very important to name the controls. we use prefixes to handle the form name, section name, type of control (tb for text box, cb for combo box) etc. The more detailed the better for troubleshooting etc.
Now, how the dynamic custom validation works?
Step 1 - add a non-visual custom validator control
A new custom validator control is added to the asp.net form (defined in the database). This would be a non-visual control. The new custom
validator is implemented in the class library by setting up the required attributes defined in the database. This mainly includes controlToValidate,
clientValidationFunction, Error Message, ValidationGroup etc.
Step 2 - register the client javascript at run-time
Container.Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "genxClient", Field.ValidationMessage_en)
Working solution -
Departure date and a return date can be compared
now to make sure they are valid. Any field can be made mandatory based on the
user’s input in another field on the same form at run time.
Pekanbaru
Member
4 Points
4 Posts
Re: Dynamic custom validation in ASP.NET
Nov 29, 2011 04:53 PM|LINK
thanks sir... I am a beginner so petrified.... :)
..:: Ban Terbaik di Indonesia GT Radial ::..
guozhang
Member
2 Points
3 Posts
Re: Dynamic custom validation in ASP.NET
Jan 31, 2012 06:01 PM|LINK
Hi,
I'd like to know your actual solution on adding the "non-visual" custom validator control to an asp.net page that has dynamically added controls. I am facing the same situation where I am able to dynamically add custom controls of different kinds based on configuration settings on the database side to an asp.net page but not able to come up with a good solution to add validation controls to these dynamically added server controls.
It would be really great if you could post your solution on adding custom validation controls to work with dynamically added controls.
Thanks.
kprashu
Member
2 Points
1 Post
Re: Dynamic custom validation in ASP.NET
Mar 29, 2012 05:42 PM|LINK
How did you implement the dynamic custom validator? I am trying to do something similar (load the validation error messages from the database and create the validation controls at runtime).
Please provide me additional details on how you implemented it or please email me at kprashu@hotmail.com