• ASP.NET控件Repeter的使用


    使用repeter控件,绑定数据源,能够省去在前台页面中拼接繁杂的for、foreach的时间,整个页面看起来也更加直观。常配合<select>标签、<table>标签使用。

    其中<itemTemplate>不断循环,<headerTemplate>和<footerTemplate>只执行一次,适用于<table>标签。

    后台后置类获取List<Model.Student> 这样一个对象,Model.Student中有 studentId 和 studentName 两个字段。

    //以下代码在page_Load方法中
    if (!IsPostBack)
                {
                    BLL.Student bllStu = new BLL.Student();
                    List<Model.student> stuList= bllStu.GetStudentList();
                     
                    //给repeater绑定数据源,记得dataBind()  
                    Repeater1.DataSource = stuList;
                    Repeater1.DataBind();  
                }            

    Select例子:

    绑定后把想要循环的内容写在<itemTemplate>标签内。

    1 <select name="stuDropDownList">
    2      <asp:Repeater ID="Repeater1" runat="server">
    3        <ItemTemplate>
    4          <option value="<%#Eval("studentId") %>"><%#Eval("studentName")%></option>
    5        </ItemTemplate>
    6      </asp:Repeater>
    7 </select>

    Table例子:

    一样要像上边的代码一样到后台给repeater控件绑定数据源。下面的例子展示了如何使用headerTemplate和footerTemplate

    <table border="1" cellpadding="0" cellspacing="0">
                <asp:Repeater ID="Repeater1" runat="server">
                    <HeaderTemplate>
                <
    tr><th>学号</th><th>姓名</th></tr> </HeaderTemplate> <ItemTemplate>
                <
    tr><td><%#Eval("studentId") %></td><td><%#Eval("studentName")%></td></tr> </ItemTemplate> <FooterTemplate> <!--这里可以放页码--> </FooterTemplate> </asp:Repeater> </table>

    下面附一段不用repeater,使用foreach循环来给select绑定数据的代码,感受一下区别。

    1  <select name="stuDropDownList">
    2         <%foreach (var stu in stuList)
    3         {%>
    4         <option value="<%=stu.studentId %>">
    5         <%=stu.studentName%></option>
    6         <%  } %>
    7 </select>

    代码量也不多,不过看起来就没有repeater清晰了。

  • 相关阅读:
    Programming Collecive Intelligence 笔记 Making Recommendations
    Managing Gigabytes文本压缩
    Hadoop The Definitive Guide 笔记二
    POS Tagging with NLTK
    MG查询
    MG索引构造
    对SharePoint 2010的job failover的一些比较深入的说明
    SharePoint 2010中Search功能的数据库连接字符串在哪里?
    记解决一个数据库删不掉的问题
    SharePoint升级失败?
  • 原文地址:https://www.cnblogs.com/2333/p/5071172.html
Copyright © 2020-2023  润新知