熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在ASP.NET 3.5中使用新的ListView控件[3]

2022-06-13   來源: .NET編程 

  用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 />&nbsp;
<asp:LinkButton ID=btnCancel runat=server
CommandName=Cancel Text=Cancel />
</td>
</tr>
</EditItemTemplate>

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/14844.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.