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! :-)