郁闷了好长一段时间,做了各种各样的修改,就是“删除”无法执行,罪魁祸首竟然是ListView控件中少设置了一个DataKeyNames属性,悲哀啊!
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="listView6._Default" %>
<!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" mce_bogus="1">body
{
font-family:宋体;
font-size:14px;
}
.insert {
background-color: #9C0;
}
.xm {
width: 80px;
}
.dh, .qq {
width: 100px;
}
.edit td {
padding-top: 3px;
padding-right: 0px;
padding-bottom: 4px;
padding-left: 6px;
font-family:宋体;
background-color: #CFF;
color:#F00;
font-weight:bolder;
}
a
{
color: #06F;
text-decoration: none;
}
.edit a, .edit input
{
font-weight:bolder;
color:#F00;
text-decoration: none;
font-family:宋体;
}
.edit a:hover
{
font-weight:bolder;
color:#F00;
text-decoration: underline overline;
}
a:hover {
color: #F00;
text-decoration: underline overline;
}</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="AccessDataSource1" DataKeyNames="xh"
ItemPlaceholderID="PlaceHolder1"
InsertItemPosition="LastItem" >
<LayoutTemplate>
<table border="2" cellpadding="6" cellspacing="0" align="center" style="border-collapse:collapse;" mce_style="border-collapse:collapse;">
<tr>
<th width="80">学号</th>
<th width="105">姓名</th>
<th width="124">电话</th>
<th width="124">QQ</th>
<th width="80">管理</th>
</tr>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<tr>
<td colspan="5" align="right">
<asp:DataPager ID="DataPager1" runat="server" PageSize="15" >
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("xh") %>'></asp:Label></td>
<td><%#Eval("xm") %></td>
<td><%#Eval("dh") %></td>
<td><%#Eval("qq") %></td>
<td align="center">
<asp:LinkButton ID="btnModify" runat="server" CommandName="Edit" >修改</asp:LinkButton>
<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" >删除</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr class="edit">
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("xh") %>'></asp:Label></td>
<td><asp:TextBox ID="TextBox2" runat="server" Text="<%# Bind('xm') %>" CssClass="xm"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox3" runat="server" Text="<%# Bind('dh') %>" CssClass="dh"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox4" runat="server" Text="<%# Bind('qq') %>" CssClass="qq"></asp:TextBox></td>
<td align="center">
<asp:LinkButton ID="btnModify" runat="server" CommandName="Update" >确认</asp:LinkButton>
<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel" >取消</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr class="insert">
<td><asp:TextBox ID="TextBox1" runat="server" Text="<%# Bind('xh') %>" CssClass="xm"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox2" runat="server" Text="<%# Bind('xm') %>" CssClass="xm"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox3" runat="server" Text="<%# Bind('dh') %>" CssClass="dh"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox4" runat="server" Text="<%# Bind('qq') %>" CssClass="qq"></asp:TextBox></td>
<td align="center">
<asp:LinkButton ID="btnInsert" runat="server" CommandName="Insert" >插入</asp:LinkButton>
<asp:LinkButton ID="btnClear" runat="server" CommandName="Cancel" >清除</asp:LinkButton>
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/students.mdb"
SelectCommand="SELECT * FROM [students] ORDER BY [xh]"
UpdateCommand="UPDATE [students] SET [xm] = ?, [dh] = ?, [qq] = ? WHERE [xh] = ?"
DeleteCommand="DELETE FROM [students] WHERE (xh =?)"
InsertCommand="INSERT INTO [students] ([xh], [xm], [dh], [qq]) VALUES (?, ?, ?, ?)"
>
<UpdateParameters>
<asp:Parameter Name="xm" Type="String" />
<asp:Parameter Name="dh" Type="String" />
<asp:Parameter Name="qq" Type="String" />
<asp:Parameter Name="xh" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="xh" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="xh" Type="String" />
<asp:Parameter Name="xm" Type="String" />
<asp:Parameter Name="dh" Type="String" />
<asp:Parameter Name="qq" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>