溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Repeater中DataSource和數(shù)據(jù)顯示總結(jié)

發(fā)布時(shí)間:2020-05-09 11:33:35 來(lái)源:億速云 閱讀:415 作者:Leah 欄目:編程語(yǔ)言

今天小編就為大家?guī)?lái)一篇Repeater中DataSource和數(shù)據(jù)顯示總結(jié)的文章。小編覺(jué)得挺不錯(cuò)的,為此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

Content
一、Repeater要能顯示數(shù)據(jù),其要有數(shù)據(jù)源,而了解數(shù)據(jù)源類型,才能匹配正確的數(shù)據(jù)源:
        查看MSDN:Repeater 的 DataSource 可以是任何 System.Collections.IEnumerable 集合 (如) 訪問(wèn)數(shù)據(jù)庫(kù)的 System.Data.DataView , System.Collections.ArrayList或數(shù)組或 IListSource 對(duì)象。
只要確保數(shù)據(jù)類型是上所描述,就可以作為Repeater的數(shù)據(jù)源,就可以通過(guò)Repeater來(lái)顯示數(shù)據(jù)。
注:為Repeater指定數(shù)據(jù)源后,記得綁定數(shù)據(jù)(eg:this.Repeater1.DataBind();).
二、通過(guò)代碼來(lái)展示數(shù)據(jù)的顯示:
    1.頁(yè)面運(yùn)行結(jié)果:
Repeater中DataSource和數(shù)據(jù)顯示總結(jié)
2. 后臺(tái)主要代碼:
  1. #region 定義List<string>的數(shù)據(jù)源類型
  2. List<string> testDS1 = new List<string>() { "1", "2" };//初始化數(shù)據(jù)
  3. this.Repeater1.DataSource = testDS1;//指定數(shù)據(jù)源
  4. this.Repeater1.DataBind();//綁定數(shù)據(jù)
  5. #endregion
  6. #region   定義List<person>的數(shù)據(jù)源類型
  7. List<person> testDS2 = new List<person>() { new person(1, "person1"), new person(2, "person2") };//初始化數(shù)據(jù)
  8. this.Repeater2.DataSource = testDS2;//指定數(shù)據(jù)源
  9. this.Repeater2.DataBind();//綁定數(shù)據(jù)
  10. #endregion
  11. #region 定義Dictionary<string,string>的數(shù)據(jù)源類型
  12. Dictionary<string, string> testDS3 = new Dictionary<string, string>() { { "1", "value1" }, { "2", "value2" } };//初始化數(shù)據(jù)
  13. this.Repeater3.DataSource = testDS3;//指定數(shù)據(jù)源
  14. this.Repeater3.DataBind();//綁定數(shù)據(jù)
  15. #endregion
3. 前臺(tái)主要代碼:
  1. <div>  
  2.     <a>第一個(gè)repeater顯示的數(shù)據(jù):</a>  
  3.     <asp:Repeater ID="Repeater1" runat="server">  
  4.         <HeaderTemplate>  
  5.             <table>  
  6.                 <th>  
  7.                     顯示  
  8.                 </th>  
  9.         </HeaderTemplate>  
  10.         <ItemTemplate>  
  11.             <tr>  
  12.                 <td>  
  13.                     <%#Container.DataItem %>  
  14.                 </td>  
  15.             </tr>  
  16.         </ItemTemplate>  
  17.         <FooterTemplate>  
  18.             </table>  
  19.         </FooterTemplate>  
  20.     </asp:Repeater>  
  21. </div>  
  22. <div >  
  23.     <a>第二個(gè)repeater顯示的數(shù)據(jù):</a>  
  24.     <asp:Repeater ID="Repeater2" runat="server">  
  25.         <HeaderTemplate>  
  26.             <table>  
  27.                 <th>  
  28.                     Age  
  29.                 </th>  
  30.                 <th>  
  31.                     Name  
  32.                 </th>  
  33.         </HeaderTemplate>  
  34.         <ItemTemplate>  
  35.             <tr>  
  36.                 <td>  
  37.                     <%#DataBinder.Eval(Container.DataItem,"Age") %>    
  38.                 </td>  
  39.                 <td>  
  40.                    <%#DataBinder.Eval(Container.DataItem,"Name") %>  
  41.                 </td>  
  42.             </tr>  
  43.         </ItemTemplate>  
  44.         <FooterTemplate>  
  45.             </table>  
  46.         </FooterTemplate>  
  47.     </asp:Repeater>  
  48. </div>  
  49. <div>  
  50.     <a>第三個(gè)repeater顯示的數(shù)據(jù):</a>  
  51.     <asp:Repeater ID="Repeater3" runat="server">  
  52.         <HeaderTemplate>  
  53.             <table>  
  54.                 <th>  
  55.                     Key  
  56.                 </th>  
  57.                 <th>  
  58.                     Value  
  59.                 </th>  
  60.         </HeaderTemplate>  
  61.         <ItemTemplate>  
  62.             <tr>  
  63.                 <td>  
  64.                     <%#Eval("key") %>  
  65.                 </td>  
  66.                 <td>  
  67.                     <%#Eval("value") %>  
  68.                 </td>  
  69.             </tr>  
  70.         </ItemTemplate>  
  71.         <FooterTemplate>  
  72.             </table>  
  73.         </FooterTemplate>  
  74.     </asp:Repeater>  
  75. </div> 
       由于"2. 后臺(tái)主要代碼"指定了repeater的數(shù)據(jù)源,在"3. 前臺(tái)主要代碼"  在<ItemTemplate>中,如果數(shù)據(jù)源沒(méi)有字段名,通過(guò) <%#Container.DataItem %> 來(lái)顯示數(shù)據(jù),如第一個(gè)Repeater所示;如果數(shù)據(jù)源存在字段名通過(guò)<%#Eval("字段名")%>或者<%#DataBinder.Eval(Container.DataItem,"字段名")%>來(lái)顯示數(shù)據(jù)。使用以上來(lái)顯示一般數(shù)據(jù)就可以,如果數(shù)據(jù)是需要修改的,則使用Bind("字段名").
4.其它有用小結(jié):
Eval和Bind區(qū)別:Eval是只讀方法,將其參數(shù)中字段的值作為字符串返回;Bind則可讀可寫,可以檢索數(shù)據(jù)綁定控件的值并將任何修改提回會(huì)數(shù)據(jù)庫(kù)。
Container.DataItem和DataBinder:
DataBinder是System.Web里面的一個(gè)靜態(tài)類,它提供了Eval方法用于簡(jiǎn)化數(shù)據(jù)綁定表達(dá)式的編寫

Container則是ASP.NET頁(yè)面編譯器在數(shù)據(jù)綁定事件處理程序內(nèi)部聲明的局部變量,其類型是可以進(jìn)行數(shù)據(jù)綁定的控件的數(shù)據(jù)容器類型(如在Repeater內(nèi)部的數(shù)據(jù)綁定容器叫RepeaterItem),在這些容器類中基本都有DataItem屬性,因此你可以寫Container.DataItem,這個(gè)屬性返回的是你正在被綁定的數(shù)據(jù)源中的那個(gè)數(shù)據(jù)項(xiàng)。如果你的數(shù)據(jù)源是DataTable,則這個(gè)數(shù)據(jù)項(xiàng)的類型實(shí)際是DataRowView。


關(guān)于Repeater中DataSource和數(shù)據(jù)顯示的總結(jié)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的參考價(jià)值,如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI