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 {
            100%;
            height:80px;
            background-color:navy;
            position:relative;
        }
        #footer {
        100%;
        height:80px;
        background-color:black;
        position:relative;
        }
        #items {
        80%;
        margin-left:10%;
        position:relative;       
      
        }
        .item {
        position:relative;
        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;
            100%;
            height:60%;
            }
        .itemname {
        position:relative;
        80%;
        margin-left:10%;
        font-size:18px;
        }
        .itemprice {
        position:relative;
        100%;
        color:red;
        text-align:right;
        font-size:18px;
        }
            .itemprice span {
            font-size:12px;
            text-decoration:line-through;            
            }
        .itemcon {
            position:relative;
            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里:

Repeater的Command操作

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

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

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

                                后台调用:e.CommandName

3、CommandArgument : 触发事件所传递过来的数据,放在这里面 界面值绑定时要用  单引号 !!!!!! 不同的name可以有不同的commandarguement值,在判断对应name时,可以取到对应的Arguement值  if (e.CommandName == "delete")

                                      后台调用:e.CommandArgument 

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;
    }




}
原文地址:https://www.cnblogs.com/wy1992/p/6262259.html