Asp. The encapsulation and application of data checkout part in net

Source: Internet
Author: User
Asp.net| Package | data

Our business system involves more forms to edit and verify, the simplest way is to use the ASP.net data validation control, but this has the following 3 questions:

1 Check control to add to the form at design time, and manually set: data type, error information and other attributes, more cumbersome.

2 for an input box, we may want to check multiple items, such as: required, must be date format, and more than 2007-1-1. This makes it necessary to add multiple validation controls.

3 Business rules and forms are bound together and are not conducive to maintenance and reuse.

The verification features we want are:

1 validation rules and form separation, the form of the input box can be drawn, the validation control at run time, according to the configuration file dynamic creation.

2 according to the validation rules, generate the default: cannot be null, wrong number type, must be between 1 to 100 such a hint information.

3 Flexible Validation Rule expressions: Range (1), >=0.5, Mail (), Mobile (). Not currently

Conditions such as and, or, but easy to expand.

For this we encapsulate this part of the function, when used as long as:

Set in XML file: Control name, data type, whether required, validation expression information.

<ValidateInfo>
<ControlName> Outlet Quantity </ControlName>
<DataType>Integer</DataType>
<Require>true</Require>
<expression>range (1) </Expression>
</ValidateInfo>

In the page Page_Init event, call the class method:
Validatehelper.loadfromfile ("Data validation _ Configuration. xml").

Class Design:
Configuration Information Class
Class Validateinfo
{
public string controlname;
Public ValidationDataType DataType;
public string errormessage;
public bool Require;
public string Expression;
}

Validation control Create factory
public class Validatorfactory
{
Createrequiredfieldvalidator ();
Createdatetypecheckvalidator ();
Createrangevalidator (string min, string max);
Createcomparevalidator (ValidationCompareOperator oper, string valuetocompare);
...
}

Validation rule parser, creating a validation control based on configuration information
public class Validatorparser
{
Parse (validateinfo info, validatorfactory factory);
...
}

Read the configuration information and bind the validation controls to the form
public class Validatehelper
{
LoadFromFile (Page page, string fileName)
}

The attachment is sample code and documentation: Click here to download the source file



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.