repeater灵活运用repeater的commmand用法如何不用repeater展示数据
Posted 游称
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了repeater灵活运用repeater的commmand用法如何不用repeater展示数据相关的知识,希望对你有一定的参考价值。
实体类:
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// gouwu 的摘要说明 /// </summary> public class gouwu { public gouwu() { // // TODO: 在此处添加构造函数逻辑 // } public int ids { get; set; } public string pic { get; set; } public string name { get; set; } public decimal nowprice { get; set; } public decimal oldprice { get; set; } public string context { get; set; } }
数据访问类:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// gouwudata 的摘要说明 /// </summary> public class gouwudata { SqlConnection conn = null; SqlCommand cmd = null; public gouwudata() { conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<gouwu> select() { List<gouwu> glist = new List<gouwu>(); cmd.CommandText = "select*from gouwu"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { gouwu g = new gouwu(); g.ids = Convert.ToInt32(dr[0]); g.pic = dr[1].ToString(); g.name = dr[2].ToString(); g.nowprice = Convert.ToDecimal(dr[3]); g.oldprice = Convert.ToDecimal(dr[4]); g.context = dr[5].ToString(); glist.Add(g); } } conn.Close(); return glist; } public void delete(int ids) { cmd.CommandText = "delete from gouwu where ids=‘"+ids+"‘"; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } }
aspx里:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> * { padding:0px; margin:0px; } #header { width:100%; height:80px; background-color:navy; position:relative; } #footer { width:100%; height:80px; background-color:black; position:relative; } #items { width:80%; margin-left:10%; position:relative; } .item { position:relative; width:23.5%; margin-left:0.5%; margin-right:0.5%; height:295px; margin-top:5px; border:solid 1px; float:left; margin-bottom:5px; } .item img { position:relative; width:100%; height:60%; } .itemname { position:relative; width:80%; margin-left:10%; font-size:18px; } .itemprice { position:relative; width:100%; color:red; text-align:right; font-size:18px; } .itemprice span { font-size:12px; text-decoration:line-through; } .itemcon { position:relative; width:90%; margin-left:5%; } </style> </head> <body> <form id="form1" runat="server"> <div id="header"></div> <div id="items"> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <div class="item"> <img src="<%#Eval("pic") %>" /> <div class="itemname"><%#Eval("name") %></div> <div class="itemprice">价格:<%#Eval("nowprice") %><span><%#Eval("oldprice") %></span></div> <div class="itemcon"><%#Eval("context") %></div> <asp:Button ID="Button1" runat="server" Text="删除" CommandName="delete" CommandArgument=‘<%#Eval("ids") %>‘ /><%--repeater的command方法--%> </div> </ItemTemplate> </asp:Repeater> <div style="clear:both"></div>//使高度自适应 </div> <div id="footer"></div> </form> </body> </html>
cs里:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new gouwudata().select(); Repeater1.DataBind(); } Repeater1.ItemCommand += Repeater1_ItemCommand; } void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "delete")//repeater的command方法 { new gouwudata().delete(Convert.ToInt32(e.CommandArgument));//repeater的command方法 Repeater1.DataSource = new gouwudata().select();//删除后及时刷新数据 Repeater1.DataBind(); } } }
如何不用repeater展示数据:
aspx中:用literal
<body style="font-family: 微软雅黑;"> <form id="form1" runat="server"> <div id="header"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> <div id="items"> <asp:Literal ID="Literal1" runat="server"></asp:Literal> <div style="clear: both;"></div> </div> <div id="footer"></div> </form> </body>
cs中:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Literal1.Text = DataBind(); } } public string DataBind() { string end = ""; List<gouwu> glist = new gouwuData().Select(); foreach (gouwu g in glist) { if (g.name == "猕猴桃") { continue; } end += "<div class=\"item\">"; end += "<img src=\"" + g.pic + "\" />"; end += " <div class=\"item-name\">" + g.name + "</div>"; end += "<div class=\"item-price\">价格:" + g.nowPrice + "<span>" + g.oldPrice + "</span></div>"; end += "<div class=\"item-context\">" + g.context + "</div>"; end += "<a href=\"Delete.aspx?id=" + g.ids + "\">删除</a>"; end += "</div>"; } return end; } }
以上是关于repeater灵活运用repeater的commmand用法如何不用repeater展示数据的主要内容,如果未能解决你的问题,请参考以下文章
甩掉DataList,Repeater,列表数据显示得灵活--转