首页 > 代码库 > ListView的插入和编辑模板实例
ListView的插入和编辑模板实例
.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test"MaintainScrollPositionOnPostback="true" %>
<!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"
DataKeyNames="se_id" InsertItemPosition="LastItem">
<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" OnClientClick="return confirm(‘是否确定删除?‘)"/>
<asp:Button ID="btn_delmak" runat="server" Text="标记删除" CommandName="lvcmd" CommandArgument=‘<%#Eval("se_id") %>‘ OnCommand="mycmd" OnClientClick="return confirm(‘是否确定删除?‘)"/>
</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>
<InsertItemTemplate>
<div class="divItem" style="border:1px solid red;">
<div class="divBtns">
<asp:Button ID="btn_insert" runat="server" Text="插入" CommandName="insert" OnClientClick="return confirm(‘是否确定插入?‘)"/><br />
<asp:Button ID="btn_cancel" runat="server" Text="取消" CommandName="cancel" />
</div>
<div class="divInfo">
系列名称:<asp:TextBox ID="txt_i_name" runat="server" Text=‘<%#Bind("se_name") %>‘/><br />
系列简介:<asp:TextBox ID="txt_i_memo" runat="server" Text=‘<%#Bind("se_memo") %>‘ TextMode="MultiLine"/><%--Bind双向绑定--%>
</div>
<div class="divInfo2">
<asp:CheckBox ID="chk_i_enable" runat="server" Checked=‘<%#Bind("se_enable") %>‘ Text="有效性"/><br />
排序:<asp:TextBox ID="txt_i_order" runat="server" Text=‘<%#Bind("se_order") %>‘/><%--Bind双向绑定--%>
</div>
<div class="clr"></div>
</div>
</InsertItemTemplate>
<EditItemTemplate>
<div class="divItem" style="border:1px solid red;">
<div class="divBtns">
<asp:Button ID="btn_insert" runat="server" Text="保存" CommandName="update" OnClientClick="return confirm(‘是否保存更改?‘)"/><br />
<asp:Button ID="btn_cancel" runat="server" Text="取消" CommandName="cancel" />
</div>
<div class="divInfo">
系列名称:<asp:TextBox ID="txt_e_name" runat="server" Text=‘<%#Bind("se_name") %>‘/><br />
系列简介:<asp:TextBox ID="txt_e_memo" runat="server" Text=‘<%#Bind("se_memo") %>‘ TextMode="MultiLine"/><%--Bind双向绑定--%>
</div>
<div class="divInfo2">
<asp:CheckBox ID="chk_e_enable" runat="server" Checked=‘<%#Bind("se_enable") %>‘ Text="有效性"/><br />
排序:<asp:TextBox ID="txt_e_order" runat="server" Text=‘<%#Bind("se_order") %>‘/><%--Bind双向绑定--%>
</div>
<div class="clr"></div>
</div>
</EditItemTemplate>
</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_name], [se_memo], [se_order], [se_enable], [se_delete]) VALUES (?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [T_SERIES] WHERE SE_DELETE=FALSE ORDER BY SE_ORDER DESC"
UpdateCommand="UPDATE [T_SERIES] SET [se_name] = ?, [se_memo] = ?, [se_order] = ?, [se_enable] = ?, [se_delete] = ? WHERE [se_id] = ?">注释:Bug位置
<DeleteParameters>
<asp:Parameter Name="se_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
Bug位置
<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>
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="background-color: rgb(255, 0, 0);"> e.CommandArgument.ToString()</span>;//参数注意 cmd = new OleDbCommand(str_sql, cnn); cmd.ExecuteNonQuery(); cnn.Close(); Listview1.DataBind();//数据控件内部按钮,执行完之后需要重新绑定一下数据控件的数据 } }
<span style="background-color: rgb(255, 0, 0);">MaintainScrollPositionOnPostback="true"点击编辑按钮时滚动条不滚动,提高用户体验</span>
效果如下:
ListView的插入和编辑模板实例