Create a TextBox control introduction with self-verification feature in front of powerful .NET, I have a lot of ideas, I hope that there is an authentication feature in the ASP.NET web control, and there is no need to insert too much verification control in the page. confusion! Now we will be able to use the .NET to create a TextBox control that allows you to enter integer numbers or only the TextBox controls that allow the input currency data, and so on. Of course, you can also specify whether the TextBox control is empty or whether it is within a range, wait, and other, when the data we entered meets the requirements, you can also return a value, so that we feel whether we want to intervene, and even us When it is running, it is finally achieved according to the input data, and I finally realize the above ideas.
Implementation IVALIDATOR Interface I found that any control can be verified as if the IValidator interface is implemented, and any controls can be verified. The following is a simple example of inheriting the TextBox control: use 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 program does not do, but it fully implements the architecture of the basic Ivalidator interface (at least it can be successfully compiled), I use the private keyword to create two fields (Field) To save verification status and error message, in order to ensure that the verification control can be executed, we must add our validation control to the validation control collection in the page. I found that when I read the SDK documentation, the verification control is loaded in the initialization. The IValidators interface is mainly used to register themselves, so we need to use overworked methods to re-implement Oninit and ONUNLOAD events so that we can be from the page. Verify that add or delete them protected override void oninit (Eventargs E); page.validators.add (this);}
Protected Override Void ONUNLOAD (Eventargs E) {IF (page! = null) {page.validators.remove (this);} Base.onload (e);} Finish settings before we implement our verification function, in order to make the event more Simple, I set some help items, because I don't want to separately provide an error message for verify controls separately, but I want to embed them into the control, so that we expect the formatted data entry, so I need to do something , Make it an error prompt appears appropriate. I will add a referlyname property, which will appear in all error prompt information to prompt the user legal data type, so if we call the control ID is RetailPrice, we will make the control's RiendlyName is Retail Price String _FriendlyName = ""; public string friendlyname {get {_friendlyname;} set {_friendlyname = value;}} Finally, we rewrite the IsValid event so that it can change the background color of the control when verify that it is not passing the background color public bool isvalid {Get {Return _valid;} set {_valid = value; if (! _valid) {this.backcolor = color.lightcoral;} else {this.backcolor = color.White;}}}}
Do not allow spaces to first we need to determine, provide an option to determine if it is allowed to be empty, we need to create a property here to determine if it can be empty private bool _blankallowed = true;
Public Bool ALOWBLANK {Get {Return _blankAllowed;} set {_blankallowed = value;}} Finally, we can rewrite the verification function publicual 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 our creativity Now we have created a TextBox control for built-in basic authentication. Now we can extend our ideas. Create more interesting TextBox controls under the TextBox control with specific verification capabilities to create a full-time data. The TextBox control (IntegertextBox), and makes the control only allows the input data to be entered must be within a certain range, but we still need to consider whether to allow null values, so as above, you need to add an attribute to the basics we created above. On the basis of the TextBox control, we only need to inherit the control, then override the validate function and add some new attributes private int _minvalue = int32.minvalue; private int _maxvalue = int32.maxvalue; public int minvalue {get {returnvalue;} 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;}}} We then expanded the Validate function, and compile It is a local code 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.issalid = false;}} else {TRY {_Value = int32.parse (THIS.TEXT); if (_Value