Use reflection to implement bidirectional binding between ASP. NET controls and data entities, and automatically verify the input content on the client.

Source: Internet
Author: User
First of all, please forgive me for my poor Chinese. I hope you will understand it.

ASP. net UI is not as easy as winform, so setting a datasource and datafield will do everything. although binding in the form of <% # %> is provided, this binding is unidirectional and can only be assigned to the control from the data source. The control content cannot be written back to the data source. therefore, when saving the data, you have to write it one by one. You have to process null, type conversion, or something. for example
Anobject. anintproperty = convert. toint32 (acontrol. Text = ""? Acontrol. Text: "0"); // if you use a data entity
Or
Adatarow ["anintfield"] = convert. toint32 (acontrol. Text = ""? Acontrol. Text: "0"); // use datarow
Sometimes dozens of fields in a form can be written as crazy >_<

So I used reflection to implement a display of data entities for controls (I prefer O-R Mapping instead of dataset/datarow, and of course datarow can also be done in a similar way) and the control content is assigned back to the data entity, and then, by the way, the data legality verification is also done (reflection performance loss is high, do not do more things in the mind is not balanced)

In my bindingmanager class, all controls ending with "_" will be processed, and these controls will be automatically associated with the attributes corresponding to the object class. by setting the prefixlength value, specify the number of characters in the control name to match the object class attributes. For example, if I used to use three characters as the control prefix, set prefixlength to 3, the name of the control associated with the name attribute is like txtname _. call getentityvalues (page, theobject) and setentityvalue (page, theobject) to bind controls to data entities.

As for the legitimacy verification function of "by the way", the system first ensures that the input content of the control conforms to the object attribute type, which is automatically completed. For example, a control corresponding to an int attribute cannot enter letters. if you have more requirements, such as using regular expressions, do not allow null values, or give users more detailed prompts when an error occurs, you can add validateattribute to the object attributes.

This is part of a demo I wrote: The person class, which has various types of attributes. it can be seen that the name specified by validateattribute cannot be blank and is described as "name". In this way, when txtname _. when text is null, the system will prompt "name cannot be blank ". the ID specifies the format of the new ID card to be entered. If not, the system prompts "Incorrect ID card number format ". in addition, sex is a custom class. you must create a typeconverter for the sex class, and then add the typeconverterattribute to the sex class before it can be correctly processed.

Public   Class Person
{
Public Person ()
{

}

String _ Name;
Decimal _ Height;
Datetime _ birthday;
Sex _ sex;
[Validate (allownull =   False , Description =   " Name " )]
Public   String Name
{
Get   {Return_ Name ;}
Set   {_ Name=Value ;}
}
[Validate (Description =   " Height " )]
Public   Decimal Height
{
Get   {Return_ Height ;}
Set   {_ Height=Value ;}
}
String _ Id;
[Validate (RegEx =   " ^ \ D {17} (\ d | x) $ " , Description =   " ID card number " )]
Public   String ID
{
Get   {Return_ Id ;}
Set   {_ Id=Value ;}
}

Public Datetime birthday
{
Get   {Return_ Birthday ;}
Set   {_ Birthday=Value ;}
}

Public Sex
{
Get   {Return_ Sex ;}
Set   {_ Sex=Value ;}
}
}

When using the person class, one row of statements can assign values between controls and data. The following is An ASPX background. Code Segment:
Person P =   New Person ();
Bindingmanager BM =   New Bindingmanager ();
Private   Void Page_load ( Object Sender, system. eventargs E)
{

If ( ! Ispostback)
{
// Validateform () is a function called when the client verifies the validity of the control.
Button1.attributes [ " Onclick " ] =   " Return validateform (); " ;
// Set a random value for P.
P. Name =   " Yok " ;
P. Height =   170 ;
P. Sex = Sex. Male;
P. Birthday =   New Datetime ( 1980 , 11 , 21 );
// This allows the control to display P content.
BM. getentityvalues (page, P );
}
}

Private   Void Button#click ( Object Sender, system. eventargs E)
{
// Assign value to entity
BM. setentityvalues (page, P );
// At this time, P has obtained the value of each attribute from the control. Write to check whether it is correct.
Response. Write ( " Name = "   + P. Name +   " <Br> " );
Response. Write ( " Height = "   + P. Height +   " <Br> " );
Response. Write ( " Birthday = "   + P. Birthday. tow.datestring () +   " <Br> " );
Response. Write ( " Id = "   + P. ID +   " <Br> " );
Response. Write ( " Sex = "   + P. Sex. tostring () +   " <Br> " );
}

No, my code should be better understood than my text.

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.