首页 > 代码库 > Webform Repeater的灵活运用

Webform Repeater的灵活运用

案例:模拟购物列表

封装实体类:

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 /// <summary>
 7 /// gouwu 的摘要说明
 8 /// </summary>
 9 public class gouwu
10 {
11     public int ids { get; set; }
12     public string pic { get; set; }
13     public string name { get; set; }
14     public decimal nowPrice { get; set; }
15     public decimal oldPrice { get; set; }
16     public string context { get; set; }
17 
18     public string qx {
19         get
20         {
21             string eee = "";
22             if (this.name == "猕猴桃")
23             {
24                 eee = " display:none;";
25             }
26 
27             return eee;
28         }
29     }
30 
31 }
View Code

数据访问类:

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Data.SqlClient;
 6 
 7 /// <summary>
 8 /// gouwuData 的摘要说明
 9 /// </summary>
10 public class gouwuData
11 {
12     SqlConnection conn = null;
13     SqlCommand cmd = null;
14     public gouwuData()
15     {
16         conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");
17         cmd = conn.CreateCommand();
18     }
19 
20     public List<gouwu> Select()
21     {
22         List<gouwu> glist = new List<gouwu>();
23         cmd.CommandText = "select *from gouwu";
24 
25         conn.Open();
26         SqlDataReader dr = cmd.ExecuteReader();
27         if (dr.HasRows)
28         {
29             while (dr.Read())
30             {
31                 gouwu g = new gouwu();
32                 g.ids = Convert.ToInt32(dr[0]);
33                 g.pic = dr[1].ToString();
34                 g.name = dr[2].ToString();
35                 g.nowPrice = Convert.ToDecimal(dr[3]);
36                 g.oldPrice = Convert.ToDecimal(dr[4]);
37                 g.context = dr[5].ToString();
38 
39                 glist.Add(g);
40 
41             }
42         }
43 
44 
45         conn.Close();
46 
47         return glist;
48     }
49 
50 
51     public void Delete(int ids)
52     {
53         cmd.CommandText = "delete from gouwu where ids=‘" + ids + "";
54 
55         conn.Open();
56         cmd.ExecuteNonQuery();
57         conn.Close();
58     }
59 
60 
61 }
View Code

用Repeater展示:

技术分享
  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2 
  3 <!DOCTYPE html>
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head id="Head1" runat="server">
  7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8     <title></title>
  9     <style>
 10         * {
 11             padding: 0px;
 12             margin: 0px;
 13         }
 14 
 15         #header {
 16             position: relative;
 17             width: 100%;
 18             height: 80px;
 19             background-color: navy;
 20         }
 21 
 22         #footer {
 23             position: relative;
 24             width: 100%;
 25             height: 100px;
 26             background-color: black;
 27         }
 28 
 29         #items {
 30             position: relative;
 31             width: 80%;
 32             margin-left: 10%;
 33         }
 34 
 35         .item {
 36             position: relative;
 37             width: 23.5%;
 38             margin-left: 0.5%;
 39             margin-right: 0.5%;
 40             height: 300px;
 41             border: 1px solid black;
 42             margin-top: 5px;
 43             margin-bottom: 5px;
 44             float: left;
 45         }
 46 
 47             .item img {
 48                 position: relative;
 49                 width: 100%;
 50                 height: 60%;
 51             }
 52 
 53         .item-name {
 54             position: relative;
 55             width: 80%;
 56             margin-left: 10%;
 57             font-size: 18px;
 58         }
 59 
 60         .item-price {
 61             position: relative;
 62             width: 100%;
 63             color: red;
 64             text-align: right;
 65             font-size: 18px;
 66         }
 67 
 68             .item-price span {
 69                 font-size: 12px;
 70                 text-decoration: line-through;
 71             }
 72 
 73         .item-context {
 74             position: relative;
 75             width: 90%;
 76             margin-left: 5%;
 77         }
 78 
 79         #Label1 {
 80             color: white;
 81         }
 82     </style>
 83 
 84 </head>
 85 <body style="font-family: 微软雅黑;">
 86     <form id="form1" runat="server">
 87         <div id="header"></div>
 88         <div id="items">
 89             <asp:Repeater ID="Repeater1" runat="server">
 90                 <ItemTemplate>
 91                     <div class="item">
 92                         <img src=http://www.mamicode.com/"<%#Eval("pic") %>" />
 93                         <div class="item-name"><%#Eval("name") %></div>
 94                         <div class="item-price">价格:<%#Eval("nowPrice") %><span><%#Eval("oldPrice") %></span></div>
 95                         <div class="item-context"><%#Eval("context") %></div>
 96                         <asp:Button ID="Button1" runat="server" Text="删除" CommandName="Delete" CommandArgument=<%#Eval("ids") %> />
 97                     </div>
 98                 </ItemTemplate>
 99             </asp:Repeater>
