




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、中Repeater控件用法筆記_ 大家可能都對datagrid比較熟識,但是假如在數(shù)據(jù)量大的時候,我們就得考慮用法repeater作為我們的數(shù)據(jù)綁定控件了。Repeater控件與DataGrid (以及DataList)控件的主要區(qū)分是在于如何處理HTML。ASP.NET建立HTML代碼以顯示DataGrid控件,但Repeater允許開發(fā)人員決定如何顯示數(shù)據(jù)。所以,你可以選擇將數(shù)據(jù)顯示在一個HTML表格中或者一個挨次列表中。這主要取決于你的選擇,但你必需將正確的HTML插入到 ASP.NET頁面中。 模板與DataList一樣,Repeater控件只支持模板。以下的模板可供選擇: Alter
2、natingItemTemplate: 指定如何顯示每一其它選項。 ItemTemplate: 指定如何顯示選項。(AlternatingItemTemplate可以掩蓋這一模板。) HeaderTemplate: 建立如何顯示標題。 FooterTemplate: 建立如何顯示頁腳。 SeparatorTemplate: 指定如何顯示不同選項之間的分隔符。 你可以用法這些模板來顯示你盼望的數(shù)據(jù)。唯一具有強制性的模板是ItemTemplate,全部其它的模板都是具有選擇性的。 對于處理一個數(shù)據(jù)源,Repeater控件具有與DataGrid與DataList相同的屬性: DataMember:獲
3、得或者設置與 Repeater 控件綁定的相應DataSource屬性的表格。 DataSource:獲得或者設置為 Repeater 顯示供應數(shù)據(jù)的數(shù)據(jù)源。 除此之外,還有一個Items屬性,你可以通過這一屬性編程訪問Repeater數(shù)據(jù)中單一選項。它返回一個RepeaterItemCollection對象,為一組RepeaterItem對象的集合,代表 Repeater 數(shù)據(jù)的每一行。 ASP.NET Web數(shù)據(jù)控件還有其它一個共性:它們都用法DataBind方法來生成用戶界面。調用這一方法可以返回并顯示數(shù)據(jù)(假設DataSource和 DataMember屬性設置正確)。在查看DataB
4、ind方法之前,我們先看看如何在一個Web頁面中用法一個Repeater控件。 用法Repeater控件 用法Repeater控件的第一步驟是決定我們將要用法的數(shù)據(jù)源和字段。例如,我們將要用法SQL Server Northwind數(shù)據(jù)庫中的Employees列表。Web頁面將顯示職工的完整名字,地址,以及電話號碼。HTML將用法DIV標記,用 Repeater 模板來分隔內容。下面是 Web 頁面的 HTML 內容: 前臺代碼 代碼如下: asp:Repeater ID=Repeater1 runat=server HeaderTemplatetable cellpadding=0 cell
5、spacing=0 border=1 align=center class=auto-style1 tr align=left th編號/th th姓名/th th年齡/th th班級/th th零花/th /tr /HeaderTemplate ItemTemplate tr td%#Eval(id)%/td td%#Eval(name)%/td td%#Eval(age)%/td td%#Eval(classid)%/td td%#Eval(salary)%/td /tr /ItemTemplate FooterTemplate/table/FooterTemplate /asp:Repe
6、ater 后臺代碼 代碼如下: string sql = string.Format(select * from person); DataTable dt = sqlhelper.helper.ExecuteQuery(sql); Repeater1.DataSource = dt; Repeater1.DataBind(); AspNetpager的用法方法: 首先注冊% Register Assembly=AspNetPager Namespace=Wuqi.Webdiyer TagPrefix=webdiyer % 前臺: 代碼如下: webdiyer:AspNetPager ID=A
7、spNetPager1 runat=server CustomInfoHTML=第%CurrentPageIndex%頁,共%PageCount%頁,每頁%PageSize%條 FirstPageText=首頁 LastPageText=尾頁 LayoutType=Table NextPageText=下一頁 onpagechanging=AspNetPager1_PageChanging PageIndexBoxType=DropDownList PagingButtonLayoutType=Span PrevPageText=上一頁 ShowCustomInfoSection=Left S
8、howPageIndexBox=Always SubmitButtonText=Go TextAfterPageIndexBox=頁 TextBeforePageIndexBox=轉到 Width=450px PageSize=3 /webdiyer:AspNetPager 后臺: 代碼如下: private void AddPages(Repeater rpt,Wuqi.Webdiyer.AspNetPager anp,DataTable dt) PagedDataSource pds = new PagedDataSource(); pds.AllowPaging = true; pds.
9、DataSource = dt.DefaultView; anp.RecordCount = dt.Rows.Count;/給分頁控件指定當前總數(shù) pds.PageSize = anp.PageSize;/確定分頁,每頁有n項 pds.CurrentPageIndex = anp.CurrentPageIndex - 1;/確定當前頁數(shù) rpt.DataSource = pds;/綁定數(shù)據(jù)源 rpt.DataBind(); protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventAr
10、gs e) AspNetPager1.CurrentPageIndex = e.NewPageIndex; BindPersonData(); 例子 repeater控件動態(tài)添加、刪除一行 代碼如下: script type=text/javascript var txtEquipmentIdsIDArray = new Array(); function OpenWindow(clientId, TypeId, ReqNum) var url = ./EquipmentIssue/EquipmentRequestIssue.aspx?TypeId= + TypeId + ReqNum= +
11、ReqNum; var widths = 600; var heigths = 450; var winPar = window.showModalDialog(url, window, dialogWidth= + widths + px;dialogHeight= + heigths + px;status=no;center=yes;scroll=no;help:No;); if (winPar != undefined) var txtEquipmentIds = document.getElementById(clientId); txtEquipmentIds.value = wi
12、nPar; / /審批用戶控件中保存發(fā)放的EquipmentId / var IsExistEquipmentIdsID = false; / for (var i = 0; i txtEquipmentIdsIDArray.length; i+) / if (txtEquipmentIdsIDArrayi = clientId) / IsExistEquipmentIdsID = true; / / / if (IsExistEquipmentIdsID = false) / txtEquipmentIdsIDArray.push(clientId); / / var tempEquipme
13、ntId = ; / for (var i = 0; i txtEquipmentIdsIDArray.length; i+) / tempEquipmentId = tempEquipmentId + document.getElementById(txtEquipmentIdsIDArrayi).value; / var hiddenEquipmentIds = document.getElementById(UC_ApprovalAction1_hfEquipmentIds); / hiddenEquipmentIds.value = tempEquipmentId; / / /aler
14、t(hiddenEquipmentIds.value); var tempReturnValue = ; var tb_Request = document.getElementById(tb_Request); var tr = tb_Request.getElementsByTagName(tr); for (var i = 0; i tr.length; i+) if (tri.id != ) var span_EquRequestItemId = tri.getElementsByTagName(span); /獵取申請明細的IDEquRequestItemId var select_
15、ddlStation; /獵取用法工位StationId var option = tri.getElementsByTagName(select)1.getElementsByTagName(option); for (var j = 0; j option.length; j+) if (optionj.selected) select_ddlStation = optionj var textarea_EquipmentIds = tri.getElementsByTagName(textarea); /獵取發(fā)放的資產號EquipmentNo tempReturnValue = temp
16、ReturnValue + span_EquRequestItemId0.innerText + : + select_ddlStation.value + : + textarea_EquipmentIds0.innerText + |; var hiddenEquipmentIds = document.getElementById(UC_ApprovalAction1_hfEquipmentIds); hiddenEquipmentIds.value = tempReturnValue; /alert(hiddenEquipmentIds.value); /script Repeater
17、: 代碼如下: div id=div_Repeater asp:HiddenField ID=hfRptColumns runat=server Value=Guid,EquRequestItemId,EquipmentType,Station,EquipmentNum,EquipmentIds / table id=tb_Request cellpadding=1 cellspacing=0 width=100% style=background-color: #DFE8F6; font-size:12px; padding:10px; thead tr th序號/th th明細編號/th
18、th 資產類型 /th th 用法工位 /th th 申請數(shù)量 /th th 發(fā)放的資產號font color=red(資產管理員填寫)/font /th /tr /thead tbody asp:Repeater ID=rptRequest runat=server onitemcommand=rptRequest_ItemCommand onitemdatabound=rptRequest_ItemDataBound ItemTemplate tr id=tr_Request td %# Container.ItemIndex+1 % asp:Label ID=lblGuid runat=
19、server Text=%#Eval(Guid) % Visible=false/asp:Label /td tdasp:Label ID=lblEquRequestItemId runat=server Text=%#Eval(EquRequestItemId) %/asp:Label/td td asp:DropDownList ID=ddlEquipmentType runat=server/asp:DropDownList asp:Label ID=lblEquipmentType runat=server Text=%#Eval(EquipmentType) % Visible=fa
20、lse/asp:Label /td td asp:DropDownList ID=ddlStation runat=server/asp:DropDownList asp:Label ID=lblStation runat=server Text=%#Eval(Station) % Visible=false/asp:Label /td tdasp:TextBox ID=txtReqEquipmentNum runat=server Text=%#Eval(EquipmentNum) %/asp:TextBox/td tdasp:TextBox ID=txtEquipmentIds runat
21、=server Text=%#Eval(EquipmentIds) % TextMode=MultiLine/asp:TextBox/td tdasp:Button ID=btnAddRow runat=server Text=新增一行 CommandName=add / nbsp;nbsp;nbsp;nbsp;asp:Button ID=btnDeleteRow runat=server Text=刪除本行 CommandName=delete /td /tr /ItemTemplate /asp:Repeater /tbody /table /div 后臺: 代碼如下: / summary
22、 / 綁定repeater的數(shù)據(jù)源 / /summary private void RepeaterBindData() DataTable dt = DefineDataTableSchema(hfRptColumns.Value); if (RequestBusinessNo = null) LoadData(dt); else LoadData(RequestBusinessNo.ToString(), dt); rptRequest.DataSource = dt; rptRequest.DataBind(); private void LoadData(string business
23、No, DataTable dt) string strSql = select * from EMS_EquipmentRequestItem where BussinessNo = + businessNo +; DataTable dt_EquipmentRequestItem = DBUtility.DbHelperSQL.Query(strSql).Tables0; /for (int i = 0; i dt_EquipmentRequestItem.Rows.Count; i+) foreach (DataRow dr in dt_EquipmentRequestItem.Rows
24、) DataRow row = dt.NewRow(); rowGuid = Guid.NewGuid(); rowEquRequestItemId = drEquRequestItemId.ToString(); rowEquipmentType = drTypeId.ToString(); rowStation = drStationId.ToString(); rowEquipmentNum = drEquipmentNum.ToString(); rowEquipmentIds = drEquipmentIds.ToString(); dt.Rows.Add(row); / summa
25、ry / repeater數(shù)據(jù)默認加載 / /summary / param name=dt/param private void LoadData(DataTable dt) /默認顯示1行 for (int i = 0; i 1; i+) DataRow row = dt.NewRow(); dt.Rows.Add(row); /為第一行加載一些數(shù)據(jù) DataRow row0 = dt.Rows0; row0Guid = Guid.NewGuid(); row0EquRequestItemId = ; row0EquipmentType = ; row0Station = ; row0Eq
26、uipmentNum = 1;/默認初始為1 row0EquipmentIds = ; / summary / 依據(jù)repeater相對應的列名,定義數(shù)據(jù)源datatable的schema / /summary / param name=columns列名/param / returns/returns public DataTable DefineDataTableSchema(string columns) DataTable dt = new DataTable(); string columnsAry = columns.Split(,); foreach (string str in
27、 columnsAry) dt.Columns.Add(str); return dt; protected void rptRequest_ItemCommand(object source, RepeaterCommandEventArgs e) if (e.CommandName = add) System.Web.UI.WebControls.Label lblGuid = (System.Web.UI.WebControls.Label)e.Item.FindControl(lblGuid); /首先,復原數(shù)據(jù)源 DataTable dt = DefineDataTableSchem
28、a(hfRptColumns.Value); foreach (RepeaterItem item in rptRequest.Items) DataRow newRow = dt.NewRow(); newRowGuid = (System.Web.UI.WebControls.Label)item.FindControl(lblGuid).Text; newRowEquRequestItemId = (System.Web.UI.WebControls.Label)item.FindControl(lblEquRequestItemId).Text; newRowEquipmentType
29、 = (DropDownList)item.FindControl(ddlEquipmentType).SelectedValue; newRowStation = (DropDownList)item.FindControl(ddlStation).SelectedValue; newRowEquipmentNum = (TextBox)item.FindControl(txtReqEquipmentNum).Text; newRowEquipmentIds = (TextBox)item.FindControl(txtEquipmentIds).Text; dt.Rows.Add(newR
30、ow); if (lblGuid.Text = (System.Web.UI.WebControls.Label)item.FindControl(lblGuid).Text) /添加一行 DataRow row = dt.NewRow(); rowGuid = Guid.NewGuid(); rowEquipmentType = ; rowStation = ; rowEquipmentNum = 1;/默認初始為1 rowEquipmentIds = ; dt.Rows.Add(row); rptRequest.DataSource = dt; rptRequest.DataBind();
31、 else if (e.CommandName = delete) System.Web.UI.WebControls.Label lblGuid = (System.Web.UI.WebControls.Label)e.Item.FindControl(lblGuid); /首先,復原數(shù)據(jù)源 DataTable dt = DefineDataTableSchema(hfRptColumns.Value); foreach (RepeaterItem item in rptRequest.Items) if (lblGuid.Text != (System.Web.UI.WebControls
32、.Label)item.FindControl(lblGuid).Text) DataRow newRow = dt.NewRow(); newRowGuid = (System.Web.UI.WebControls.Label)item.FindControl(lblGuid).Text; newRowEquRequestItemId = (System.Web.UI.WebControls.Label)item.FindControl(lblEquRequestItemId).Text; newRowEquipmentType = (DropDownList)item.FindContro
33、l(ddlEquipmentType).SelectedValue; newRowStation = (DropDownList)item.FindControl(ddlStation).SelectedValue; newRowEquipmentNum = (TextBox)item.FindControl(txtReqEquipmentNum).Text; newRowEquipmentIds = (TextBox)item.FindControl(txtEquipmentIds).Text; dt.Rows.Add(newRow); rptRequest.DataSource = dt;
34、 rptRequest.DataBind(); protected void rptRequest_ItemDataBound(object sender, RepeaterItemEventArgs e) if (e.Item.ItemType = ListItemType.Item | e.Item.ItemType = ListItemType.AlternatingItem) DropDownList ddlEquipmentType_temp = e.Item.FindControl(ddlEquipmentType) as DropDownList; string sqlstr =
35、 select distinct b.TypeId,b.EquipmentName + _ + b.EquipmentType as EquipmentName_EquipmentType from EMS_EquipmentInfo a join EMS_EquipmentType b on a.TypeId = b.TypeId and b.Status = 1 order by EquipmentName_EquipmentType; DataSet ds = DBUtility.DbHelperSQL.Query(sqlstr); ddlEquipmentType_temp.DataT
36、extField = EquipmentName_EquipmentType; ddlEquipmentType_temp.DataValueField = TypeId; ddlEquipmentType_temp.DataSource = ds; ddlEquipmentType_temp.DataBind(); ddlEquipmentType_temp.SelectedValue = (e.Item.FindControl(lblEquipmentType) as System.Web.UI.WebControls.Label).Text; DropDownList ddlStatio
37、n_temp = e.Item.FindControl(ddlStation) as DropDownList; string sqlstr2 = select NodeId,Line + _ + StationName as Line_StationName from V_Stations where status=1 order by Line_StationName; DataSet ds2 = DBUtility.DbHelperSQL.Query(sqlstr2); ddlStation_temp.DataTextField = Line_StationName; ddlStatio
38、n_temp.DataValueField = NodeId; ddlStation_temp.DataSource = ds2; ddlStation_temp.DataBind(); ddlStation_temp.SelectedValue = (e.Item.FindControl(lblStation) as System.Web.UI.WebControls.Label).Text; TextBox txtEquIds = e.Item.FindControl(txtEquipmentIds) as TextBox; txtEquIds.Attributes.Add(readonly, true); if (RequestBusinessNo != null) (e.Item.FindControl(btnAddRow) as System.Web.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年軍隊文職人員招聘之軍隊文職法學提升訓練試卷A卷附答案
- 水利水電工程的質量提升路徑與試題及答案
- 2025年教師資格之小學教育學教育心理學真題練習試卷B卷附答案
- 《圍城》名句賞析
- 水利水電工程考試復習的有效途徑試題及答案
- 金科新未來河南2025屆高三5月聯(lián)考歷史試卷(含答案)
- 大學學生會部門工作計劃
- 2025年湖南彬州農村黨務(村務)工作者公開招聘考試試題含答案
- 行政管理中的公共關系信任構建試題及答案
- 2025年企業(yè)主要負責人安全培訓考試試題帶答案(培優(yōu)B卷)
- 江西新定額2017土建定額說明及解釋
- 國家電網(wǎng)有限公司十八項電網(wǎng)重大反事故措施(修訂版)-2018版(word文檔良心出品)
- 2019年重慶江津小升初數(shù)學真題及答案
- 《菱形的判定》教學設計(共3頁)
- 部編版三下語文《宇宙的另一邊》教學課件PPT
- 電纜井工程量計算
- 《工程勘察設計收費管理規(guī)定》計價格200210號文
- 育種學 第6章雜交育種
- 附件一∶ 教育部專家實地評估案頭必備材料
- 火災撲救記錄表
- 鋼芯鋁絞線參數(shù)
評論
0/150
提交評論