Asp. NET control Repeter use

Source: Internet
Author: User

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

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.