|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
OverviewI believe that everyone has experienced filling in a form. If there are errord, it is annoying to find them. You know the error but you cannot find it right away. Highlighting an error field is the most preferred method for the end user. I did remember I created a similar solution in ASP before, but it required a lot of code to handle this eye-catching solution. IntroductionThe built-in ASP.NET Validation Summary Control is good. It is easy for us to show a summary of errors in a form. However, it is difficult to search the invalid entries if we have got too many fields in a form. The best solution is to highlight the field if it is invalid. Recently, I developed this solution and would like to share it with you. The codeFirst of all, we need to create a control class to override the Validation Summary control. public class SmartValidationSummary :
System.Web.UI.WebControls.ValidationSummary
The [
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("CSS class name applied to the invalid control.")
]
public string ErrorCssClass
{
get
{
object o = ViewState["ErrorCssClass"];
if (o != null)
return (string) o;
else
return String.Empty;
}
set
{
ViewState["ErrorCssClass"] = value;
}
}
On the pre-rendering process, we need to search through the form for the protected override void OnPreRender(EventArgs e)
{
foreach(Control ctlForm in Page.Controls)
{
if (ctlForm.GetType() == typeof(HtmlForm))
{
ControlCollection ccForm = ctlForm.Controls;
foreach(Control ctlItem in ccForm)
{
if (ctlItem.GetType() == typeof(RequiredFieldValidator))
ProcessRequiredFieldValidator(ctlItem);
}
}
}
base.OnPreRender (e);
}
If the private void ProcessRequiredFieldValidator(Control ctlItem)
{
RequiredFieldValidator vdrItem = (RequiredFieldValidator)ctlItem;
if (vdrItem.ControlToValidate != string.Empty)
{
string strInvalid = vdrItem.ControlToValidate;
Control ctlToValidateItem = Page.FindControl(strInvalid);
if(ctlToValidateItem.GetType() == typeof(TextBox))
ProcessTextBox(ctlToValidateItem, vdrItem.IsValid);
}
}
If there is an error in the private void ProcessTextBox(Control ctlToValidateItem, bool boolIsValid)
{
TextBox txtItem = (TextBox)ctlToValidateItem;
if (boolIsValid)
{
if (ViewState[txtItem.ClientID + "CssClass"] != null)
{
txtItem.CssClass = ViewState[txtItem.ClientID + "CssClass"].ToString();
ViewState[txtItem.ClientID + "CssClass"] = null;
}
}
else
{
if (txtItem.CssClass != this.ErrorCssClass)
ViewState[txtItem.ClientID + "CssClass"] = txtItem.CssClass;
txtItem.CssClass = this.ErrorCssClass;
}
}
Installing the controlAll you have to do is unzip the source file and copy the smart.dll file to your project \bin\ folder. Using the control
Note: If you still have difficulties to implement this solution, please refer to my demo project. LimitationAs this is a server-side only control, all validators using the smart Validation Summary must not be set to use the client script. Further EnhancementsThere are lots of enhancements required for this project. Following is my expectation:
History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||