ASP.NET數(shù)據(jù)綁定之Repeater控件
在ASP.NET的學(xué)習(xí)過(guò)程中,其控件的學(xué)習(xí)和使用占了很大的一部分,本文為大家介紹一下控件Repeater控件的使用,用它來(lái)綁定后臺(tái)數(shù)據(jù),然后在客戶(hù)端(瀏覽器)上顯示出來(lái)!
一、 Repeater控件
1、用途:使用模板循環(huán)顯示數(shù)據(jù)。
2、包含的模板:
- <ItemTemplate></ItemTemplate> 項(xiàng)目模板(里面的數(shù)據(jù)正常顯示)
- <AlternatingItemTemplate></AlternatingItemTemplate> 交錯(cuò)顯示模板(里面綁定的數(shù)據(jù)交錯(cuò)著顯示)<FooterTemplate></FooterTemplate>頁(yè)腳模板(編輯頁(yè)腳)
- <HeaderTemplate></HeaderTemplate>頁(yè)眉模板(編輯頁(yè)眉)
- <SeparatorTemplate></SeparatorTemplate>間隔模板 (在顯示的數(shù)據(jù)中插入間隔,像橫線(xiàn)、特殊符號(hào)等等)
二、示例
1、內(nèi)容介紹
將數(shù)據(jù)庫(kù)中Person表中的信息選出來(lái),然后用Repeater控件在客戶(hù)端顯示出來(lái)。下圖是我Sqlser數(shù)據(jù)庫(kù)中person表中的信息。
1)、將數(shù)據(jù)庫(kù)中的信息選出來(lái)并在后臺(tái)綁定: 新建Web窗體應(yīng)用程序,添加窗體,在窗體的Page_Load事件中添加如下代碼。
protected void Page_Load(object sender, EventArgs e) { SqlConnection con = DB.createConnection(); SqlDataAdapter sda = new SqlDataAdapter(); string sql="select * from person "; sda.SelectCommand = new SqlCommand(sql, con); DataSet ds=new DataSet(); sda.Fill(ds, "per"); this.Repeater1.DataSource=ds.Tables["per"]; Repeater1.DataBind(); }
2)、用控件Repeater的模板 <ItemTemplate></ItemTemplate> 將信息顯示,代碼如下
<asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <p align="center"> <%# DataBinder.Eval(Container.DataItem,"pID") %> <%# DataBinder.Eval(Container.DataItem,"personName") %> <%# DataBinder.Eval(Container.DataItem,"personSex") %> </p> </ItemTemplate> </asp:Repeater>
3)、顯示效果如下
4)、<AlternatingItemTemplate></AlternatingItemTemplate>模板使用(讓數(shù)據(jù)交叉顯示)
<asp:Repeater ID="Repeater1" runat="server"> <AlternatingItemTemplate> <p align="center"> <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %> <%# DataBinder.Eval(Container.DataItem,"personName") %> <%# DataBinder.Eval(Container.DataItem,"personSex") %></font> </p> </AlternatingItemTemplate> </asp:Repeater>
顯示效果如下,結(jié)構(gòu)只顯示2、4、6、9列,這就是所謂的交叉顯示。
最后,我將五個(gè)模板一塊使用,前臺(tái)代碼如下
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <h3 align="center">頁(yè)眉模板</h3> </HeaderTemplate> <ItemTemplate> <p align="center"> <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %> <%# DataBinder.Eval(Container.DataItem,"personName") %> <%# DataBinder.Eval(Container.DataItem,"personSex") %></font> </p> </ItemTemplate> <AlternatingItemTemplate> <p align="center"> <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %> <%# DataBinder.Eval(Container.DataItem,"personName") %> <%# DataBinder.Eval(Container.DataItem,"personSex") %></font> </p> </AlternatingItemTemplate> <SeparatorTemplate> <hr color="red" size="1" /> </SeparatorTemplate> <FooterTemplate> <h3 align="center">頁(yè)腳模板</h3> </FooterTemplate> </asp:Repeater>
顯示效果圖如下
這就是利用控件將后臺(tái)數(shù)據(jù)庫(kù)中的信息用瀏覽器顯示出來(lái)的方法,其實(shí)不光Repeater控件,像DataList,GridView,CheckBoxList、DropDownList等等都能將數(shù)據(jù)庫(kù)中的信息加以綁定然后再在瀏覽器中顯示出來(lái),希望對(duì)這幾個(gè)重要的控件可以熟練掌握。
- 詳解ASP.NET-----Repeater數(shù)據(jù)控件的用法總結(jié)
- 詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法
- asp.net中使用 Repeater控件拖拽實(shí)現(xiàn)排序并同步數(shù)據(jù)庫(kù)字段排序
- asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
- ASP.NET中repeater控件用法實(shí)例
- asp.net Repeater控件的說(shuō)明及詳細(xì)介紹及使用方法
- asp.net下Repeater使用 AspNetPager分頁(yè)控件
- asp.net 遍歷repeater中的控件的幾種方式
- ASP.NET實(shí)現(xiàn)Repeater控件的數(shù)據(jù)綁定
相關(guān)文章
ASP.NET MVC3 SEO優(yōu)化:利用Routing特性提高站點(diǎn)權(quán)重
這篇文章主要介紹了ASP.NET MVC3 SEO優(yōu)化:利用Routing特性消除多個(gè)路徑指向同一個(gè)Action,從而提高站點(diǎn)權(quán)重,需要的朋友可以參考下。2016-06-06詳解在ASP.NET Core下使用SignalR技術(shù)
本篇文章主要介紹了在ASP.NET Core下使用SignalR技術(shù) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02使用FlashPaper在線(xiàn)轉(zhuǎn)換.doc為.swf
Flashpaper的使用相信大多數(shù)人都知道,這里的Demo是用于在線(xiàn)轉(zhuǎn)換 .doc文件轉(zhuǎn)換為 .swf的flash文件。2011-02-02ASP.NET中RadioButtonList綁定后臺(tái)數(shù)據(jù)后觸發(fā)點(diǎn)擊事件
這篇文章主要介紹了ASP.NET中RadioButtonList綁定后臺(tái)數(shù)據(jù)后觸發(fā)點(diǎn)擊事件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05ASP.NET堆和棧一之基本概念和值類(lèi)型內(nèi)存分配
這篇文章介紹了ASP.NET堆和棧的基本概念和值類(lèi)型內(nèi)存分配,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Asp.Net?Core使用Ocelot結(jié)合Consul實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)
這篇文章介紹了Asp.Net?Core使用Ocelot結(jié)合Consul實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04詳解如何在ASP.NET Core中使用IHttpClientFactory
這篇文章主要介紹了詳解如何在ASP.NET Core中使用IHttpClientFactory,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02ASP.NET 2.0 程序安全的基礎(chǔ)知識(shí)
成員關(guān)系的概念在人類(lèi)社會(huì)中是一個(gè)層次比較低的概念,源于希望屬于某個(gè)群組的意識(shí)。同樣,在ASP.NET 2.0程序開(kāi)始開(kāi)發(fā)涉及到成員關(guān)系的應(yīng)用程序時(shí),必須首先理解身份、驗(yàn)證和授權(quán)這幾個(gè)關(guān)鍵的概念。2010-04-04