Asp.net 通過Repeater迴圈添加對應的一組控制項

來源:互聯網
上載者:User

Asp.net 通過Repeater迴圈添加對應的一組控制項,主要用於後台向前台 動態 重複的post資料時,能夠很好的將前台代碼的重複部分 歸一,並且能夠通過後台添加任意數量的重複模組。在實際項目中經常會用到這樣的處理方法。此文主要簡單的介紹一下Repeater的結構與使用方法。

例如我們想要實現如下的樣式,當然了,資料是從後台擷取到,資料的行數是不定的。

首先定義資料結構:(定義的資料結構盡量要包含前台顯示的結構)

    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;            }        }    }

 

之後處理後台方法:

    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 Repeater1_ItemDataBound(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>";                }            }        }    }

這裡可以看出 repeater 的資料來源需要一個實現了 IEnumerable 介面的對象執行個體,例如此處用的是 ArrayList 的對象執行個體。

而Repeater1_ItemDataBound方法,則可以對迴圈的Item進行特殊處理。

 

最後來看前台代碼實現:

<%@ 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="Repeater1_ItemDataBound">                                <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>

 這裡可以清晰的看到repeater有三段結構:HeaderTemplate、ItemTemplate 和 FooterTemplate。

 那麼在進行資料來源遍曆顯示的時候,重複的部分只是ItemTemplate所包含的部分。 HeaderTemplate 和 FooterTemplate 所包含的部分只顯示一份。這樣就很好的能夠處理:“首、尾 特殊,內容重複” 的情形。

這裡迴圈處理的是tr,td,如果我們想要迴圈處理自己定義的控制項,或者比較複雜的控制項(比如 TreeView 等),那麼可以將我們需要重複的控制項放在ItemTemplate 中來替換此處的tr,td。

 

附加msdn相關文章: 

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.