我們先講講Repeater基礎(chǔ)。我們可以在aspx文件中融入Repeater 控件,也可以在<ItemTemplate></ItemTemplate>包含的范圍融入自己的代碼,當(dāng)然這里也要使用到變量<%# Eval("SellerName")%>;注意兩側(cè)的引號(hào)。
.aspx:
<asp:Repeater ID="SellerRpt" runat="server"> <ItemTemplate> <li><a href='<%# Eval("SellerName")%>' target="_blank"> <%# Eval("ComName")%></a></li> </ItemTemplate> </asp:Repeater>
和后臺(tái)cs中相對(duì)照,在頁(yè)面加載處加入數(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)化
當(dāng)讓我們也能夠用DataItem可減少Eval函數(shù)的執(zhí)行步驟,優(yōu)化頁(yè)面解析時(shí)間:
<%# ((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="#" rel="external nofollow" ><span> <%#Container.DataItem%></span></a> </ItemTemplate> </asp:Repeater>
.cs:
ArrayList alterText; AdDA.GetIndexTopList(out alterText); topAdHintRpt.DataSource = alterText; topAdHintRpt.DataBind();
處理后顯示
現(xiàn)在我們要說的就是在特定情況下,數(shù)據(jù)庫(kù)中檢索出來的數(shù)據(jù)并不適合直接顯示出來,想要適當(dāng)處理后顯示(eg:日期的格式,字符串長(zhǎng)度的控制),可使用標(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ù)項(xiàng) string strDate = rowv["clDate"].ToString(); Label DateLB = e.Item.FindControl("colinDate") as Label; DateLB.Text = strDate.Substring(0, 10); } }
嵌套R(shí)eapeter的顯示
對(duì)于某些復(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的哪個(gè)位置( 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對(duì)象 DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關(guān)聯(lián)的數(shù)據(jù)項(xiàng) string str = Convert.ToString(rowv["Pro_Content"]); //獲取填充子類的內(nèi)容 rep.DataSource = Product.FillPara(str); rep.DataBind(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/128202.html
閱讀 547·2023-03-27 18:33
閱讀 732·2023-03-26 17:27
閱讀 630·2023-03-26 17:14
閱讀 591·2023-03-17 21:13
閱讀 521·2023-03-17 08:28
閱讀 1801·2023-02-27 22:32
閱讀 1292·2023-02-27 22:27
閱讀 2178·2023-01-20 08:28