一層就很潮了,還要用到兩層唷,怎麼這麼花俏阿~
其實對網頁真的不是那麼拿手,平常99%都是寫視窗的嘛,
這星期有個後台的Case需要處理一下,剛好就使用Repeater來處理,
沒想到一層我還ok,兩層只好研究一下,所以在這邊記錄一下,以免下次用又腦殘了。
那先簡單介紹一下Repeater的碼是長怎樣囉:
<asp:Repeater id=Repeater1 runat="server">
<HeaderTemplate>
<table border=1>
<tr>
<td><b>Company</b></td>
<td><b>Symbol</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
HeaderTemplate
裡面如果有定義,就會決定清單標頭的內容和配置。如果沒有定義,則不會呈現標頭。
ItemTemplate
定義清單內項目的內容和配置。這個樣板是必要的。 (沒有配置的話你用Repeater幹嘛啦)
FooterTemplate
如果有定義,就會決定清單頁尾 (Footer) 的內容和配置。如果沒有定義,則不會呈現頁尾。
Repeater.DataSource 屬性
取得或設定提供資料以填入清單的資料來源。
Repeater.DataBind 方法 ()
用來將 DataSource 屬性所指定的資料來源繫結至 Repeater 控制項。
所以我想要有兩層就把第二個Repeater放在第一層的ItemTemplate的中間就可以了吧,
放進去是沒錯,但是當然事情不是像我這種笨蛋想的這麼簡單,所以要來作一些處理:
- 我要在Repeater1作ItemDataBound的時候把Repeater2利用FindControl找出來,直接作DataSource&DataSource
-
在Repeater2標籤內加上
<asp:Repeater ID="Repeater2" OnItemDataBound="Repeater2_ItemDataBound" OnItemCommand ="Repeater2_ItemCommand" runat="server">
- 再來程式裡加Repeater2_ItemDataBound的Sub,像是我有編輯跟新增的按鈕在Repater2裡面所以在此Sub就用FindControl找出來即可,記得Handles那段不用寫。
Protected Sub Repater2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
- 也有一個Del按鈕是直接使用ItemCommand所以一樣依照上面那樣寫一個Repeater2_ItemCommand的Sub
Protected Sub Repater2_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
因為你在標籤內有加入OnItemDataBound="Repater2_ItemDataBound" OnItemCommand ="Repater2_ItemCommand"
所以那兩個Sub都會被引發喔,其實我也不確定這是最好的方式啦,只是似乎都挺正常的,如果有更好的方法請前輩告知,謝謝。