ListView.UpdateItem(Int32, Boolean) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在資料來源中更新指定之索引上的資料錄。
public:
virtual void UpdateItem(int itemIndex, bool causesValidation);
public virtual void UpdateItem (int itemIndex, bool causesValidation);
abstract member UpdateItem : int * bool -> unit
override this.UpdateItem : int * bool -> unit
Public Overridable Sub UpdateItem (itemIndex As Integer, causesValidation As Boolean)
參數
- itemIndex
- Int32
要更新之項目的索引。
- causesValidation
- Boolean
true
表示呼叫這個方法時執行頁面驗證,否則為 false
。
例外狀況
範例
下列範例示範如何使用 UpdateItem 方法來以程序設計方式更新 ListView 數據源中的專案。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// <Snippet2>
protected void PreferredCheckBox_CheckedChanged(object sender, EventArgs e)
{
// Gets the CheckBox object that fired the event.
CheckBox chkBox = (CheckBox) sender;
// Gets the item that contains the CheckBox object.
ListViewDataItem item = (ListViewDataItem) chkBox.Parent.Parent;
// Update the database with the changes.
VendorsListView.UpdateItem(item.DisplayIndex, false);
}
// </Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView UpdateItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView UpdateItem Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
Select your preferred vendors:
<ul runat="server" id="lstVendors" style="text-align:left">
<li runat="server" id="itemPlaceholder" />
</ul>
<asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<li runat="server">
<asp:CheckBox runat="server" ID="PreferredCheckBox" AutoPostBack="True"
Checked='<%# Bind("PreferredVendorStatus") %>'
OnCheckedChanged="PreferredCheckBox_CheckedChanged" />
<asp:Label runat="server" ID="NameLabel" text='<%# Eval("Name") %>' /><br/>
</li>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="VendorsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT VendorID, Name, PreferredVendorStatus
FROM Purchasing.Vendor WHERE (ActiveFlag = 1)"
UpdateCommand="UPDATE Purchasing.Vendor
SET PreferredVendorStatus = @PreferredVendorStatus
WHERE (VendorID = @VendorID)" >
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <Snippet2>
Protected Sub PreferredCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
' Gets the CheckBox object that fired the event.
Dim chkBox As CheckBox = CType(sender, CheckBox)
' Gets the item that contains the CheckBox object.
Dim item As ListViewDataItem = CType(chkBox.Parent.Parent, ListViewDataItem)
' Update the database with the changes.
VendorsListView.UpdateItem(item.DisplayIndex, False)
End Sub
' </Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView UpdateItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView UpdateItem Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
Select your preferred vendors:
<ul runat="server" id="lstVendors" style="text-align:left">
<li runat="server" id="itemPlaceholder" />
</ul>
<asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<li runat="server">
<asp:CheckBox runat="server" ID="PreferredCheckBox" AutoPostBack="True"
Checked='<%# Bind("PreferredVendorStatus") %>'
OnCheckedChanged="PreferredCheckBox_CheckedChanged" />
<asp:Label runat="server" ID="NameLabel" text='<%# Eval("Name") %>' /><br/>
</li>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="VendorsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT VendorID, Name, PreferredVendorStatus
FROM Purchasing.Vendor WHERE (ActiveFlag = 1)"
UpdateCommand="UPDATE Purchasing.Vendor
SET PreferredVendorStatus = @PreferredVendorStatus
WHERE (VendorID = @VendorID)" >
</asp:SqlDataSource>
</form>
</body>
</html>
備註
UpdateItem使用方法,以程式設計方式更新數據源中指定索引處的記錄。 當您想要從控件外部 ListView 更新記錄時,通常會使用這個方法,例如從頁面上的不同控件更新記錄。
注意
這個方法只能針對包含雙向數據綁定輸入控件的專案呼叫。 如需雙向系結表達式的詳細資訊,請參閱 系結至資料庫。
若要指定是否在更新作業之前執行頁面驗證,請使用 causesValidation
參數。
這個方法會 ItemUpdated 引發和 ItemUpdating 事件。