Asp. NET introductory thoughts of the Old lady _ practical Tips

Source: Internet
Author: User
Tags html tags string methods ticket
   The German Football Association warned fans who might go to Germany to watch the 2006 World Cup match to beware of fake tickets and not to buy tickets from non-distributors and scalper. The World Cup tickets are High-tech products, the tickets are implanted microchip, only through legal access to the fans to enter the ticket. The phenomenon of counterfeit tickets has been rampant in recent years, and the German Football Association hopes to prevent World Cup fans from becoming victims.

   ■ The Ming-Data availability

World Cup million people attention but tickets are limited, so some people fake buy fake, Allianz Stadium front gate before several ticket inspectors are essential, and our procedures are sometimes the same. When you receive a data sheet that accumulates for several years, and you find that only the contact phone has 23 formats, do you also lament that the system also needs to be validated by the inspector when the user submits the data? But we still find that content such as data validation is often overlooked, many systems often allow the guy with the bus ticket to infiltrate the stadium wildly, all kinds of data like the wild horse in the general March, not to say security, from the point of view of data effectiveness, a few years later it will be like mold to let the entire database rot, rubbish piles. But often there is another voice in the ear sounded: "To do so complicated, this system, or even the enterprise did not wait for mold to begin to work, it hung!" "

The application must ensure that the data validity of the system has two levels of meaning: first, format validation, to ensure that the data belong to the system design-defined type, scope, or even the specific format of the industry, which is usually done in the UI layer of the system to complete validation; second, logic verification, the data often meet the specific business logic rules, Data operations do not disrupt the logical relationship and cause system anomalies, which is usually done at the bottom of the system to complete validation. This paper mainly discusses the realization of the former.

But many systems often overdo it to keep data valid, the interface is flooded with complex input format restrictions of text input box, requiring users to spend a lot of time to do the self data correction, fill out a form than the Entrance examination box to make people more laborious, business users to do some, the most protest two also accepted, Internet users can not eat this set, to some creative!

   ■ the old ladies at the check-in-Validator Architecture

The Web application is based on the form to enter data. To ensure data availability, we have to test the input data on the server side, but in view of the user's feelings, we often insert some JavaScript scripts on the client. Client-side verification just like the nose when we breathe air, we block out the particulate matter and so on, which greatly relieves the burden of the lungs. Server-side validation is required to build secure Web applications, and only server-side validation can provide true application-level security. Double check, trivial boring, no wonder few people like.

. NET first the validator and verification object separated into two parts, verify the object exposure validation attributes, inspectors separate supervision of verification objects, the completion of data verification, more complex calibration can let several ticket inspectors together; Each page needs a lead, let her hold the list, In turn, each validator in the page is urged to validate the input fields, summarizing the results; the last page requires some controls to automatically trigger validation, which is typically played by a Button class control with a postback function, as shown in Figure 13-1.


server controls that participate in Web Forms validation must implement the IValidator interface, which is a common feature of the ticket inspectors. The Volidate method completes the verification process, obtains a result value IsValid attribute, contains the reason which rejects the release errormessage the attribute. The following are defined:

public interface ivalidator{
String Errormessage{set;get;}
BOOL Isvalid{set;get;}
void volidate{};
}

As the atomic trigger unit of the validation behavior, The page class sets the necessary members: the Page.validators collection is the validator container for the page, and the Page.validate method updates the respective IsValid properties by calling the Validate method of each validator in the container in turn, and the Page.valid property is used to summarize the checksum results of the page, and it Is the lock of the heart of the page.

The CausesValidation property is the public property that causes the postback control. When True, the control automatically invokes the Page.validate method before the Click event Processing, which makes it possible for us to complete the validation process without explicitly calling the Page.validate method.

   ■ Model-BaseValidator class and client-side validation

Although in ASP.net, any class that implements the Ivalidate interface can be considered to be a validator, in practical applications, we usually design the validator as a server control.

