用ListView控件编辑数据
正如你所看到的使用ListView控件显示数据相对要直接得多但你还可以让用户在ListView中直接编辑数据添加一个新页面ListViewEditExampleaspx它的代码如清单所示
清单编辑ListView
<%@ Page Language=C# %>
<script runat=server>
void deptsView_ItemUpdated(object sender
ListViewUpdatedEventArgs e)
{
lblResultText = eAffectedRowsToString() +
row(s) successfully updated;
}
void deptsView_PagePropertiesChanged(object sender EventArgs e)
{
//Set the text to empty when navigating to a different page
lblResultText = ;
}
</script>
<html xmlns=http://wwwworg//xhtml>
<head runat=server>
<link rel=Stylesheet type=text/css href=StyleSheetcss />
<title>Editing Data using ListView Control</title>
</head>
<body>
<form id=form runat=server>
<div>
<asp:ListView ID=ContactsListView DataSourceID=deptSource
DataKeyNames=DepartmentID runat=server
OnItemUpdated=deptsView_ItemUpdated
OnPagePropertiesChanged=deptsView_PagePropertiesChanged>
<LayoutTemplate>
<table cellpadding= width=px border=
runat=server id=tblProducts>
<tr id=row runat=server class=header>
<th id=header runat=server>Name</th>
<th id=header runat=server>Group Name</th>
<th id=header runat=server>Action</th>
</tr>
<tr runat=server id=itemPlaceholder />
</table>
<asp:DataPager runat=server ID=deptsDataPager
PageSize=>
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton=True
ShowLastPageButton=True FirstPageText=|<<
LastPageText= >>| NextPageText= >
PreviousPageText= < />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr id=row runat=server>
<td>
<asp:Label ID=lblName runat=Server
Text=<%#Eval(Name) %> />
</td>
<td valign=top>
<asp:Label ID=lblGroupName runat=Server
Text=<%#Eval(GroupName) %> />
</td>
<td>
<asp:LinkButton ID=btnEdit runat=Server Text=Edit
CommandName=Edit />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style=backgroundcolor: #ADDE>
<td>
<asp:TextBox ID=txtName runat=server
Text=<%# Bind(Name) %>
MaxLength= /><br />
</td>
<td>
<asp:TextBox ID=txtGroupName runat=server Text=<%#
Bind(GroupName) %> MaxLength= /><br />
</td>
<td>
<asp:LinkButton ID=btnUpdate runat=server
CommandName=Update Text=Update />
<asp:LinkButton ID=btnCancel runat=server
CommandName=Cancel Text=Cancel />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID=deptSource runat=server
ConnectionString=<%$ ConnectionStrings:AdventureWorks %>
SelectCommand=SELECT [DepartmentID][Name][GroupName] FROM
HumanResourcesDepartment UpdateCommand=UPDATE
HumanResourcesDepartment SET Name = @Name
GroupName = @GroupName WHERE DepartmentID = @DepartmentID>
</asp:SqlDataSource>
<br /><br />
<asp:Label runat=server ID=lblResult Text=
FontBold=true />
</div>
</form>
</body>
</html>
清单的代码说明了如何使用EditItemTemplate组件在编辑模式下生成内容然后通过SqlDataSource更新数据库
首先你设置SqlDataSource的UpdateCommand属性这样SQL语句就会用由用户指定的最新值执行数据库更新操作
<asp:SqlDataSource ID=deptSource runat=server
ConnectionString=<%$ ConnectionStrings:AdventureWorks %>
SelectCommand=SELECT [DepartmentID][Name][GroupName] FROM
HumanResourcesDepartment UpdateCommand=UPDATE
HumanResourcesDepartment SET Name = @Name
GroupName = @GroupName WHERE DepartmentID = @DepartmentID>
</asp:SqlDataSource>
接下来在ItemTemplate组件中指定编辑项目的连接用户
<ItemTemplate>
<asp:LinkButton ID=btnEdit runat=Server Text=Edit
CommandName=Edit />
</td>
</tr>
</ItemTemplate>
然后指定EditItemTemplate声明用户输入更新的部门名称或组名的文本框以及提交或取消当前操作的用户连接
<EditItemTemplate>
<tr style=backgroundcolor: #ADDE>
<td>
<asp:TextBox ID=txtName runat=server
Text=<%# Bind(Name) %>
MaxLength= /><br />
</td>
<td>
<asp:TextBox ID=txtGroupName runat=server Text=<%#
Bind(GroupName) %> MaxLength= /><br />
</td>
<td>
<asp:LinkButton ID=btnUpdate runat=server
CommandName=Update Text=Update />
<asp:LinkButton ID=btnCancel runat=server
CommandName=Cancel Text=Cancel />
</td>
</tr>
</EditItemTemplate>
[] [] [] []