ASP. NET laziness method 2

Source: Internet
Author: User

As mentioned in the previous article, Orm is popular and there areCodeGenerator. Do I have to write code on the front-end? Today, let's talk about the class that uses the reflection mechanism to automatically bind the data persistence layer!

 

Protected   Override   Void Onitemdatabound (repeateritemeventargs E)
{
Foreach (Control VaR In E. Item. Controls)
{
Type TT = E. Item. dataitem. GetType ();
System. reflection. propertyinfo _ proinfo = TT. getproperty (var. ID );
  If (_ Proinfo ! =   Null )
{
Text = _ Proinfo. getvalue (E. Item. dataitem, Null ) =   Null   ?   String . Empty: _ proinfo. getvalue (E. Item. dataitem, Null ). Tostring ();
}
}
}

In fact, the core of this Code is to first determine the type of the bound object class. Then, the Control ID is used to find the property with the same name in the corresponding object class and obtain its value.

In fact, this is too simple to write. But here is a tip. If a person ID is displayed in most projects, it is automatically converted to a person name and a page is displayed when you click the mouse. At this time, you can write an interface to achieve code reuse.

Public InterfaceIcustomitem
{
ObjectDataitem {Get;Set;}
}

Then add

 
If(VARIsIcustomitem)
{
(VARAsIcustomitem). dataitem=E. Item. dataitem;
}

If you implement this interface in a specific project, the repeater will automatically bind and display data as required.

By the way: in fact, girdview can also implement automatic binding, but I always think that girdview is too large to be used as a Web Front-end. So I like the lightweight repeater.

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.