a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

Repeater控件綁定的三種方式

2020-01-24 03:22:51
字體:
供稿:網(wǎng)友

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

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

<asp:Repeater ID="rpImage" runat="server">
    <ItemTemplate>   
        <li>
            <a href="http://www.5lwq4hdr.cn/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
                <img src="http://www.5lwq4hdr.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方法來獲取List<ProductImage>類型的數(shù)據(jù)列表,并綁定在Repeater控件上面:
上面的不包含用戶自定義控件、服務(wù)器控件,所以不需要ItemDataBound事件來對單個的數(shù)據(jù)項進行個性化的賦值
復(fù)制代碼 代碼如下:

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

方式二
在aspx頁面,這次包含了用戶自定義控件,所以需要用到ItemDataBound事件來對列表中的每一個用戶自定義控件進行個性化的賦值,用戶自定義控件可以有公用的方法或者屬性,

讓我們在ItemDataBound事件中賦值:

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

<asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
    <ItemTemplate>
         <li>
             <UCCommon:ImageCell ID="imageCell" runat="server" />
             <a href="http://www.5lwq4hdr.cn/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='//www.5lwq4hdr.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文件,用戶自定義控件可以有公用的方法或者屬性,讓我們在ItemDataBound事件中賦值:
復(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頁面,可以顯示設(shè)置OnItemDataBound屬性,就不用像方式二那樣,在cs文件中的OnInit方法中動態(tài)綁定,代碼如下:
復(fù)制代碼 代碼如下:

<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
    <ItemTemplate>
        <li>
           <a href='//www.5lwq4hdr.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;
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品久久久久久福利一牛影视 | 国产中文字幕在线 | 一级免费黄色 | 看欧美黄色录像 | 一本色道久久综合亚洲精品不 | 国产美女精品人人做人人爽 | 亚洲国产日本 | 国精品一区| 日本三级视频在线观看 | 天堂精品一区 | 欧美成人一区二区三区片免费 | 高清国产一区二区三区 | 自拍偷拍小视频 | 中文二区| 免费视频99| 亚洲不卡在线 | 可以免费在线看黄的网站 | 日韩视频网站在线观看 | 久久不卡 | 国精日本亚洲欧州国产中文久久 | 国产精品自产拍在线观看桃花 | 久久国产综合 | 国产黄色在线播放 | 妞干网福利视频 | 国产一区日韩在线 | 日韩一级黄色大片 | 天天操天天拍 | 毛片毛片毛片毛片毛片毛片 | 麻豆二区 | 一级片手机免费看 | 羞羞网站在线观看 | 99国产精品99久久久久久 | 黄色毛片观看 | 亚洲精品福利视频 | 91.com在线 | 亚洲免费视频网站 | 国产黄色大片 | 国产精品欧美日韩 | 亚洲免费在线 | 亚洲一区二区视频在线 | 日日噜噜噜噜久久久精品毛片 |