Repeater控件綁定的三種方式
方式一
在aspx頁(yè)面,寫(xiě)好需要循環(huán)輸出的內(nèi)容,一般包含用戶(hù)自定義控件、服務(wù)器控件、Html格式的片段、和<%# Eval("Name")%>這種方式來(lái)動(dòng)態(tài)顯示獲取到得數(shù)據(jù)列表:
<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è)性化的賦值
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事件中賦值:
<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事件中賦值:
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)綁定,代碼如下:
<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文件:
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;
}
- asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
- ASP.NET中repeater控件用法實(shí)例
- 在Repeater控件中通過(guò)Eval的方式綁定Style樣式代碼
- Repeater控件與PagedDataSource結(jié)合實(shí)現(xiàn)分頁(yè)功能
- Repeater控件實(shí)現(xiàn)編輯、更新、刪除等操作示例代碼
- 給Repeater控件里添加序號(hào)的5種才常見(jiàn)方法介紹
- Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息實(shí)現(xiàn)思路
- 如何取得Repeater控件選擇的項(xiàng)目及注意事項(xiàng)
- Repeater控件分別綁定數(shù)組和ArrayList實(shí)現(xiàn)思路
- Repeater控件數(shù)據(jù)導(dǎo)出Excel(附演示動(dòng)畫(huà))
- 淺析Repeater控件的使用 (原樣導(dǎo)出和動(dòng)態(tài)顯示/隱藏Repeater中的列)
相關(guān)文章
C#使用StreamReader和StreamWriter類(lèi)讀寫(xiě)操作文件
這篇文章介紹了C#使用StreamReader和StreamWriter類(lèi)讀寫(xiě)操作文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#如何打開(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í)例代碼
這篇文章主要給大家介紹了關(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-05C#實(shí)現(xiàn)將HTML轉(zhuǎn)換成純文本的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將HTML轉(zhuǎn)換成純文本的方法,基于自定義類(lèi)實(shí)現(xiàn)文本轉(zhuǎn)換功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07c#中SAPI使用總結(jié)——SpVoice的使用方法
最近使用C#重做了點(diǎn)名系統(tǒng)(要用到TTS,讓計(jì)算機(jī)點(diǎn)名)使用了SAPI,在這里總結(jié)一下SpVoice的使用方法。2011-10-10