首页 > 代码库 > ListView自定义按钮实例-标记删除功能

ListView自定义按钮实例-标记删除功能

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .divItem{ padding:5px; border-bottom:1px solid #dadada; }
    .divBtns{ width:130px; float:left;}
    .divInfo{ width:350px; padding:0px 10px 0px 10px;
               border-left:1px solid #dadada;
               border-right:1px solid #dadada; float:left;}
    .divInfo2{ width:200px; float:left;} 
    .clr{ clear:both;}           
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<asp:listview ID="Listview1" runat="server" DataSourceID="AccessDataSource1" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<div id="itemHolder" runat="server">

</div>
</LayoutTemplate>
<ItemTemplate>
<div class="divItem">
<div class="divBtns">
    <asp:Button ID="btn_edit" runat="server" Text="编辑"  CommandName="edit"/><br />
     <asp:Button ID="btn_delete" runat="server" Text="删除"  CommandName="delete" <span style="color:#ff0000;">OnClientClick="return confirm('是否确定删除?')"</span>/>
     <asp:Button ID="btn_delmak" runat="server" Text="标记删除"  <span style="color:#ff0000;">CommandName="lvcmd" CommandArgument='<%#Eval("se_id") %>' OnCommand="mycmd" OnClientClick="return confirm('是否确定删除?')"</span>/>
</div>
<div class="divInfo">
<h2><%#Eval("se_name") %></h2>
<p><%#Eval("se_memo") %></p>
</div>
<div class="divInfo2">
    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Eval("se_enable") %>' Enabled="false"  Text="有效性"/>
    <p>排序:<%#Eval("se_order") %></p>
</div>
<div class="clr"></div>
</div>

</ItemTemplate>
</asp:listview>
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/mdb/EV.mdb" 
        DeleteCommand="DELETE FROM [T_SERIES] WHERE [se_id] = ?" 
        InsertCommand="INSERT INTO [T_SERIES] ([se_id], [se_name], [se_memo], [se_order], [se_enable], [se_delete]) VALUES (?, ?, ?, ?, ?, ?)" 
       <span style="color:#ff0000;"> SelectCommand="SELECT * FROM [T_SERIES] WHERE SE_DELETE=FALSE ORDER BY SE_ORDER DESC" </span>
        UpdateCommand="UPDATE [T_SERIES] SET [se_name] = ?, [se_memo] = ?, [se_order] = ?, [se_enable] = ?, [se_delete] = ? WHERE [se_id] = ?">
        <DeleteParameters>
            <asp:Parameter Name="se_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="se_id" Type="Int32" />
            <asp:Parameter Name="se_name" Type="String" />
            <asp:Parameter Name="se_memo" Type="String" />
            <asp:Parameter Name="se_order" Type="Int32" />
            <asp:Parameter Name="se_enable" Type="Boolean" />
            <asp:Parameter Name="se_delete" Type="Boolean" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="se_name" Type="String" />
            <asp:Parameter Name="se_memo" Type="String" />
            <asp:Parameter Name="se_order" Type="Int32" />
            <asp:Parameter Name="se_enable" Type="Boolean" />
            <asp:Parameter Name="se_delete" Type="Boolean" />
            <asp:Parameter Name="se_id" Type="Int32" />
        </UpdateParameters>
    </asp:AccessDataSource>
    </div>
     </form>
</body>
</html>
   
<span style="color:#ff0000;">SelectCommand="SELECT * FROM [T_SERIES] WHERE SE_DELETE=FALSE ORDER BY SE_ORDER DESC" 要修改这一语句,表示显示SE_DELETE标记为false的项。</span>
<span style="color:#ff0000;"> CommandArgument='<%#Eval("se_id") %>' 用于参数的传递</span>
.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class test : System.Web.UI.Page
{
    string str_cnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
    string str_sourcefile = "mdb\\EV.mdb";
    OleDbConnection cnn;
    OleDbCommand cmd;
    OleDbDataReader datar;
    string str_sql;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void mycmd(object sender, CommandEventArgs e){
         string str_conn = str_cnn + MapPath(str_sourcefile);
        cnn = new OleDbConnection(str_conn);
        cnn.Open();

        str_sql = "update t_series set se_delete =true where " +
                "se_id =" +<span style="color:#ff0000;"> e.CommandArgument.ToString();//接收参数!!!</span>
        cmd = new OleDbCommand(str_sql, cnn);
        cmd.ExecuteNonQuery();
        cnn.Close();
        <span style="color:#ff0000;">Listview1.DataBind();//数据控件内部按钮,执行完之后需要重新绑定一下数据控件的数据</span>
}
}

效果如下:

当点击确定时,此项被标记删除。

ListView自定义按钮实例-标记删除功能