本文实例讲述了ASP.NET中repeater控件用法。分享给大家供大家参考。具体实现方法如下:
repeater绑定数据:
复制代码 代码如下:protected void Page_Load(object sender, EventArgs e)
{
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();
}
}
}
删除数据:
复制代码 代码如下:protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
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" + e.CommandArgument.ToString());
}
}
前台:
复制代码 代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
启用,禁用:
复制代码 代码如下:protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//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程序设计有所帮助。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]