足球游戏_中国足彩网¥体育资讯$

asp.net中repeater控件用法笔记
来源:易贤网 阅读:558 次 日期:2015-01-19 13:28:11
温馨提示:易贤网小编为您整理了“asp.net中repeater控件用法笔记”,方便广大网友查阅!

大家可能都对datagrid比较熟悉,但是如果在数据量大的时候,我们就得考虑使用repeater作为我们的数据绑定控件了。repeater控件与datagrid (以及datalist)控件的主要区别是在于如何处理html。asp.net建立html代码以显示datagrid控件,但repeater允许开发人员决定如何显示数据。所以,你可以选择将数据显示在一个html表格中或者一个顺序列表中。这主要取决于你的选择,但你必须将正确的html插入到 asp.net页面中。

模板与datalist一样,repeater控件只支持模板。以下的模板可供选择:

alternatingitemtemplate: 指定如何显示每一其它选项。

itemtemplate: 指定如何显示选项。(alternatingitemtemplate可以覆盖这一模板。)

headertemplate: 建立如何显示标题。

footertemplate: 建立如何显示页脚。

separatortemplate: 指定如何显示不同选项之间的分隔符。

你可以使用这些模板来显示你希望的数据。唯一具有强制性的模板是itemtemplate,所有其它的模板都是具有选择性的。

对于处理一个数据源,repeater控件具有与datagrid与datalist相同的属性:

datamember:获得或者设置与 repeater 控件绑定的相应datasource属性的表格。

datasource:获得或者设置为 repeater 显示提供数据的数据源。

除此之外,还有一个items属性,你可以通过这一属性编程访问repeater数据中单一选项。它返回一个repeateritemcollection对象,为一组repeateritem对象的集合,代表 repeater 数据的每一行。

asp.net web数据控件还有其它一个共性:它们都使用databind方法来生成用户界面。调用这一方法可以返回并显示数据(假设datasource和 datamember属性设置正确)。在查看databind方法之前,我们先看看如何在一个web页面中使用一个repeater控件。

使用repeater控件

使用repeater控件的第一步骤是决定我们将要使用的数据源和字段。例如,我们将要使用sql server northwind数据库中的employees列表。web页面将显示职工的完整名字,地址,以及电话号码。html将使用div标记,用 repeater 模板来分隔内容。下面是 web 页面的 html 内容:

前台代码

代码如下:

<asp:repeater id=repeater1 runat=server>

<headertemplate><table cellpadding=0 cellspacing=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:repeater>

后台代码

代码如下:

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=aspnetpager1 runat=server custominfohtml=第%currentpageindex%页,共%pagecount%页,每页%pagesize%条

firstpagetext=首页 lastpagetext=尾页 layouttype=table nextpagetext=下一页

onpagechanging=aspnetpager1_pagechanging pageindexboxtype=dropdownlist

pagingbuttonlayouttype=span prevpagetext=上一页 showcustominfosection=left

showpageindexbox=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.datasource = dt.defaultview;

anp.recordcount = dt.rows.count;//给分页控件指定当前总数

pds.pagesize = anp.pagesize;//确定分页,每页有n项

pds.currentpageindex = anp.currentpageindex - 1;//确定当前页数

rpt.datasource = pds;//绑定数据源

rpt.databind();

}

protected void aspnetpager1_pagechanging(object src, wuqi.webdiyer.pagechangingeventargs e)

{

aspnetpager1.currentpageindex = e.newpageindex;

bindpersondata();

}

例子

repeater控件动态添加、删除一行

代码如下:

<script type=text/javascript>

var txtequipmentidsidarray = new array();

