ArticleDirectory
- 1.
- 2.
- 3.
- Evalluateisvalid
- Getcontrolvalidationvalue
Previous review
We have mentioned five official verification methods.
The following describes how to verify programming and extend your own verification controls.
Verify the Asp.net control programmatically
By default, when the page is sent back to the server, the ASPNET verification control automatically performs verification after the page initialization (that is, after the view viewstate and the returned data are processed) and before the call event, if the browser supports client scripts, the control can also be verified in the browser.
However, in actual development applications, the above situations may not meet your development needs. Sometimes you may need to perform dynamic execution verification in programming mode. In the following three cases, verification is performed by programming:
1.
The verification value is not set at runtime, and it may need to beProgramYou can dynamically set the parameters at runtime based on relevant conditions. For example, if you are using the rangevlidator verification control, you may need to dynamically set the maximum and minimum values of the range control during program execution based on the user input value or user identity. In this case, the default verification does not work, because when the page calls verification, the range verification control cannot specify its maximum and minimum values.
2.
You need to determine the control and validity in the page_load event handler. In the page processing phase, the verification control has not been called, so the isvalid attribute of the page or independent control is not set. If you try to obtain the value of this attribute, an exception is thrown. However, if you want to determine its validity, it is called programmatically.
3.
Editing controls during runtime is actually a convenient and flexible verification technology for verification by programming. Like other server control background programming, you only needCodeYou can set the property of the verification control.
Let's demonstrate the 3rd methods.
<Div>
<ASP: textbox ID= "Textbox1" Runat= "Server"> </ASP: textbox>
<ASP: rangevalidator ID= "Rangevalidator1" Runat= "Server" Controltovalidate= "Textbox1" enableclientscript = "false"> </ASP: rangevalidator>
<ASP: button ID= "Button1" Runat= "Server" Onclick= "Button#click" Text= "Button" />
</Div>
Event code:
Protected VoidButton#click (ObjectSender, eventargs E)
{
Rangevalidator1.maximumvalue ="50";
Rangevalidator1.minimumvalue ="40";
Rangevalidator1.type = validationdatatype. Integer;
Rangevalidator1.validate ();
If(! Rangevalidator1.isvalid)
Rangevalidator1.errormessage ="Must be between 40-50";
}
Result:
In this way, we can perform different verification based on different situations.
Develop your own text verification controls
We mentioned the basevalidator class before. It is an abstract class of all verification controls. It defines the basic functions of the verification control and provides core implementation for all verification controls. Therefore, you can create a new verification control by deriving a new control class from the basevalidator class. When using basevalidator, you must note the following two methods:
Evalluateisvalid
If the verified form field passes verification, this method returns true; otherwise, false. Generally, you must override this method when creating a custom verification control and call the getcontrolvalidationvalue () method in the override method to obtain the value of the verified control.
Getcontrolvalidationvalue
This method is used to obtain the value of the verified control.
After a brief understanding of basevalidator, we will use the basevalidator class to create a text verification control against SQL injection attacks. Baidu will not repeat this injection attack here.
Custom verification class
UsingSystem. Web;
UsingSystem. Web. UI. webcontrols;
UsingSystem. Web. UI;
UsingSystem. Text. regularexpressions;
NamespaceAspnetteach3
{
Public ClassStringvalidator: basevalidator
{
Private Static BoolValiddata (StringInputdata ){
If(RegEx. ismatch (inputdata, getregexstrings ()))
Return False;
Return True;
}
Private Static StringGetregexstrings ()
{
String [] Stringbadchar = { "And" ,"EXEC" , "Insert" , "Select" , "Delete" , "Update" , "Count" , "From" , "Drop" , "ASC" , "Char" , "Or" , "%" , ";" , ":" , "\'" , "\"" ,"_" , "CHR" , "Mid" , "Master" , "Truncate" , "Declare" , "Sitename" , "Net user" , "Xp_cmdshell" , "/Add" , "Exec master. DBO. xp_mongoshell" , "Net localgroup Administrators" };
StringStr_regex =".*(";
Foreach(VAR itemInStringbadchar)
{
Str_regex + = item +"|";
}
Str_regex = str_regex.substring (0, str_regex.length-1 );
Str_regex + =").*";
ReturnStr_regex;
}
Protected Override BoolEvaluateisvalid ()
{
String Value=This. Getcontrolvalidationvalue (This. Controltovalidate );
ReturnValiddata (Value);
}
}
}
ASPnet Page code
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "webform6.aspx. cs" inherits = "aspnetteach3.webform6" %>
<% @ Register tagprefix = "myvalid" namespace = "aspnetteach3" assembly = "aspnetteach3" %>
<!Doctype Html Public "-// W3C // dtd xhtml 1.0 transitional // en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html Xmlns= "Http://www.w3.org/1999/xhtml">
<Head Runat= "Server">
<Title> </Title>
</Head>
<Body>
<Form ID= "Form1" Runat= "Server">
<Div>
<ASP: textbox ID= "Textbox1" Runat= "Server"> </ASP: textbox>
<Myvalid: stringvalidatorRunat= "Server" ID= "Mystr"Controltovalidate= "Textbox1" Text= "Invalid character" />
<ASP: button ID= "Button1" Runat= "Server" Text= "Button" />
</Div>
</Form>
</Body>
</Html>
Result
Of course, we can also use the configuration file.
Through the configuration of the configuration file, we do not need to configure it on the page, just use it directly.
<Configuration>
<System. Web>
<Pages>
<Controls>
<Add Tagprefix= "Myvalid" Namespace= "Aspnetteach3" Assembly= "Aspnetteach3"/>
</Controls>
</Pages>
<Compilation Debug= "True" Targetframework= "4.0" />
</System. Web>
<System. Webserver>
<Modules Runallmanagedmodulesforallrequests= "True"> </Modules>
</System. Webserver>
</Configuration>
We can also make a DLL class library.
After making the class library, we can reference it through the designer like other net Dynamic Link Libraries.
Summary
We know how to customize the authentication.
Next time, we will explain how to use user controls.