生活随笔
收集整理的這篇文章主要介紹了
数据绑定控件之Repeater
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前幾篇的文章在說AJAX的內(nèi)容,利用AJAX技術(shù)能夠開發(fā)出高效運(yùn)行的網(wǎng)站應(yīng)用程序,不過在進(jìn)行B/S項(xiàng)目開發(fā)時(shí)只擁有AJAX技術(shù)是遠(yuǎn)遠(yuǎn)不夠的,踏入到B/S要學(xué)的東西會(huì)更多,但相較C/S的復(fù)雜邏輯結(jié)構(gòu)來說B/S在開發(fā)時(shí)還是很簡單的。
? ? ? ? 在開發(fā)B/S項(xiàng)目時(shí),常常會(huì)用到數(shù)據(jù)綁定控件,.NET平臺(tái)已經(jīng)對這些控件進(jìn)行了良好的封裝,只要稍有經(jīng)驗(yàn)的程序猿很快就能夠上手使用這些數(shù)據(jù)控件,所以接下來的幾篇文章將會(huì)討論數(shù)據(jù)控件,首先將會(huì)從數(shù)據(jù)控件的細(xì)節(jié)入手討論ListView、GridView、Repeater、DataList控件的基本使用方法,并在會(huì)后系列文章的最后對這幾個(gè)控件進(jìn)行綜合性的分析總結(jié)。
一、綁定控件之Repeater
??????? .NET封裝了多種數(shù)據(jù)綁定控件,諸如GridView、DataList等但該篇文章將會(huì)從Repeater入手,因?yàn)镽epeater只提供了基本的數(shù)據(jù)綁定模板,沒有內(nèi)置其它分頁等功能,所以它是最原始的數(shù)據(jù)綁定控件,只要能夠熟練運(yùn)用Repeater控件其它的綁定控件也就很簡單了。
? 1、Repeater簡介
? ? ? ? Repeater 控件是基本模板化數(shù)據(jù)列表。 它不像GridView控件一樣能夠可視化的設(shè)計(jì)格式或樣式,因此開發(fā)時(shí)在控件模板中必須顯式聲明所有格式、格式和樣式標(biāo)記。另外Repeater控件沒有內(nèi)置選擇、排序、編輯、分頁等功能,它只提供了基本的數(shù)據(jù)綁定,但是它為開發(fā)人員提供了ItemCommand 事件,該事件支持在控件中收發(fā)命令。
? ? ? ? 想要綁定數(shù)據(jù),模板是必不可少的,Repeater控件同樣支持?jǐn)?shù)據(jù)模板,而且還可以在模板中添加想要的標(biāo)簽,它主要用法如下圖:
? ? ? ? ? ?Note:每個(gè) Repeater 控件必須定義 ItemTemplate。
二、控件使用技巧
? ? ? 上文講解了Repeater基本的使用方法及它的一些基本特性,接下來做幾個(gè)經(jīng)典的示例來運(yùn)用Repeater控件。
? 1、數(shù)據(jù)綁定之刪除、編輯
? ? ? 該示例將會(huì)使用Asp.net的前臺(tái)和后臺(tái)結(jié)合來實(shí)現(xiàn)顯示數(shù)據(jù),并能夠編輯和刪除數(shù)據(jù)。
? ? ? 刪除頁面:
? ? ?編輯頁面:
? ? ? ?前臺(tái)代碼:在單擊編輯按鈕后將會(huì)進(jìn)入編輯頁面,頁面是由兩個(gè)Panel控件來控制,通過傳遞ID號的方式判斷顯示的是編輯頁面還是刪除頁面,另外前臺(tái)代碼通過設(shè)置控件的CommandArgument屬性來傳遞后臺(tái)所需要判斷的id號。
[html] view plain
copy <body>??????<form?id="form1"?runat="server">??????<div>??????????<asp:Repeater?ID="userRepeat"?runat="server"?OnItemCommand="userRepeat_ItemCommand"?OnItemDataBound="userRepeat_ItemDataBound">??????????????<HeaderTemplate>??????????????????<table?border="1"?style="width:1000px;text-align:center;border-collapse:collapse;">??????????????????????<thead?style="background-color:red;">??????????????????????????<tr>??????????????????????????????<th>ID</th>??????????????????????????????<th>內(nèi)容</th>??????????????????????????????<th>操作</th>??????????????????????????</tr>??????????????????????</thead>??????????????</HeaderTemplate>??????????????<ItemTemplate>??????????????????<asp:Panel?ID="plItem"?runat="server">??????????????????????<tr>??????????????????????????<td><asp:Label?runat="server"?ID="lblID"?Text='<%#Eval("id")?%>'></asp:Label></td>??????????????????????????<td><%#Eval("name")?%></td>??????????????????????????<td>??????????????????????????????<asp:LinkButton?ID="lbtEdit"?CommandName="Edit"?CommandArgument='<%#Eval("id")?%>'?runat="server">編輯</asp:LinkButton>??????????????????????????????<asp:LinkButton?ID="lbtDelete"?CommandName="Delete"?CommandArgument='<%#Eval("id")?%>'?runat="server">刪除</asp:LinkButton>??????????????????????????</td>??????????????????????</tr>??????????????????</asp:Panel>??????????????????<asp:Panel?ID="plEdit"?runat="server">??????????????????????<tr>??????????????????????????<td><asp:Label?runat="server"?ID="Label1"?Text='<%#Eval("id")?%>'></asp:Label></td>??????????????????????????<td><asp:TextBox?ID="txtName"?runat="server"?Text='<%#Eval("name")?%>'></asp:TextBox></td>??????????????????????????<td>??????????????????????????????<asp:LinkButton?ID="lbtCancel"?CommandName="Cancel"?CommandArgument='<%#Eval("id")?%>'?runat="server">取消</asp:LinkButton>??????????????????????????????<asp:LinkButton?ID="lbtUpdate"?CommandName="Update"?CommandArgument='<%#Eval("id")?%>'?runat="server">更新</asp:LinkButton>??????????????????????????</td>??????????????????????</tr>??????????????????</asp:Panel>??????????????</ItemTemplate>??????????????<FooterTemplate>??????????????????</table>??????????????</FooterTemplate>??????????</asp:Repeater>??????</div>??????</form>??</body>??
? ? ? ? 后臺(tái)代碼:在后臺(tái)代碼中很重要的兩個(gè)事件是ItemCommand和ItemDataBound,其中ItemCommand負(fù)責(zé)接收前臺(tái)傳進(jìn)來的按鈕命令,根據(jù)命令的參數(shù)來設(shè)置后臺(tái)傳遞的id,并在ItemDataBound中來驗(yàn)證id判斷切換顯示Panel。
[csharp] view plain
copy using?System;??using?System.Collections.Generic;??using?System.Data;??using?System.Data.SqlClient;??using?System.Web;??using?System.Web.UI;??using?System.Web.UI.WebControls;????namespace?WebApplication4??{??????public?partial?class?EditPage?:?System.Web.UI.Page??????{??????????private?int?id?=?0;?????????????????????????????????????????????????????????????protected?void?Page_Load(object?sender,?EventArgs?e)??????????{??????????????if?(!Page.IsPostBack)??????????????{??????????????????this.DataBindToRepeater();??????????????}??????????}??????????????????????????????????????????private?void?DataBindToRepeater()?{????????????????????????????using?(SqlConnection?sqlCon=new?SqlConnection("server=.;database=MyBlog;uid=sa;pwd=1"))??????????????{??????????????????sqlCon.Open();??????????????????????SqlCommand?sqlcom?=?new?SqlCommand();?????????????????????sqlcom.CommandText?=?"select?*?from?match";???????????????????sqlcom.Connection?=?sqlCon;?????????????????????this.userRepeat.DataSource?=?sqlcom.ExecuteReader();??????????????????????this.userRepeat.DataBind();???????????????}??????????}??????????????????????????????????????????????????????????????protected?void?userRepeat_ItemCommand(object?source,?RepeaterCommandEventArgs?e)??????????{????????????????????????????if?(e.CommandName=="Edit")????????????????{??????????????????id?=?int.Parse(e.CommandArgument.ToString());?????????????????}??????????????else?if?(e.CommandName=="Cancel")?????????????????{??????????????????id?=?-1;??????????????}??????????????else?if(e.CommandName=="Delete")??????????????????{??????????????????id?=?int.Parse(e.CommandArgument.ToString());???????????????????????????????????????this.DeleteRepeater(id);??????????????}??????????????else?if?(e.CommandName?==?"Update")???????????????{????????????????????????????????????string?strText?=?((TextBox)e.Item.FindControl("txtName")).Text.Trim();??????????????????int?intId=int.Parse(((Label)e.Item.FindControl("lblID")).Text);????????????????????????????????????this.UpdateRepeater(strText,intId);??????????????}??????????????????????????????this.DataBindToRepeater();??????????}????????????????????????????????????????????????????private?void?DeleteRepeater(int?intId)?{??????????????using?(SqlConnection?sqlCon?=?new?SqlConnection("server=.;database=MyBlog;uid=sa;pwd=1"))??????????????{??????????????????sqlCon.Open();??????????????????????SqlCommand?sqlcom?=?new?SqlCommand();?????????????????????sqlcom.CommandText?=?"delete?from?match?where?id=@id";???????????????????sqlcom.Connection?=?sqlCon;???????????????????????????????????????SqlParameter?sqlParam?=?new?SqlParameter("@id",?intId);??????????????????sqlcom.Parameters.Add(sqlParam);????????????????????sqlcom.ExecuteNonQuery();???????????????????}??????????}??????????????????????????????????????????????????????????????private?void?UpdateRepeater(string?strText,int?intId)?{??????????????using?(SqlConnection?sqlCon?=?new?SqlConnection("server=.;database=MyBlog;uid=sa;pwd=1"))??????????????{??????????????????sqlCon.Open();??????????????????????SqlCommand?sqlcom?=?new?SqlCommand();?????????????????????sqlcom.CommandText?=?"update?match?set?name=@str?where?id=@id";???????????????????sqlcom.Connection?=?sqlCon;???????????????????????????????????????SqlParameter[]?sqlParam?=?{?new?SqlParameter("@str",?strText),?new?SqlParameter("@id",?intId)?};??????????????????sqlcom.Parameters.AddRange(sqlParam);????????????????????sqlcom.ExecuteNonQuery();???????????????????????????????????}??????????}??????????????????????????????????????????????????????????????protected?void?userRepeat_ItemDataBound(object?sender,?RepeaterItemEventArgs?e)??????????{??????????????????????????????????????????if?(e.Item.ItemType==ListItemType.Item?||?e.Item.ItemType==ListItemType.AlternatingItem)??????????????{??????????????????????????????????????????????????????System.Data.Common.DbDataRecord?record?=?(System.Data.Common.DbDataRecord)e.Item.DataItem;??????????????????????????????????????????????????????????????????????????if?(id?==?int.Parse(record["id"].ToString()))??????????????????{??????????????????????((Panel)e.Item.FindControl("plItem")).Visible?=?false;??????????????????????((Panel)e.Item.FindControl("plEdit")).Visible?=?true;??????????????????}??????????????????else??????????????????{??????????????????????((Panel)e.Item.FindControl("plItem")).Visible?=?true;??????????????????????((Panel)e.Item.FindControl("plEdit")).Visible?=?false;??????????????????}??????????????}??????????}??????}??}??
? ?2、分頁--PageDataSource
? ? ? ? 前臺(tái)代碼:使用原始的html文本,并添加了一個(gè)Literal標(biāo)簽,用來動(dòng)態(tài)添加并指定html標(biāo)簽。
? ? ? ? 頁面截圖:
[html] view plain
copy <html?xmlns="http://www.w3.org/1999/xhtml">??<head?runat="server">??<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"/>??????<title></title>??????<style?type="text/css">????????????.pageBar??????????{??????????????margin-top:?10px;??????????}??????????.pageBar?a??????????{??????????????color:?#333;??????????????font-size:?12px;??????????????margin-right:?10px;??????????????padding:?4px;??????????????border:?1px?solid?#ccc;??????????????text-decoration:?none;??????????}??????</style>??</head>??<body>??????<form?id="form1"?runat="server">??????<div>????????????????????<asp:Repeater?ID="Repeater1"?runat="server"?>??????????????<HeaderTemplate>??????????????????<table?border="1"?cellpadding="0"?cellspacing="0"?style="width:1006px;border-collapse:collapse;?text-align:center;">??????????????????????<tr>??????????????????????????<th?style="background-color:red">ID</th>??????????????????????????<th?style="background-color:red">內(nèi)容</th>??????????????????????</tr>??????????????</HeaderTemplate>??????????????<ItemTemplate>??????????????????<tr>??????????????????????<td><asp:Label?ID="lblId"?runat="server"?Text='<%#?DataBinder.Eval(Container.DataItem,"id")?%>'?></asp:Label></td>??????????????????????<td><%#?DataBinder.Eval(Container.DataItem,"name")?%></td>??????????????????</tr>??????????????</ItemTemplate>??????????????<FooterTemplate>??????????????????</table>??????????????</FooterTemplate>??????????</asp:Repeater>????????????????</div>??????<div?class="pageBar">??????????<asp:Literal?ID="ltlPageBar"?runat="server"></asp:Literal>??????</div>??????</form>??</body>??</html>??
? ? ? ? ?后臺(tái)代碼:Repeater控件的數(shù)據(jù)源是PagedDataSource對象,在頁面加載時(shí)為該對象動(dòng)態(tài)指定了分頁的屬性,并使用Literal標(biāo)簽來動(dòng)態(tài)指定每個(gè)標(biāo)簽跳轉(zhuǎn)頁的鏈接。
[csharp] view plain
copy using?System;??using?System.Collections.Generic;??using?System.Data;??using?System.Data.SqlClient;??using?System.Text;??using?System.Web;??using?System.Web.UI;??using?System.Web.UI.WebControls;????namespace?WebApplication4??{??????public?partial?class?PageDemo?:?System.Web.UI.Page??????{??????????private?string?id?=?"";??????????protected?void?Page_Load(object?sender,?EventArgs?e)??????????{??????????????if?(!Page.IsPostBack)??????????????{????????????????????????????????????int?pageIndex?=?1;??????????????????try??????????????????{????????????????????????????????????????????pageIndex?=?Convert.ToInt32(Request.QueryString["Page"]);????????????????????????????????????????????if?(pageIndex?<=?0)??????????????????????{??????????????????????????pageIndex?=?1;??????????????????????}??????????????????}??????????????????catch??????????????????{??????????????????????pageIndex?=?1;??????????????????}????????????????????DataTable?dt?=?this.GetDataTable();?????????????????????PagedDataSource?pds?=?new?PagedDataSource();??????????????????????pds.DataSource?=?dt.DefaultView;??????????????????????pds.AllowPaging?=?true;???????????????????pds.PageSize?=?2;?????????????????????pds.CurrentPageIndex?=?pageIndex?-?1;???????????????????????????????????????this.Repeater1.DataSource?=?pds;??????????????????this.Repeater1.DataBind();??????????????????????????????????????ltlPageBar.Text?=?this.GetPageBar(pds);??????????????}??????????}??????????????????????????????????????????????????????????????private?string?GetPageBar(PagedDataSource?pds)??????????{??????????????string?pageBar?=?string.Empty;????????????????int?currentPageIndex?=?pds.CurrentPageIndex?+?1;??????????????????????????????????if?(currentPageIndex?==?1)????????????????{??????????????????pageBar?+=?"<a?href=\"javascript:void(0)\">首頁</a>";??????????????}??????????????else???????????????{????????????????????????????????????pageBar?+=?"<a?href=\""?+?Request.CurrentExecutionFilePath?+?"?Page=1\">首頁</a>";??????????????}??????????????????????????????if?((currentPageIndex?-?1)?<?1)???????????????{??????????????????pageBar?+=?"<a?href=\"javascript:void(0)\">上一頁</a>";??????????????}??????????????else??????????????{????????????????????????????????????pageBar?+=?"<a?href=\""?+?Request.CurrentExecutionFilePath?+?"?Page="?+?(currentPageIndex?-?1)?+?"\">上一頁</a>";??????????????}??????????????????????????????if?((currentPageIndex?+?1)?>?pds.PageCount)??????????????{????????????????????????????????????pageBar?+=?"<a?href=\"javascript:void(0)\">下一頁</a>";??????????????}??????????????else??????????????{????????????????????????????????????pageBar?+=?"<a?href=\""?+?Request.CurrentExecutionFilePath?+?"?Page="?+?(currentPageIndex?+?1)?+?"\">下一頁</a>";??????????????}??????????????????????????????if?(currentPageIndex?==?pds.PageCount)??????????????{??????????????????pageBar?+=?"<a?href=\"javascript:void(0)\">末頁</a>";??????????????}??????????????else??????????????{??????????????????pageBar?+=?"<a?href=\""?+?Request.CurrentExecutionFilePath?+?"?Page="?+?pds.PageCount?+?"\">末頁</a>";??????????????}????????????????return?pageBar;???????????}????????????????????????????????????????????????????private?DataTable?GetDataTable()??????????{??????????????DataTable?dt?=?new?DataTable();???????????????using?(SqlConnection?con?=?new?SqlConnection("server=.;DataBase=MyBlog;uid=sa;pwd=1;"))??????????????{????????????????????con.Open();?????????????????????SqlCommand?sqlCom?=?new?SqlCommand();??????????????????????StringBuilder?sqlStr?=?new?StringBuilder();???????????????????sqlStr.Append("select?*?from?match");???????????????????????sqlCom.CommandText?=?sqlStr.ToString();?????????????????????sqlCom.Connection?=?con;??????????????????????SqlDataAdapter?sqlDa?=?new?SqlDataAdapter(sqlCom);????????????????????SqlCommandBuilder?sqlBuilder?=?new?SqlCommandBuilder(sqlDa);??????????????????sqlDa.Fill(dt);???????????????}????????????????return?dt;??????????}????????}??}??
? ? ? ?
上文Demo下載地址:Repeater使用技巧。
結(jié)語
? ? ? ? ?文章主要介紹了Repeater控件的基本使用方法,并通過兩個(gè)示例來更深一步的學(xué)習(xí)了Repeater控件的使用。雖然Repeater控件封裝的操作較少,但它是最基礎(chǔ)的數(shù)據(jù)綁定控件,另外可以通過使用其它控件來彌補(bǔ)Repeater控件的不足,如可以通過使用PagedataSource類來實(shí)現(xiàn)數(shù)據(jù)的分頁。文章寫到這里并沒有結(jié)束,下篇文章將會(huì)著重討論ListView。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的数据绑定控件之Repeater的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。