function openwindow(clientid, typeid, reqnum) {

var url = ../equipmentissue/equipmentrequestissue.aspx?typeid= + typeid + &reqnum= + 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 = winpar;

// //审批用户控件中保存发放的equipmentid

// var isexistequipmentidsid = false;

// for (var i = 0; i < txtequipmentidsidarray.length; i++) {

// if (txtequipmentidsidarray[i] == clientid) {

// isexistequipmentidsid = true;

// }

// }

// if (isexistequipmentidsid == false) {

// txtequipmentidsidarray.push(clientid);

// }

// var tempequipmentid = ;

// for (var i = 0; i < txtequipmentidsidarray.length; i++) {

// tempequipmentid = tempequipmentid + document.getelementbyid(txtequipmentidsidarray[i]).value;

// var hiddenequipmentids = document.getelementbyid(uc_approvalaction1_hfequipmentids);

// hiddenequipmentids.value = tempequipmentid;

// }

// //alert(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 (tr[i].id != ) {

var span_equrequestitemid = tr[i].getelementsbytagname(span); //获取申请明细的idequrequestitemid

var select_ddlstation; //获取使用工位stationid

var option = tr[i].getelementsbytagname(select)[1].getelementsbytagname(option);

for (var j = 0; j < option.length; j++) {

if (option[j].selected)

{

select_ddlstation = option[j]

}

}

var textarea_equipmentids = tr[i].getelementsbytagname(textarea); //获取发放的资产号equipmentno

tempreturnvalue = tempreturnvalue + span_equrequestitemid[0].innertext + : + select_ddlstation.value + : + textarea_equipmentids[0].innertext + |;

}

}

var hiddenequipmentids = document.getelementbyid(uc_approvalaction1_hfequipmentids);

hiddenequipmentids.value = tempreturnvalue;

//alert(hiddenequipmentids.value);

}

</script>

repeater:

代码如下:

<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>

<th>

资产类型

</th>

<th>

使用工位

</th>

<th>

申请数量

</th>

<th>

发放的资产号<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=server text='<%#eval(guid) %>' visible=false></asp:label>

</td>

<td><asp: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=false></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>

<td><asp:textbox id=txtreqequipmentnum runat=server text='<%#eval(equipmentnum) %>'></asp:textbox></td>

<td><asp:textbox id=txtequipmentids runat=server text='<%#eval(equipmentids) %>' textmode=multiline></asp:textbox></td>

<td><asp: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>

/// 绑定repeater的数据源

/// </summary>

private void repeaterbinddata()

{

datatable dt = definedatatableschema(hfrptcolumns.value);

if (request[businessno] == null)

{

loaddata(dt);

}

else

{

loaddata(request[businessno].tostring(), dt);

}

rptrequest.datasource = dt;

rptrequest.databind();

}

private void loaddata(string businessno, datatable dt)

{

string strsql = select * from ems_equipmentrequestitem where bussinessno = '+ businessno +';

datatable dt_equipmentrequestitem = dbutility.dbhelpersql.query(strsql).tables[0];

//for (int i = 0; i < dt_equipmentrequestitem.rows.count; i++)

foreach (datarow dr in dt_equipmentrequestitem.rows)

{

datarow row = dt.newrow();

row[guid] = guid.newguid();

row[equrequestitemid] = dr[equrequestitemid].tostring();

row[equipmenttype] = dr[typeid].tostring();

row[station] = dr[stationid].tostring();

row[equipmentnum] = dr[equipmentnum].tostring();

row[equipmentids] = dr[equipmentids].tostring();

dt.rows.add(row);

}

}

/// <summary>

/// repeater数据默认加载

/// </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);

}

//为第一行加载一些数据

datarow row0 = dt.rows[0];

row0[guid] = guid.newguid();

row0[equrequestitemid] = ;

row0[equipmenttype] = ;

row0[station] = ;

row0[equipmentnum] = 1;//默认初始为1

row0[equipmentids] = ;

}

/// <summary>

/// 根据repeater相对应的列名,定义数据源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 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);

//首先,恢复数据源

datatable dt = definedatatableschema(hfrptcolumns.value);

foreach (repeateritem item in rptrequest.items)

