repeater用法一步一步详讲,用来绑定和显示数据信息
估計(jì)要分兩部:
?一部分UI部分:在一個(gè)div里面可以寫個(gè)repeater服務(wù)器控件,用來橫排顯示需要的數(shù)據(jù);<asp:Repeater id="" runnat="servece" ....................>
首先,repeater這個(gè)控件,有head,items,foot三個(gè)部分,不一定全用。
然后,布局:headtemplate里面寫上顯示的字段欄,<th align=字段文字位置,居中是center ?class=樣式>字段名稱</th>
? ? ? ? 若干字段后,就改寫顯示數(shù)據(jù)部分,也就是itemtemplate,這個(gè)數(shù)據(jù)段書寫類似head,但有個(gè)地方要注意就是<%#Eval('xxxx')%>這部分,?最好寫這個(gè)數(shù)據(jù)字段的列要對(duì)應(yīng)headtemplate的字段列,可以這樣寫<td align=center ><asp:label id="xxx" runate ="service" Text='<%#Eval("xianshiID")%>'></asp:label></td>,這里再次說明一下,這個(gè)Eval部分是執(zhí)行在后臺(tái),后臺(tái)給ui顯示賦值來著。
? ? ? ? ? 舉例:<td align="center"><asp:Label ID="Value1" runat="server" Text='<%#Eval("Value") %>'></asp:Label> </td>
? ? ? ? 如果沒有數(shù)據(jù),可以在footertemplate中添加一行visible='<%#Repeate.Items.Count==0 %>'屬性,然后添加<tr><td align=xx>沒有數(shù)據(jù)</td></tr>;
? ? ? ??
? ? 二部分后臺(tái)部分:這就是Repeater控件UI頁顯示,前面的主要部分,下面說說后臺(tái)要做的
? ?在后臺(tái),你需要把數(shù)據(jù)跟Repeater控件綁定才能顯示數(shù)據(jù),我們可以按下面來做:
? ? ? ? A:在頁面加載的時(shí)候,實(shí)現(xiàn)RepeaterName.source = 一個(gè)datatable類型的返回(寫一個(gè)返回函數(shù)最好),然后是 ? ? ? ? ? ? ? RepeaterNmae.DataBind();
? ? ? ? B:
? ? ? ? ? ? ? ? ? B1:?在這個(gè)datatable類型的返回中,我們首先要寫一個(gè)datatable類型的函數(shù)實(shí)現(xiàn)欄目的顯示字段,
? ? ? ? ? ? ? ? ? ? ? ? ? ?DataTable One = new DataTable();
? ? ? ? ? ? ? ? ? ? ? ? ? ?One.Columns.Add("xianshiID");//注意與頁面Eval中的ID對(duì)其
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return ? One;//暫定為返回給TableStart
? ? ? ? ? ? ? ? ??B2: 然后再這個(gè)返回的DataTable容器中寫,然后
? ? ? ? ? ? ?如果數(shù)據(jù)比較多,可以循環(huán)寫:DataRow ro =?TableStart.newRow();//這里就新產(chǎn)生了datatabel類型的One的 ? ? 一行,對(duì)應(yīng)頁面上的一列,
? ? 然后,我們需要將數(shù)據(jù)庫讀出的一行數(shù)據(jù)填寫到這個(gè)ro行里面,
? ? ? ? ? ? ? ? ? ? ? ? ? ?ro[0] = 我數(shù)據(jù)讀出來的一個(gè)數(shù)據(jù);
? ? ? ? ? ? ?B3: 然后,必須是TableStart來用添加到里面,TableStart.add.Row(ro);
? ? ? ? ? ? 最后經(jīng)過循環(huán),tablestart已經(jīng)將所要顯示的數(shù)據(jù)都存起來了,然后return回去,就是一個(gè)datatable類型的返 ? ? ? ? ?回,這個(gè)就是綁定給Repeater的數(shù)據(jù),數(shù)據(jù)源;
? ?三部分操作部分:
? ? ? ? A:關(guān)于Repeater的數(shù)據(jù)刪除操作,首先要在Ui中寫上刪除按鈕
? ? ? ? ? ? ? ? ? 在head,itemtemplate,foot三部分的itemtemplate部分最后添加一列
<td align=xxx >
? ? ? ? ? ? ?<asp:linkbutton id=xxx Text="刪除" runate="server" CommanName="nameYouself" ?CommanAugumnet='關(guān)鍵字,一般用<%#Eval("ID")+"&"+Eval("ID2")%>來寫,主要是后臺(tái)跟蹤傳遞而已' >
? ? ? ? ? ? </asp:linkbutton>
</td>
? ? ? ? ?B:
? ? ? ? ? ?在后臺(tái)用RepeaterName_ItemsCommand(object source,RepeaterCommandEventArgs e)
? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?try
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (e.CommandName == "Link")
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string[] key = e.CommandArgument.ToString().Split('&');? ? ? ? ? ? ? ? ??? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xxxxxxxxxx其它數(shù)據(jù)操作,刪除操作;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?catch?{?? }
? ? ? ? ? ? }
? ? ? ? 上面的代碼生成是雙擊repeater產(chǎn)生的,也即是<asp:Repeater ID="RepeaterShow" runat="server" ?..........
? ? ? ? ? ? onitemcommand="RepeaterShow_ItemCommand">也就是repeater的onitemcommand事件;
總結(jié)
以上是生活随笔為你收集整理的repeater用法一步一步详讲,用来绑定和显示数据信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datalist,Repeater和Gr
- 下一篇: 数据绑定控件之Repeater