ListView.UpdateItem(Int32, Boolean) 方法

定义

更新数据源中指定索引处的记录。

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

例外

itemIndex 小于 0。

- 或 -

DataSourceView 控件相关联的 ListView 对象为 null

示例

以下示例演示如何使用 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 参数。

此方法引发 ItemUpdatedItemUpdating 事件。

适用于

另请参阅