生活随笔
收集整理的這篇文章主要介紹了
数据绑定控件之Repeater
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前幾篇的文章在說AJAX的內容,利用AJAX技術能夠開發出高效運行的網站應用程序,不過在進行B/S項目開發時只擁有AJAX技術是遠遠不夠的,踏入到B/S要學的東西會更多,但相較C/S的復雜邏輯結構來說B/S在開發時還是很簡單的。
? ? ? ? 在開發B/S項目時,常常會用到數據綁定控件,.NET平臺已經對這些控件進行了良好的封裝,只要稍有經驗的程序猿很快就能夠上手使用這些數據控件,所以接下來的幾篇文章將會討論數據控件,首先將會從數據控件的細節入手討論ListView、GridView、Repeater、DataList控件的基本使用方法,并在會后系列文章的最后對這幾個控件進行綜合性的分析總結。
一、綁定控件之Repeater
??????? .NET封裝了多種數據綁定控件,諸如GridView、DataList等但該篇文章將會從Repeater入手,因為Repeater只提供了基本的數據綁定模板,沒有內置其它分頁等功能,所以它是最原始的數據綁定控件,只要能夠熟練運用Repeater控件其它的綁定控件也就很簡單了。
? 1、Repeater簡介
? ? ? ? Repeater 控件是基本模板化數據列表。 它不像GridView控件一樣能夠可視化的設計格式或樣式,因此開發時在控件模板中必須顯式聲明所有格式、格式和樣式標記。另外Repeater控件沒有內置選擇、排序、編輯、分頁等功能,它只提供了基本的數據綁定,但是它為開發人員提供了ItemCommand 事件,該事件支持在控件中收發命令。
? ? ? ? 想要綁定數據,模板是必不可少的,Repeater控件同樣支持數據模板,而且還可以在模板中添加想要的標簽,它主要用法如下圖:
? ? ? ? ? ?Note:每個 Repeater 控件必須定義 ItemTemplate。
二、控件使用技巧
? ? ? 上文講解了Repeater基本的使用方法及它的一些基本特性,接下來做幾個經典的示例來運用Repeater控件。
? 1、數據綁定之刪除、編輯
? ? ? 該示例將會使用Asp.net的前臺和后臺結合來實現顯示數據,并能夠編輯和刪除數據。
? ? ? 刪除頁面:
? ? ?編輯頁面:
? ? ? ?前臺代碼:在單擊編輯按鈕后將會進入編輯頁面,頁面是由兩個Panel控件來控制,通過傳遞ID號的方式判斷顯示的是編輯頁面還是刪除頁面,另外前臺代碼通過設置控件的CommandArgument屬性來傳遞后臺所需要判斷的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>內容</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>??
? ? ? ? 后臺代碼:在后臺代碼中很重要的兩個事件是ItemCommand和ItemDataBound,其中ItemCommand負責接收前臺傳進來的按鈕命令,根據命令的參數來設置后臺傳遞的id,并在ItemDataBound中來驗證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
? ? ? ? 前臺代碼:使用原始的html文本,并添加了一個Literal標簽,用來動態添加并指定html標簽。
? ? ? ? 頁面截圖:
[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">內容</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>??
? ? ? ? ?后臺代碼:Repeater控件的數據源是PagedDataSource對象,在頁面加載時為該對象動態指定了分頁的屬性,并使用Literal標簽來動態指定每個標簽跳轉頁的鏈接。
[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使用技巧。
結語
? ? ? ? ?文章主要介紹了Repeater控件的基本使用方法,并通過兩個示例來更深一步的學習了Repeater控件的使用。雖然Repeater控件封裝的操作較少,但它是最基礎的數據綁定控件,另外可以通過使用其它控件來彌補Repeater控件的不足,如可以通過使用PagedataSource類來實現數據的分頁。文章寫到這里并沒有結束,下篇文章將會著重討論ListView。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的数据绑定控件之Repeater的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。