BaseValidator is the abstract base class derived from the Label class, in addition to implementing the IValidator interface, mainly performs the various common tasks required for validation, including extracting values from the validated object, defining the validation abstraction method, generating a client-side validation script, Complete registration in the Page.validators collection, and so on. By inheriting it, we can implement the custom validator by concentrating on the implementation of the server-side and the client's validation logic. The following example checks whether the CheckBoxList has more than 1 custom validator instances checked.

Using System;
Using System.Collections;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;

namespace essay. controls{
Custom validators derived from the BaseValidator base class
public class Requiredcheckboxlistvalidator:basevalidator {
Private ListControl M_listctl;
Enable client-side validation
Public Requiredcheckboxlistvalidator () {
Base. EnableClientScript = true;
}
Determines whether the validated control specified by ControlToValidate is valid
protected override bool Controlpropertiesvalid () {
Control WebControl = this. FindControl (ControlToValidate);
if (WebControl!= null) {
M_listctl = (ListControl) WebControl;
return true;
}
Else
return false;
}
Defining a checksum rule
protected override bool EvaluateIsValid () {
Return (M_listctl.selectedindex!=-1);
}
protected override void OnPreRender (EventArgs e) {
if (this. EnableClientScript) this. Generatescript ();
Base. OnPreRender (e);
}
Generate and inject client-side validation scripts to the page
protected void Generatescript () {
StringBuilder sb_script = new StringBuilder ();
Sb_script.append ("<script language=\" javascript\ ">\n");
Sb_script.append ("function Cb_vefify (val) {\ n");
Sb_script.append ("var isvalid=false;\n");
Sb_script.append ("var val = document.all[document.all[\" "+this.id+" \ "].controltovalidate];\n");
Sb_script.append ("var col = val.all;\n");
Sb_script.append ("if (col!= null) \ n");
Sb_script.append ("for" (i = 0; i < col.length; i++) \ n ");
Sb_script.append ("If" (Col.item (i). TagName = = \ "input\") \ n ");
Sb_script.append ("If" (Col.item (i). checked) Isvalid=true; \ n ");
Sb_script.append ("return isValid;");
Sb_script.append ("}\n");
Sb_script.append ("</script>");
This. Page.registerclientscriptblock ("Rblscript", sb_script.tostring ());
Specify its client-side validation logic by setting the Evaluationfunction attribute of the validator
This. attributes["evaluationfunction"] = "cb_vefify";
}
}
}

   ■ I'm ugly, but I'm capable.-Regular expression (Regular Expression)

Regular expressions are used to describe a particular format of string methods, can be concise, geometric multiple to improve the efficiency of text processing, but it is not a pain so annoying, because it is not the concept of how advanced, mainly it is too ugly! Whether a string conforms to a particular format of a string depends on whether it conforms to multiple matching rules in this particular format.

The key to understanding regular expressions is to refine the three elements of each matching rule: a subset of characters, the number of repetitions, and the position. Example string "Two digits at the beginning of the three-digit English letter End", before writing regular expressions, we need to change the sentence pattern: "The beginning number {2 bits} any character {0 or more bits} letter {3-bit} end", the regular expression is "^ [0-9]{2} [\s\s]* [A-za-z]{3} $]."

In each matching rule, the possible character subcode string has a range, a fixed subset of characters, and regular expressions represent the set with constants, regions, and logical operations, as in Figure 13-2, which is noteworthy as the partial character has been defined infinitesimal characters with special meaning.


In each matching rule, a subset of its characters may be duplicated, which can be expressed in two ways: one follows the character subset followed by a curly brace, an inner number, and the other in a meta character, as in Figure 13-3. The position of a subset of characters in each matching rule is determined by its position in the regular expression, the entire string begins with "^", and the end is represented by "$".


In many cases, two or more matching rules may be used to use the same subset of characters, such as HTML tags, where the label declaration should be a subset of the same character, such as "<div>......</div>." If you want to define a subset of characters for subsequent references, you can use the parentheses to identify them by using the "\ Number" reference.

Regular expressions are widely used, in addition to data validation, UBB Forum, page dynamic static, search technology and so often appear its figure, "I am ugly but I am very capable", here we just discuss the expression of the writing method. Finally, two examples are summarized, as shown in Figure 13-4.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.