Webform Repeater的灵活运用

案例:模拟购物列表

封装实体类:

 View Code

数据访问类:

 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              100%;
 18             height: 80px;
 19             background-color: navy;
 20         }
 21 
 22         #footer {
 23             position: relative;
 24              100%;
 25             height: 100px;
 26             background-color: black;
 27         }
 28 
 29         #items {
 30             position: relative;
 31              80%;
 32             margin-left: 10%;
 33         }
 34 
 35         .item {
 36             position: relative;
 37              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                  100%;
 50                 height: 60%;
 51             }
 52 
 53         .item-name {
 54             position: relative;
 55              80%;
 56             margin-left: 10%;
 57             font-size: 18px;
 58         }
 59 
 60         .item-price {
 61             position: relative;
 62              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              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="<%#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展示:

Repeater的Command操作

1、ItemCommand事件 :在Repeater中所有能触发事件的控件,都会来触发这一个事件

                                    后台创建:在Page_Load中  Repeater1.ItemCommand +=  ,然后双击Tab键创建

2、CommandName : 判断点击的是什么按钮,

                                后台调用:e.CommandName

3、CommandArgument : 触发事件所传递过来的主键值数据,放在这里面 界面值绑定时要用  单引号 !!!!!! 

                                      后台调用:e.CommandArgument 

原文地址:https://www.cnblogs.com/1030351096zzz/p/6262648.html