欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Repeater控件綁定的三種方式

 更新時(shí)間:2013年05月06日 11:25:08   作者:  
Repeater 控件用于顯示重復(fù)的信息,這些信息被綁定在該控件上。一般項(xiàng)目中經(jīng)常出現(xiàn)三種使用方式

方式一
在aspx頁(yè)面,寫(xiě)好需要循環(huán)輸出的內(nèi)容,一般包含用戶(hù)自定義控件、服務(wù)器控件、Html格式的片段、和<%# Eval("Name")%>這種方式來(lái)動(dòng)態(tài)顯示獲取到得數(shù)據(jù)列表:

復(fù)制代碼 代碼如下:

<asp:Repeater ID="rpImage" runat="server">
    <ItemTemplate>   
        <li>
            <a href="http://www.dbjr.com.cn/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
                <img src="http://www.dbjr.com.cn/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>"
                     class="<%# (Container.DataItem as ProductImage).ImageVersion)%>">
                <%# Eval("Name").ToString() %>
            </a>
        </li>
    </ItemTemplate>
</asp:Repeater>

在cs文件,是用GetProductImageList方法來(lái)獲取List<ProductImage>類(lèi)型的數(shù)據(jù)列表,并綁定在Repeater控件上面:
上面的不包含用戶(hù)自定義控件、服務(wù)器控件,所以不需要ItemDataBound事件來(lái)對(duì)單個(gè)的數(shù)據(jù)項(xiàng)進(jìn)行個(gè)性化的賦值
復(fù)制代碼 代碼如下:

protected override void BindDataSource()
{
    this.rpImage.DataSource = GetProductImageList();
    this.rpImage.DataBind();
}

方式二
在aspx頁(yè)面,這次包含了用戶(hù)自定義控件,所以需要用到ItemDataBound事件來(lái)對(duì)列表中的每一個(gè)用戶(hù)自定義控件進(jìn)行個(gè)性化的賦值,用戶(hù)自定義控件可以有公用的方法或者屬性,

讓我們?cè)贗temDataBound事件中賦值:

復(fù)制代碼 代碼如下:

<asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
    <ItemTemplate>
         <li>
             <UCCommon:ImageCell ID="imageCell" runat="server" />
             <a href="http://www.dbjr.com.cn/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='http://www.dbjr.com.cn/lmfeng/archive/2012/03/06/<%# Eval("Code").ToString()%>'>
                 <UCCommon:ProductFullNameCell ID="productFullNameCell" runat="server" />
             </a>
             <UCCommon:UCProductControlCell ID="productControlCell" runat="server"/>
         </li>
    </ItemTemplate>
</asp:Repeater>

在cs文件,用戶(hù)自定義控件可以有公用的方法或者屬性,讓我們?cè)贗temDataBound事件中賦值:
復(fù)制代碼 代碼如下:

protected override void BindDataSource()
{
    this.gvItemList.DataSource = productList;
    this.gvItemList.DataBind();
}
protected override void OnInit(EventArgs e)
{
    this.gvItemList.ItemDataBound += new RepeaterItemEventHandler(this.OnItemListDataBound);
    base.OnInit(e);
}
private void OnItemListDataBound(object sender, RepeaterItemEventArgs e)
{

    ProductCellInfo productItem = (ProductCellInfo)e.Item.DataItem;

    if (productItem != null)
    {
       ProductFullNameCell productName;
       ImageCell image;
       ProductControlCell productControlCell;

       foreach (Control sub in e.Item.Controls)
       {

           productName = sub as ProductFullNameCell;
           if (productName != null)
           {
               productName.InitProductFullName(productItem.Title, productItem.PromotionTitle, DispalyContentLength);
               continue;
           }

           image = sub as ImageCell;
           if (image != null)
           {
               image.InitImageCell2(productItem.ID, productItem.Code, productItem.Name, productItem.ImageUrl, productItem.ImageVersion);

               continue;
           }

           productControlCell = sub as ProductControlCell;
           if (productControlCell != null)
           {
               productControlCell.InitProductControlCell(productItem);
               continue;
           }
       }
   }
}


方式三:
在aspx頁(yè)面,可以顯示設(shè)置OnItemDataBound屬性,就不用像方式二那樣,在cs文件中的OnInit方法中動(dòng)態(tài)綁定,代碼如下:
復(fù)制代碼 代碼如下:

