ASP.NET中repeater控件用法实例
本文实例讲述了ASP.NET中repeater控件用法。分享给大家供大家参考。具体实现方法如下
repeater绑定数据
{
if(!IsPostBack)
BindStudent();
}
private void BindStudent()
{
string str = ConfigurationManager.ConnectionStrings["stun"].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["stun"].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://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://.w3./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"
onitemmand="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["stun"].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://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://.w3./1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server"
onitemmand="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.程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程