Using the Repeter control, binding the data source eliminates the clutter of the for, foreach time in the foreground page, and the entire page looks more intuitive. <select> tags, <table> tags used.
Where <itemTemplate> constantly loops,
Background backend class gets an object such as list<model.student>, with StudentID and studentname two fields in Model.student.
// The following code is in the Page_Load method if (! IsPostBack) { new BLL. Student (); List<Model.student> stulist= bllstu.getstudentlist (); // bind data source to Repeater, remember DataBind () Repeater1.datasource = stulist; Repeater1.databind (); }
Select Example:
After binding, write what you want to loop in the <itemTemplate> tab.
1 <Selectname= "Studropdownlist">2 <Asp:repeaterID= "Repeater1"runat= "Server">3 <ItemTemplate>4 <optionvalue= "<% #Eval ("StudentID ")%>"><%#Eval("Studentname")%></option>5 </ItemTemplate>6 </Asp:repeater>7 </Select>
Table Example:
The same as the code above to the background to the Repeater control to bind the data source. The following example shows how to use HeaderTemplate and FooterTemplate
<TableBorder= "1"cellpadding= "0"cellspacing= "0"> <Asp:repeaterID= "Repeater1"runat= "Server"> <HeaderTemplate>
<TR><th>School Number</th><th>Name</th></TR> </HeaderTemplate> <ItemTemplate>
<TR><TD><%#Eval("StudentID") %></TD><TD><%#Eval("Studentname")%></TD></TR> </ItemTemplate> <FooterTemplate><!--Here you can put the page number--</FooterTemplate> </Asp:repeater> </Table>
The following is a paragraph without repeater, use the Foreach loop to give the select the code to bind the data, feel the difference.
1 <Selectname= "Studropdownlist">2 <%foreach (Var stu in stulist)3 {%>4 <optionvalue= "<%=stu.studentid%>">5 <%=Stu.studentname%></option>6 <% } %>7 </Select>
The amount of code is not too much, but it doesn't look repeater clear.
Asp. NET control Repeter use