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

}
时间: 2024-12-19 05:56:19

repeater灵活运用、repeater的commmand用法、如何不用repeater展示数据的相关文章

C#-WebForm-Repeater的灵活运用、ItemCommand的用法-增删改查、如何不适用Repeater来展示数据?

浏览器页面: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="

Repeater控件 ---表格展示数据

简介: Repeater控件是Web 服务器控件中的一个容器控件,它使您可以从页的任何可用数据中创建出自定义列表. Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repeater 控件提供布局.当该页运行时, Repeater 控件依次通过数据源中的记录为每个记录呈现一个项. Repeater控件不具备内置的呈现功能,所以我们得用一些模板来实现他的数据呈现 下表描述了 Repeater 控件支持的模板.  模板属性                           

jquery validate.js表单验证的基本用法入门--不用写繁琐的验证代码了...

jquery.validate.js是jquery下的一个验证插件,功能比较强大,早就有所耳闻但是一只没有动手用过,现在在于能够研究一下了. 这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录. 先贴一个国内某大公司的代码: 1 <script type="text/javascript"> 2 function lang(key) { 3 mylang = { 4 'ls_input_myb': '请输入您的账户', 5 'ls_myb_email': '漫游

Android——Listview不用notifydatasetchanged更新数据的方法

一.介绍 先来介绍一下listview更新数据的几种方法,目前我知道的方法有如下几种: 1. 每次更新数据时都调用listview.setadapter(); 2. 每次更新数据时都调用adapter.notifydatasetchanged(): 3. 在自定义的adapter里添加更新函数update; 博客撰写人:It一zhai男 转载请标明地址:http://blog.csdn.net/u013293125/article/details/52858396 这里,我们将会一个一个来介绍,

linux中sed的用法详解(对行数据的添加、删除等)

sed使用语法 [[email protected] test]# sed --help 用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]... -n, --quiet, --silent                 取消自动打印模式空间 -e 脚本, --expression=脚本                 添加"脚本"到程序的运行列表 -f 脚本文件, --file=脚本文件                 添加"脚本文件"到

REPEATER展示表格

1.可以不用table展示数据 <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <div class="item"> <img src='<%#Eval("pic") %>' />//显示图片 <p><%#Eval("name") %></p>

WebForm数据展示 Web Repeater的使用

Repeater控件:(重复器)按某一格式展示数据 HeaderTemplate - 先执行,执行一次 FooterTemplate - 最后执行,执行一次 ItemTemplate - 在Header之后执行,有多少条数据绑定就执行多少次 AlterNatingItemTemplate - 交替项模板,与ItemTemplate交替执行 Repeater绑定数据: //集合结果指向Repeater1 Repeater1.DataSource = new StusData().SelectStu

Webform Repeater的灵活运用

案例:模拟购物列表 封装实体类:   数据访问类:   用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/1

7.19 repeater的用法:

----------------------------------------------repeater的Command用法:1.ItemCommand事件 - Repeater中所有可以触发事件的控件,都会来执行这一个事件 2.CommandName - 随意的起名,在后台事件中按照这个名字来区分你按下的是哪一个功能按钮 3.CommandArgument - 事件数据,通常放置主键值,在后台使用 e.CommandArgument来获取主键数据 ---------------------