|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWhen writing ASP.NET pages that need to be validated before submission, I find myself coming across controls that only need to be validated if another control contains a particular value. A classic example of this is an "If other, please specify" field. Typically, you'll have a combobox that contains a list of choices, and at the end of the list is an "Other" item. If the user chooses "Other", you'd like them to specify exactly what "Other" means. My previous approach was to include a JavaScript method that would make sure the "please specify" field was not empty if the combobox had the "Other" item selected. I would wire up the method to the To fix the problem, I've created a new validation control that inherits directly from BackgroundI'm going to assume some familiarity with the ASP.NET validation controls since this isn't a tutorial on what they are or how they work, but rather an implementation of a custom one. With that said, let's get started. Introducing the ControlMy validator adds a couple of properties to the
The The This may be a little confusing all at once, but hopefully I'll clear it up by the end of the article. Important MethodsFor custom validators, the most important method that you'll be overriding is the For the private bool CheckCompareCondition()
{
bool isRequired = false;
string compareValue = this.GetControlValidationValue(ControlToCompare);
if (compareValue == TriggerValue)
{
isRequired = true;
}
return isRequired;
}
As you can see, I'm grabbing the validation value of the string controlValue;
controlValue = this.GetControlValidationValue(this.ControlToValidate);
if (controlValue == null)
{
return true;
}
else
{
return controlValue.Trim() != this.InitialValue.Trim();
}
Client-side validation is handled by injecting some JavaScript into the page by overriding the function RequiredIfValidatorEvaluateIsValid(val)
{
if (val.controltocompare != "")
{
if (document.getElementById(val.controltocompare).value
== val.triggervalue)
{
//use asp.net's method!
return RequiredFieldValidatorEvaluateIsValid(val);
}
else
{
return true;
}
}
else
{
return true;
}
}
Here, I'm doing basically the same thing as my server-side When looking at the JavaScript method, you may be wondering how I'm able to use For my implementation, I do the following: base.AddAttributesToRender(writer);
if (this.RenderUplevel)
{
//this attribute is needed by the RequiredFieldValidator's
//validation method.
writer.AddAttribute("initialvalue", InitialValue);
//this is the method that asp.net calls when validating my
//control on the client-side
writer.AddAttribute("evaluationfunction",
"RequiredIfValidatorEvaluateIsValid");
//attributes that my client-side method will use to validate itself.
writer.AddAttribute("controltocompare",
this.GetControlRenderID(ControlToCompare));
writer.AddAttribute("triggervalue", TriggerValue);
}
Here, you can see I'm adding the attributes that my JavaScript method uses when validating on the client-side. The really important one is the One other thing worth mentioning is that this method is the reason why I didn't inherit my class from I hear you saying, "Well, then just don't call the base class ConclusionHopefully, this article has given you a closer look into the internals of a validation control (maybe closer than you wanted!), but most importantly, I hoped you've learned something new after reading this. At the very least, I hope you'll be able to put this control to good use. Using the codeThe source code is only one file, RequiredIfValidator.cs. To build the control, you can run the following from a command line: csc /t:library RequiredIfValidator.cs
Once compiled, you can throw the DLL in your web application's bin directory. Then, when you want to use the control in one of your aspx pages, just include the following directive in your page: <%@ Register TagPrefix="BNewtz" Namespace="BNewtz.Controls"
Assembly="RequiredIfValidator" %>
Using the demoThe demo is just a simple webpage that shows the functionality of the control. The demo contains the demo.aspx page as well as the compiled DLL of the control. Just throw the demo.aspx page in a virtual directory, and the DLL into the bin subdirectory of your application. Once that's done, just view demo.aspx in your web browser of choice. Points of InterestFor those of you curious to know how I was able to know what was going on in Microsoft's own controls, the answer is Anakrino. Anakrino is a decompiler that converts MSIL back into C#. It's a great tool to uncover the inner workings of a class, especially if it's one that you're deriving from. History
|
||||||||||||||||||||||