<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
    <ItemTemplate>
        <li>
           <a href='http://www.dbjr.com.cn/lmfeng/archive/2012/03/06/<%#Eval("ID"))>' title='<%#Encode(Eval("Name")) %>'>
                <span><%#Encode(Eval("Name")) %></span>
                <asp:PlaceHolder ID="pNew" runat="server" Visible="false"></asp:PlaceHolder>
                <asp:PlaceHolder ID="pHot" runat="server" Visible="false"></asp:PlaceHolder>
                <asp:Literal ID="literalValidGiftOption" runat="server"></asp:Literal>
        </a>
        </li>
    </ItemTemplate>
</asp:Repeater>

在cs文件:
復(fù)制代碼 代碼如下:

protected override void BindDataSource()
{
    base.BindDataSource();

    this.rptListCell.DataSource = this.List;
    this.rptListCell.DataBind();
}

protected void RptAllOnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    CategoryInfo category = (CategoryInfo)e.Item.DataItem;
    PlaceHolder pHot = e.Item.FindControl("pHot") as PlaceHolder;
    PlaceHolder pNew = e.Item.FindControl("pNew") as PlaceHolder;
    Literal lit = e.Item.FindControl("literalValidGiftOption") as Literal;
    switch (category.PromotionStatus)
    {
        case "H":
           pHot.Visible = true;
           break;
        case "N":
           pNew.Visible = true;
           break;
        default:
           break;
    }
    lit.Text = category.Name;
}

相關(guān)文章

  • 在WPF中實(shí)現(xiàn)全局快捷鍵功能

    在WPF中實(shí)現(xiàn)全局快捷鍵功能

    這篇文章介紹了在WPF中實(shí)現(xiàn)全局快捷鍵功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#使用StreamReader和StreamWriter類(lèi)讀寫(xiě)操作文件

    C#使用StreamReader和StreamWriter類(lèi)讀寫(xiě)操作文件

    這篇文章介紹了C#使用StreamReader和StreamWriter類(lèi)讀寫(xiě)操作文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#泛型的使用及示例詳解

    C#泛型的使用及示例詳解

    這篇文章主要介紹了C#泛型的使用及示例,本文通過(guò)例子個(gè)大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • C#中?、?.、??、??=運(yùn)算符的用法

    C#中?、?.、??、??=運(yùn)算符的用法

    本文主要介紹了C#中?、?.、??、??=運(yùn)算符的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • C#編程獲取實(shí)體類(lèi)屬性名和值的方法示例

    C#編程獲取實(shí)體類(lèi)屬性名和值的方法示例

    這篇文章主要介紹了C#編程獲取實(shí)體類(lèi)屬性名和值的方法,涉及C#實(shí)體類(lèi)的定義、實(shí)例化、遍歷等相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • C#如何打開(kāi)選擇文件對(duì)話(huà)框和選擇目錄對(duì)話(huà)框

    C#如何打開(kāi)選擇文件對(duì)話(huà)框和選擇目錄對(duì)話(huà)框

    這篇文章主要介紹了C#如何打開(kāi)選擇文件對(duì)話(huà)框和選擇目錄對(duì)話(huà)框問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 利用C#版OpenCV實(shí)現(xiàn)圓心求取實(shí)例代碼

    利用C#版OpenCV實(shí)現(xiàn)圓心求取實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用C#版OpenCV實(shí)現(xiàn)圓心求取的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • C#實(shí)現(xiàn)將HTML轉(zhuǎn)換成純文本的方法

    C#實(shí)現(xiàn)將HTML轉(zhuǎn)換成純文本的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將HTML轉(zhuǎn)換成純文本的方法,基于自定義類(lèi)實(shí)現(xiàn)文本轉(zhuǎn)換功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • c#中SAPI使用總結(jié)——SpVoice的使用方法

    c#中SAPI使用總結(jié)——SpVoice的使用方法

    最近使用C#重做了點(diǎn)名系統(tǒng)(要用到TTS,讓計(jì)算機(jī)點(diǎn)名)使用了SAPI,在這里總結(jié)一下SpVoice的使用方法。
    2011-10-10
  • LRU緩存替換策略及C#實(shí)現(xiàn)方法分享

    LRU緩存替換策略及C#實(shí)現(xiàn)方法分享

    LRU(Least Recently Used)緩存替換策略是一種常用的緩存管理策略,它根據(jù)數(shù)據(jù)最近被訪(fǎng)問(wèn)的時(shí)間來(lái)決定哪些數(shù)據(jù)應(yīng)該被保留在緩存中。本文將介紹LRU緩存替換策略的原理和C#實(shí)現(xiàn)方法。
    2023-04-04

最新評(píng)論