ASP.NET中repeater控件用法实例
本文实例讲述了asp.net中repeater控件用法。分享给大家供大家参考。具体实现方法如下:
repeater绑定数据:
{
if(!ispostback)
bindstudent();
}
private void bindstudent()
{
string str = configurationmanager.connectionstrings["stucnn"].connectionstring;
using (sqlconnection sqlcnn = new sqlconnection(str))
{
using (sqldataadapter da = new sqldataadapter("select * from student", sqlcnn))
{
dataset ds = new dataset();
da.fill(ds);
this.repeater1.datasource = ds;
this.repeater1.databind();
}
}
}
删除数据:
{
if (e.commandname == "delete")
{
string str = configurationmanager.connectionstrings["stucnn"].connectionstring;
using (sqlconnection sqlcnn = new sqlconnection(str))
{
using (sqlcommand sqlcmm = sqlcnn.createcommand())
{
sqlcnn.open();
sqlcmm.commandtext = "delete from student where sid="
+ e.commandargument.tostring();
sqlcmm.executenonquery();
}
}
this.bindstudent();
}
else if (e.commandname == "edit")
{
server.transfer("edit.aspx?sid=" + e.commandargument.tostring());
}
}
前台:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%--<asp:repeater id="repeater1" runat="server">
<headertemplate>
<hr />
</headertemplate>
<itemtemplate><div>
<asp:label id="lblsid" runat="server" text='<%# eval("sid") %>'></asp:label>
<asp:label id="lblsname" runat='server' text='<%# eval("sname") %>'></asp:label>
<asp:image id="imgphoto" runat="server" imageurl='<%# eval("photo") %>' /></div>
</itemtemplate>
<separatortemplate>
<hr />
</separatortemplate>
<alternatingitemtemplate>
<div style="
<asp:label id="lblsid" runat="server" text='<%# eval("sid") %>'></asp:label>
<asp:label id="lblsname" runat='server' text='<%# eval("sname") %>'></asp:label>
<asp:image id="imgphoto" runat="server" imageurl='<%# eval("photo") %>' /></div>
</alternatingitemtemplate>
<footertemplate><hr /></footertemplate>
</asp:repeater>--%>
<div>
<asp:repeater id="repeater1" runat="server"
onitemcommand="repeater1_itemcommand">
<headertemplate><table>
<tr><td style="width:100px">编号</td><td style="width:100px">姓名</td>
<td style="width:100px">图片</td><td> </td><td> </td></tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%# eval("sid") %></td><td><%# eval("sname") %></td>
<td><img width="60px" height="60px" src='<%# "images/" + eval("photo") %>' /></td>
<td><asp:linkbutton id="btndelete" runat="server" text="删除" commandname='delete' commandargument='<%# eval("sid") %>'></asp:linkbutton></td>
<td><asp:linkbutton id="btnedit" runat="server" text="编辑" commandname='edit' commandargument='<%# eval("sid") %>'></asp:linkbutton></td>
</tr>
</itemtemplate>
<alternatingitemtemplate>
<tr style="
<td><%# eval("sid") %></td><td><%# eval("sname") %></td>
<td><img width="60px" height="60px" src='<%# "images/" + eval("photo") %>' /></td>
<td><asp:linkbutton id="btndelete" runat="server" text="删除" commandname='delete' commandargument='<%# eval("sid") %>'></asp:linkbutton></td>
<td><asp:linkbutton id="btnedit" runat="server" text="编辑" commandname='edit' commandargument='<%# eval("sid") %>'></asp:linkbutton></td>
</tr>
</alternatingitemtemplate>
<separatortemplate>
<tr><td colspan="5"><hr /></td></tr>
</separatortemplate>
<footertemplate></table></footertemplate>
</asp:repeater>
</div>
</form>
</body>
</html>
启用,禁用:
{
//string status = e.commandname;
if ((e.commandname == "true")||(e.commandname == "false"))
{
string str = configurationmanager.connectionstrings["stucnn"].connectionstring;
using (sqlconnection sqlcnn = new sqlconnection(str))
{
using (sqlcommand sqlcmm = sqlcnn.createcommand())
{
sqlcnn.open();
sqlcmm.commandtext = "update student set status=@status where sid="
+ e.commandargument.tostring();
sqlcmm.parameters.addwithvalue("@status",e.commandname);
sqlcmm.executenonquery();
}
}
this.bindstudent();
}
}
<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" debug="true" inherits="default2" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:repeater id="repeater1" runat="server"
onitemcommand="repeater1_itemcommand">
<headertemplate><table><tr><th>编号</th><th>姓名</th><th>状态</th><th> </th></tr></headertemplate>
<itemtemplate>
<tr><td><%# eval("sid") %></td>
<td><%# eval("sname") %></td>
<td><%# convert.toboolean(eval("status"))?"启用":"禁用" %></td>
<td><asp:linkbutton id="btnsetstatus" runat="server" commandargument='eval("sid")' text='<%# convert.toboolean(eval("status"))?"禁用":"启用" %>' commandname='<%# convert.toboolean(eval("status"))?"false":"true" %>'></asp:linkbutton></td></tr>
</itemtemplate>
<footertemplate></table></footertemplate>
</asp:repeater>
</div>
</form>
</body>
</html>
希望本文所述对大家的asp.net程序设计有所帮助。