網(wǎng)站首頁 編程語言 正文
Repeater基礎(chǔ)
在aspx文件中加入Repeater 控件,在<ItemTemplate></ItemTemplate>包含的范圍里加入自己控制的代碼,需要替換的變量使用<%# Eval("SellerName")%>;注意兩側(cè)的引號。
.aspx:
<asp:Repeater ID="SellerRpt" runat="server">
<ItemTemplate>
<li><a href='<%# Eval("SellerName")%>' target="_blank">
<%# Eval("ComName")%></a></li>
</ItemTemplate>
</asp:Repeater>
對應(yīng)的后臺cs中,在頁面加載處加入數(shù)據(jù)綁定的代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = SellerDA.GetTopHotSellers(9);
SellerRpt.DataSource = dt;
SellerRpt.DataBind();
}
}
aspx中"SellerName"、"ComName"為DataTable 中的列名。
優(yōu)化
直接使用DataItem可減少Eval函數(shù)的執(zhí)行步驟,優(yōu)化頁面解析時間:
<%# ((DataRowView)Container.DataItem)["SellerName"]%>替換<%# Eval("SellerName")%>
ArrayList數(shù)據(jù)源
如果數(shù)據(jù)源是ArrayList,并且ArrayList為一列string數(shù)組,則可不用寫出列名:
.aspx:
<asp:Repeater ID="topAdHintRpt" runat="server">
<ItemTemplate>
<asp:Label ID="BarLabel" CssClass="bar" runat="server" Text="|"></asp:Label>
<a href="#" ><span>
<%#Container.DataItem%></span></a>
</ItemTemplate>
</asp:Repeater>
.cs:
ArrayList alterText;
AdDA.GetIndexTopList(out alterText);
topAdHintRpt.DataSource = alterText;
topAdHintRpt.DataBind();
處理后顯示
某些情況下,數(shù)據(jù)庫中檢索出來的數(shù)據(jù)并不適合直接顯示出來,想要適當(dāng)處理后顯示(eg:日期的格式,字符串長度的控制),可使用標(biāo)簽來占位,在onitemdatabound函數(shù)中自行控制:
.aspx:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
<ItemTemplate>
<asp:Label ID="colinDate" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:Repeater>
.cs:
protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關(guān)聯(lián)的數(shù)據(jù)項
string strDate = rowv["clDate"].ToString();
Label DateLB = e.Item.FindControl("colinDate") as Label;
DateLB.Text = strDate.Substring(0, 10);
}
}
嵌套Reapeter的顯示
對于某些復(fù)雜的顯示邏輯,需用用到Reapeter的嵌套,這里需要自行控制2層數(shù)據(jù)源的數(shù)據(jù)綁定:
.aspx:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="ParaRpt" runat="server" OnItemDataBound="ParaRpt_ItemDataBound">
<ItemTemplate>
<asp:Label ID="bar" CssClass="bar" runat="server" Text="|"></asp:Label>
<span class="para">
<%# Eval("Name")%>:
<%# Eval("Value")%></span>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
.cs:
protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判斷里層repeater處于外層repeater的哪個位置( AlternatingItemTemplate,F(xiàn)ooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("ParaRpt") as Repeater;//找到里層的repeater對象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關(guān)聯(lián)的數(shù)據(jù)項
string str = Convert.ToString(rowv["Pro_Content"]); //獲取填充子類的內(nèi)容
rep.DataSource = Product.FillPara(str);
rep.DataBind();
}
}
原文鏈接:https://www.cnblogs.com/me115/archive/2011/04/09/2010682.html
相關(guān)推薦
- 2023-07-18 Springboot獲取jar包同級目錄
- 2022-02-18 Redis - Redis command timed out nested exception i
- 2024-01-05 使用idea構(gòu)建父子類springboot項目教程
- 2022-12-06 React自定義視頻全屏按鈕實現(xiàn)全屏功能_React
- 2022-08-04 python調(diào)用pymssql包操作SqlServer數(shù)據(jù)庫的實現(xiàn)_python
- 2022-12-25 一文帶你了解Go語言中的指針和結(jié)構(gòu)體_Golang
- 2022-07-02 element下拉框獲取選中的內(nèi)容
- 2023-02-10 python中的list?查找與過濾方法整合_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支