ASP. NET laziness method 1

Source: Internet
Author: User

Why is this name required? Because I'm lazy, I'm really lazy, so I can leave a line left.CodeI don't want to write more code. For new companies and projects, the team lead has always said that my approach is a method of laziness, so let's record these ways of laziness. Maybe it is also a benefit of laziness :)

For B/S, data operations are often performed. Most operations include adding, modifying, deleting, and querying. Data lists are displayed on the query interface. The most common control is the datalist DataGrid repeater data binding control. Among them, repeater is the most commonly used, because it generates the least code and has a strong custom function. Most of the time, it is a Drag Control. Add the HTML style to the itemtemplate item, put literal where data needs to be displayed, and then write the data binding code in itemdatabound. Repeated work every day, especially when many projects need to be bound to the itemtemplate, it often gets bored. Is there any wayProgramDo these things for you by yourself?

In fact, it is easy to implement. Rewrite the Repeater control and let the itemdatabound event automatically traverse the controls in the itemtemplate, and then bind data according to the control name.

 Protected   Override   Void Onitemdatabound (repeateritemeventargs e ){ If (E. Item. itemindex> = 0 ){ Foreach (Control VaRIn E. Item. Controls ){ If (Var. ID! = Null ){ String TEXT = ""; If (E. Item. dataitem. GetType (). Name = "Datarowview" ) {Datarowview DR = (E. Item. dataitem As Datarowview ); If (Dr. dataview. Table. Columns. Contains (var. ID) TEXT = Dr [var. ID]. tostring ();} Switch (Controltype. Name ){ Case   "Literal" :( VaR As Literal). Text = text; Break ; Case   "Image" :( VaR As Image). imageurl = text; Break ;}}}} Base . Onitemdatabound (E );}

It's easy. You can add any control type in the switch according to your requirements. As long as the control ID in itemtemplate is the same as the name in dataset, it is automatically bound. If they are different, they don't matter. You can still perform secondary processing in Aspx.

However, there is a problem here. Only dataset can be used to process data sources. Currently, Orm is popular, and code generators are available in the background. Do you need to write code on the front end?
Now we can use our Asp.net tool. [Reflection] This is a great feature that allows us to do more things. In fact, the idea is similar to the next 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.