ASP.NET數(shù)據(jù)綁定之Repeater控件
在ASP.NET的學(xué)習(xí)過(guò)程中,其控件的學(xué)習(xí)和使用占了很大的一部分,本文為大家介紹一下控件Repeater控件的使用,用它來(lái)綁定后臺(tái)數(shù)據(jù),然后在客戶端(瀏覽器)上顯示出來(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ù)中插入間隔,像橫線、特殊符號(hào)等等)
二、示例
1、內(nèi)容介紹
將數(shù)據(jù)庫(kù)中Person表中的信息選出來(lái),然后用Repeater控件在客戶端顯示出來(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 Core中服務(wù)的生命周期選項(xiàng)區(qū)別與用法詳解
這篇文章主要給大家介紹了關(guān)于Asp.Net Core中服務(wù)的生命周期選項(xiàng)區(qū)別與用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
通用?HTTP?簽名組件的另類(lèi)實(shí)現(xiàn)方式
這篇文章主要介紹了通用?HTTP?簽名組件的另類(lèi)實(shí)現(xiàn)方式,實(shí)現(xiàn)思路大概是采用鏈?zhǔn)秸{(diào)用的方式,使得簽名的步驟可以動(dòng)態(tài)拼湊組合,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
C#調(diào)用C++版本dll時(shí)的類(lèi)型轉(zhuǎn)換需要注意的問(wèn)題小結(jié)
最近使用C#調(diào)用C++版本的dll遇到很多類(lèi)型轉(zhuǎn)換的問(wèn)題,現(xiàn)記錄出容易出錯(cuò)的部分。2010-04-04
asp.net GridView和DataList實(shí)現(xiàn)鼠標(biāo)移到行行變色
在設(shè)計(jì)頁(yè)面添加了DataList控件后,我在使用DataList綁定數(shù)據(jù)時(shí)是通過(guò)單元格來(lái)綁定的,因此鼠標(biāo)效果就在源代碼頁(yè)面去實(shí)現(xiàn)2009-02-02
ASP.NET MVC Admin主頁(yè)快速構(gòu)建
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC Admin主頁(yè)快速構(gòu)建的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
ASP.NET?Core使用MiniProfiler分析應(yīng)用
這篇文章介紹了ASP.NET?Core使用MiniProfiler分析應(yīng)用的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