{

datarow newrow = dt.newrow();

newrow[guid] = ((system.web.ui.webcontrols.label)item.findcontrol(lblguid)).text;

newrow[equrequestitemid] = ((system.web.ui.webcontrols.label)item.findcontrol(lblequrequestitemid)).text;

newrow[equipmenttype] = ((dropdownlist)item.findcontrol(ddlequipmenttype)).selectedvalue;

newrow[station] = ((dropdownlist)item.findcontrol(ddlstation)).selectedvalue;

newrow[equipmentnum] = ((textbox)item.findcontrol(txtreqequipmentnum)).text;

newrow[equipmentids] = ((textbox)item.findcontrol(txtequipmentids)).text;

dt.rows.add(newrow);

if (lblguid.text == ((system.web.ui.webcontrols.label)item.findcontrol(lblguid)).text)

{

//添加一行

datarow row = dt.newrow();

row[guid] = guid.newguid();

row[equipmenttype] = ;

row[station] = ;

row[equipmentnum] = 1;//默认初始为1

row[equipmentids] = ;

dt.rows.add(row);

}

}

rptrequest.datasource = dt;

rptrequest.databind();

}

else if (e.commandname == delete)

{

system.web.ui.webcontrols.label lblguid = (system.web.ui.webcontrols.label)e.item.findcontrol(lblguid);

//首先,恢复数据源

datatable dt = definedatatableschema(hfrptcolumns.value);

foreach (repeateritem item in rptrequest.items)

{

if (lblguid.text != ((system.web.ui.webcontrols.label)item.findcontrol(lblguid)).text)

{

datarow newrow = dt.newrow();

newrow[guid] = ((system.web.ui.webcontrols.label)item.findcontrol(lblguid)).text;

newrow[equrequestitemid] = ((system.web.ui.webcontrols.label)item.findcontrol(lblequrequestitemid)).text;

newrow[equipmenttype] = ((dropdownlist)item.findcontrol(ddlequipmenttype)).selectedvalue;

newrow[station] = ((dropdownlist)item.findcontrol(ddlstation)).selectedvalue;

newrow[equipmentnum] = ((textbox)item.findcontrol(txtreqequipmentnum)).text;

newrow[equipmentids] = ((textbox)item.findcontrol(txtequipmentids)).text;

dt.rows.add(newrow);

}

}

rptrequest.datasource = dt;

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 = @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.datatextfield = 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 ddlstation_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;

ddlstation_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 (request[businessno] != null)

{

(e.item.findcontrol(btnaddrow) as system.web.ui.webcontrols.button).visible = false;

(e.item.findcontrol(btndeleterow) as system.web.ui.webcontrols.button).visible = false;

string businessno = request[businessno].tostring();

bool isapplyuser = is_applyuser(session[currentuserid].tostring(), businessno, ems_equipmentrequest, requester);//是否为申请人本人

if (isapplyuser == true)

{

string requeststatus = httputility.urldecode(request[requeststatus].tostring(), system.text.encoding.utf8);//申请单的当前状态

if (requeststatus == 草稿 || requeststatus == 退回)

{

(e.item.findcontrol(btnaddrow) as system.web.ui.webcontrols.button).visible = true;

(e.item.findcontrol(btndeleterow) as system.web.ui.webcontrols.button).visible = true;

}

}

else

{

bool iscurrentappprovaler = is_currentappprovaler(session[currentuserid].tostring(), businessno);//是否为当前能做审批的人

if (iscurrentappprovaler == true)

{

string sql = select top 1 layername from ems_approvaler where bussinessno=' + businessno + '

+ and approvalstatus='n' order by appsequence ;

datatable dt = dbutility.dbhelpersql.query(sql).tables[0];

if (dt.rows.count != 0 && dt.rows[0][layername].tostring() == 资产管理员)

{

string reqnum = (e.item.findcontrol(txtreqequipmentnum) as textbox).text.trim();

txtequids.attributes.add(onclick, openwindow(' + txtequids.clientid + ',' + ddlequipmenttype_temp.selectedvalue + ',' + reqnum + '));

}

}

}

}

}

}

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网页制作
易贤网手机网站地址:asp.net中repeater控件用法笔记
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标