Asp.net uses the repeater loop to add a corresponding set of controls. It is mainly used for dynamic and repeated post data from the background to the foreground.CodeAnd can add any number of duplicate modules in the background. This method is often used in actual projects. This article briefly introduces the repeater structure and usage.
For example, if we want to implement the following style, of course, the data is obtained from the background, and the number of data rows is variable.
First, define the data structure: (the defined data structure should include the structure shown at the front end as much as possible)
Public Class Evaluation { Private String Productid; Private String Rating; Public Evaluation (String Productid, String Rating ){ This . Productid = Productid; This . Rating = Rating ;} Public String Productid { Get { Return Productid ;}} Public String Rating { Get { Return Rating ;}}}
Background processing method:
Public Partial Class Testrepeatorcontrol: system. Web. UI. Page { Protected Void Page_load ( Object Sender, eventargs e ){ If (! Ispostback) {arraylist values = New Arraylist (); values. Add ( New Evaluation ( " Razor wiper blades " , " Good " ); Values. Add ( New Evaluation ( " Shoe-so-soft softening Polish " , " Poor " ); Values. Add ( New Evaluation ( " Dynasmile dental fixative " , " Fair " ); Repeater1.datasource = Values; repeater1.databind ();}} Protected Void Repeaterincluitemdatabound ( Object Sender, repeateritemeventargs e ){ // This event is raised for the header, the footer, separators, and items. // Execute the following logic for items and alternating items. If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem ){ If (Evaluation) E. Item. dataitem). rating = " Good " ) {(Label) E. Item. findcontrol ( " Ratinglabel " ). Text = " <B> *** good *** </B> " ;}}}}
Here we can see that the repeater data source requires an object instance that implements the ienumerable interface, for example, the arraylist object instance is used here.
The repeaterincluitemdatabound method can be used to specially process cyclic items.
Finally, let's look at the front-end code implementation:
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Testrepeatorcontrol. aspx. CS " Inherits = " Ericsunwebappproject. testrepeatorcontrol " %> <! Doctype html > < Html Xmlns = "Http://www.w3.org/1999/xhtml" > < Head Runat = "Server" > < Title > </ Title > </ Head > < Body > < Form ID = "Form1" Runat = "Server" > < Div > < ASP: Repeater ID = "Repeater1" Runat = "Server" Onitemdatabound = "Repeaterincluitemdatabound" > < Headertemplate > < Table Border = "1" > < Tr > < TD > < B > Product</ B > </ TD > < TD > < B > Consumer rating </ B > </ TD > </ Tr > </ Headertemplate > < Itemtemplate > < Tr > < TD > < ASP: Label ID = "Label1" Text = '<% # Databinder. eval (container. dataitem, "productid") % > 'Runat = "server"/> </ TD > < TD > < ASP: Label ID = "Ratinglabel" Text = '<% # Databinder. eval (container. dataitem, "rating") % > 'Runat = "server"/> </ TD > </ Tr > </ Itemtemplate > < Footertemplate > </ Table > </ Footertemplate > </ ASP: Repeater > </ Div > </ Form > </ Body > </ Html >
Here we can clearly see that the repeater has three structures: headertemplate, itemtemplate, and footertemplate.
When the data source is traversed and displayed, the duplicate part is only the part contained in the itemtemplate. Only one portion of headertemplate and footertemplate is displayed. In this way, we can handle the situation of "beginning and end are special and the content is duplicated.
Here, we cyclically process Tr and TD. If we want to cyclically process custom controls or more complex controls (such as Treeview ), you can place the control that we need to repeat in itemtemplate to replace Tr and TD here.
Add msdnArticle:
Http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx