|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWith the richness of .NET, I want more. I want web form controls in ASP.NET that have built in validation -- no messing with all the validation controls! You should be able to just create an So, I have struggled to figure out how to do this so you won't have to. Implementing the IValidator interfaceI delved into the SDK documentation and discovered that any control can be a page validator if it implements the using System;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace MyValidatingControls {
public class TextBox : System.Web.UI.WebControls.TextBox, IValidator {
private bool _valid = true;
private string _errorMessage = "";
public bool IsValid {
get { return _valid; }
set { _valid = value; }
}
public string ErrorMessage {
get { return _errorMessage; }
set { _errorMessage = value; }
}
public void Validate() {
}
}
}
Of course this doesn't do anything, but the code above fulfills the basic I did a little homework reading the SDK and it mentions that validators add themselves to the list during initialization. protected override void OnInit(EventArgs e) {
base.OnInit(e);
Page.Validators.Add(this);
}
protected override void OnUnload(EventArgs e) {
if (Page != null) {
Page.Validators.Remove(this);
}
base.OnUnload(e);
}
Finishing the "Setup"Before we can write our validator, I want to setup a few more helper items that will make things a little neater. I don't want to have to provide error messages based on each one of the validation issues. I will program those into the control, based on what type of data it should expect. Therefore, I need to provide a little more information to the control so that it can properly give error messages. I will add a property called private string _friendlyName = "";
public string FriendlyName {
get { return _friendlyName; }
set { _friendlyName = value; }
}
Last, before we write our validation, I want to update public bool IsValid {
get { return _valid; }
set {
_valid = value;
if (!_valid) {
this.BackColor = Color.LightCoral;
}
else {
this.BackColor = Color.White;
}
}
}
No blanks allowedFor our first validation, lets make it optional that the text field won't accept blanks. We need to make a property that can be set to "enable" this validation. private bool _blankAllowed = true;
public bool AllowBlank {
get { return _blankAllowed; }
set { _blankAllowed = value; }
}
Finally, we can write the public virtual void Validate() {
this.IsValid = true;
if (!this.AllowBlank) {
bool isBlank = (this.Text.Trim() == "");
if (isBlank) {
this.ErrorMessage =
String.Format("'{0}' cannot be blank.",
this.FriendlyName);
this.IsValid = false;
}
}
}
Expanding on the ideaNow that we have a basic text field with built-in validation, we can expand on the idea and create more interesting validating text box controls. The next simple expansion on the idea would be an Since we built the basic private int _minValue = Int32.MinValue;
private int _maxValue = Int32.MaxValue;
public int MinValue {
get { return _minValue; }
set {
_minValue = value;
if (_minValue > _maxValue) {
int swap = _minValue;
_minValue = _maxValue;
_maxValue = swap;
}
}
}
public int MaxValue {
get { return _maxValue; }
set {
_maxValue = value;
if (_minValue > _maxValue) {
int swap = _minValue;
_minValue = _maxValue;
_maxValue = swap;
}
}
}
Then, we update our public override void Validate() {
this.IsValid = true;
bool isBlank = (this.Text.Trim() == "");
if (isBlank) {
if (!AllowBlank) {
this.ErrorMessage = String.Format("'{0}' " +
"cannot be blank.", this.FriendlyName);
this.IsValid = false;
}
} else {
try {
_value = Int32.Parse(this.Text);
if (_value < this.MinValue) {
this.ErrorMessage = String.Format("'{0}' cannot " +
"be less than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}
if (_value > this.MaxValue) {
this.ErrorMessage = String.Format("'{0}' " +
"cannot be more than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}
} catch {
this.ErrorMessage = String.Format("'{0}' " +
"is not a valid integer.", this.FriendlyName);
this.IsValid = false;
}
}
}
public int Value {
get { return _value; }
set {
_value = value;
this.Text = _value.ToString();
}
}
ConclusionThat's about it! Just extend on this class and you can create a Before we had: <asp:TextBox id="Number" runat="server"/>
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="Number"
Text="'Number' cannot be blank." runat="server"/>
<asp:RangeValidator id="Range1" ControlToValidate="Number"
MinimumValue="0" MaximumValue="100"
Type="Integer" Text="The value must be from 0 to 100!"
runat="server"/>
Now we have: <MyControls:IntegerText id="Number"
FriendlyName="Number" MinValue="0" MaxValue="100"
AllowBlank="false" runat="server">
Mind you, my classes are far from finished nor does it provide the exact functionality as existing validation controls. One definite improvement would be to add client side scripting so that all validation doesn't happen on the server. But, for someone like myself who doesn't use Visual Studio .NET, this can save a lot of typing and setting of properties. I wanted everyone to see an example of how this is done, in case you are looking for the same kind of thing. Maybe in the future I will feel inspired enough to show you the completed set of classes I use to do this with. HistoryI noticed in the SDK that adding the validator should occur in
|
||||||||||||||||||||||