国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Repeater控件綁定的三種方式

2020-01-24 03:22:51
字體:
來源:轉載
供稿:網友

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

復制代碼 代碼如下:

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

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

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

讓我們在ItemDataBound事件中賦值:

復制代碼 代碼如下:

<asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
    <ItemTemplate>
         <li>
             <UCCommon:ImageCell ID="imageCell" runat="server" />
             <a href="http://www.survivalescaperooms.com/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='//www.survivalescaperooms.com/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事件中賦值:
復制代碼 代碼如下:

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頁面,可以顯示設置OnItemDataBound屬性,就不用像方式二那樣,在cs文件中的OnInit方法中動態綁定,代碼如下:
復制代碼 代碼如下:

<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
    <ItemTemplate>
        <li>
           <a href='//www.survivalescaperooms.com/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文件:
復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜康市| 孙吴县| 溆浦县| 当雄县| 依安县| 克什克腾旗| 佛冈县| 云梦县| 淮滨县| 云南省| 龙井市| 镇江市| 绥棱县| 礼泉县| 连南| 建宁县| 加查县| 东光县| 万宁市| 漾濞| 九龙坡区| 新和县| 维西| 云梦县| 镇安县| 宝应县| 黄平县| 竹山县| 霍林郭勒市| 伊金霍洛旗| 成都市| 中山市| 中阳县| 拜城县| 鄂托克前旗| 凤山县| 兖州市| 和平区| 高尔夫| 太原市| 洛南县|