您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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?sid=" + 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 >编号</td><td >姓名</td> <td >图片</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>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。