100             <div style="clear: both;"></div>
101         </div>
102 
103         <div id="footer"></div>
104     </form>
105 </body>
106 </html>
界面:
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class _Default : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         if (!IsPostBack)
13         {
14             Repeater1.DataSource = new gouwuData().Select();
15             Repeater1.DataBind();
16         }
17         //点击Repeater1中的按钮时发生
18         Repeater1.ItemCommand += Repeater1_ItemCommand;
19     }
20 
21     void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
22     {
23         if (e.CommandName == "Delete")
24         {
25             new gouwuData().Delete(Convert.ToInt32(e.CommandArgument));
26 
27                 Repeater1.DataSource = new gouwuData().Select();
28                 Repeater1.DataBind();
29         }
30     }
31 }
后台:

不用Repeater展示:

技术分享
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head id="Head1" runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <style>
10         * {
11             padding: 0px;
12             margin: 0px;
13         }
14 
15         #header {
16             position: relative;
17             width: 100%;
18             height: 80px;
19             background-color: navy;
20         }
21 
22         #footer {
23             position: relative;
24             width: 100%;
25             height: 100px;
26             background-color: black;
27         }
28 
29         #items {
30             position: relative;
31             width: 80%;
32             margin-left: 10%;
33         }
34 
35         .item {
36             position: relative;
37             width: 23.5%;
38             margin-left: 0.5%;
39             margin-right: 0.5%;
40             height: 300px;
41             border: 1px solid black;
42             margin-top: 5px;
43             margin-bottom: 5px;
44             float: left;
45         }
46 
47             .item img {
48                 position: relative;
49                 width: 100%;
50                 height: 60%;
51             }
52 
53         .item-name {
54             position: relative;
55             width: 80%;
56             margin-left: 10%;
57             font-size: 18px;
58         }
59 
60         .item-price {
61             position: relative;
62             width: 100%;
63             color: red;
64             text-align: right;
65             font-size: 18px;
66         }
67 
68             .item-price span {
69                 font-size: 12px;
70                 text-decoration: line-through;
71             }
72 
73         .item-context {
74             position: relative;
75             width: 90%;
76             margin-left: 5%;
77         }
78     </style>
79 
80 </head>
81 <body style="font-family: 微软雅黑;">
82     <form id="form1" runat="server">
83         <div id="header"></div>
84         <div id="items">
85 
86             <asp:Literal ID="Literal1" runat="server"></asp:Literal>
87             
88             <div style="clear: both;"></div>
89         </div>
90 
91         <div id="footer"></div>
92     </form>
93 </body>
94 </html>
界面
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class Default2 : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         if (!IsPostBack)
13         {
14             Literal1.Text = DataBind();
15         }
16     }
17     public string DataBind()
18     {
19         string end = "";
20         List<gouwu> glist = new gouwuData().Select();
21         foreach (gouwu g in glist)
22         {
23             if(g.name=="香蕉")
24             {
25                 continue;
26             }
27             end += "<div class=\"item\">";
28             end += "<img src=http://www.mamicode.com/"" + g.pic + "\" />";
29             end += "<div class=\"item-name\">" + g.name + "</div>";
30             end += "<div class=\"item-price\">价格:" + g.nowPrice + "<span>" + g.oldPrice + "</span></div>";
31             end += "<div class=\"item-context\">" + g.context + "</div>";
32             end += "<a href=http://www.mamicode.com/"Delete.aspx?id=" + g.ids + "\">删除</a>";
33             end += "</div>";
34         }
35 
36         return end;
37     }
38 }
后台
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class Delete : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         new gouwuData().Delete(Convert.ToInt32(Request["id"]));
13         Response.Write("<script>window.location.href=http://www.mamicode.com/‘Default2.aspx‘</script>");
14     }
15 }
删除界面后台

技术分享

 

Webform Repeater的灵活运用