ListView.UpdateItem(Int32, Boolean) Metodo

Definizione

Aggiorna nell'origine dati il record in corrispondenza dell'indice specificato.

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)

Parametri

itemIndex
Int32

Indice dell'elemento da aggiornare.

causesValidation
Boolean

true per eseguire la convalida della pagina quando viene chiamato questo metodo, altrimenti false.

Eccezioni

itemIndex è minore di 0.

-oppure-

L'oggetto DataSourceView associato al controllo ListView è null.

Esempio

Nell'esempio seguente viene illustrato come usare il UpdateItem metodo per aggiornare a livello di codice un ListView elemento nell'origine dati.

<%@ 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>

Commenti

Usare il metodo per aggiornare a livello di codice il UpdateItem record in corrispondenza dell'indice specificato nell'origine dati. In genere si usa questo metodo quando si vuole aggiornare un record dall'esterno del ListView controllo, ad esempio da un controllo diverso nella pagina.

Nota

Questo metodo può essere chiamato solo per un elemento che contiene un controllo di input associato a dati bidirezionale. Per altre informazioni sulle espressioni di associazione bidirezionale, vedere Associazione ai database.

Per specificare se la convalida della pagina viene eseguita prima dell'operazione di aggiornamento, usare il causesValidation parametro .

Questo metodo genera gli ItemUpdated eventi e ItemUpdating .

Si applica a

Vedi anche