Webform Repeater的灵活运用 和Command操作

Repeater的灵活运用

<%@ 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 {
            position: relative;
             100%;
            height: 80px;
            background-color: navy;
        }

        #footer {
            position: relative;
             100%;
            height: 100px;
            background-color: black;
        }

        #items {
            position: relative;
             80%;
            margin-left: 10%;
        }

        .item {
            position: relative;
             23.5%;
            margin-left: 0.5%;
            margin-right: 0.5%;
            height: 300px;
            border: 1px solid black;
            margin-top: 5px;
            margin-bottom: 5px;
            float: left;
        }

            .item img {
                position: relative;
                 100%;
                height: 60%;
            }

        .item-name {
            position: relative;
             80%;
            margin-left: 10%;
            font-size: 18px;
        }

        .item-price {
            position: relative;
             100%;
            color: red;
            text-align: right;
            font-size: 18px;
        }

            .item-price span {
                font-size: 12px;
                text-decoration: line-through;
            }

        .item-context {
            position: relative;
             90%;
            margin-left: 5%;
        }

        #Label1 {
            color: white;
        }
    </style>

</head>
<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:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <div class="item">
                        <img src="<%#Eval("pic") %>" />
                        <div class="item-name"><%#Eval("name") %></div>
                        <div class="item-price">价格:<%#Eval("nowPrice") %><span><%#Eval("oldPrice") %></span></div>
                        <div class="item-context"><%#Eval("context") %></div>
                        <asp:Button ID="Button1" runat="server" CommandName="Delete" CommandArgument='<%#Eval("ids") %>' Text="删除" />
                        <asp:Button ID="Button2" runat="server" CommandName="Update" CommandArgument='<%#Eval("ids") %>' Text="修改" />
                    </div>
                </ItemTemplate>



            </asp:Repeater>

            <%-- <asp:Literal ID="Literal1" runat="server"></asp:Literal>--%>

            <div style="clear: both;"></div>
        </div>

        <div id="footer"></div>
    </form>
</body>
</html>

注释中的是  不用div的 简单代码 不过后台要麻烦点

  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的Command操作

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

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

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

                                后台调用:e.CommandName

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

                                      后台调用:e.CommandArgument 

4、ItemCreated :项模板中将HTML代码生成完毕时执行一遍此事件 ,即创建项时激发

5、ItemDataBound :项模板将数据绑定后执行一遍此事件

例子:以删除Repeater中数据为例

Main.aspx前台代码:放一个Button控件

<asp:Button ID="Btnshan" runat="server" CommandArgument ='<%#Eval("UserName") %>' CommandName ="Delete" Text="删除"  />

后台Main.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
    {

        Repeater1.ItemCommand += Repeater1_ItemCommand;
        
    }

    void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //判断点击的按钮
        if (e.CommandName == "Delete")
        {
            //去传过来的值,查询,重新绑定
            new UsersDA().Delete(e.CommandArgument.ToString () );
            Repeater1.DataSource = new UsersDA().Select();
            Repeater1.DataBind();
        }
    }
原文地址:https://www.cnblogs.com/shadow-wolf/p/6261969.html