LinqDataSource.Update(IDictionary, IDictionary, IDictionary) Metodo

Definizione

Esegue un'operazione di aggiornamento.

public:
 int Update(System::Collections::IDictionary ^ keys, System::Collections::IDictionary ^ values, System::Collections::IDictionary ^ oldValues);
public int Update (System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues);
member this.Update : System.Collections.IDictionary * System.Collections.IDictionary * System.Collections.IDictionary -> int
Public Function Update (keys As IDictionary, values As IDictionary, oldValues As IDictionary) As Integer

Parametri

keys
IDictionary

Valori delle chiavi delle righe per i record da aggiornare.

values
IDictionary

Valori delle righe da aggiornare nell'origine dati.

oldValues
IDictionary

Valori delle righe valutati per rilevare conflitti di dati.

Restituisce

Numero di record interessati dall'operazione di aggiornamento.

Esempio

Nell'esempio seguente viene illustrato come aggiornare a livello di codice un record nell'origine dati dopo che l'utente fa clic su un pulsante. Il codice passa un ListDictionary oggetto che contiene il valore della chiave, un ListDictionary oggetto che contiene i valori originali e un ListDictionary oggetto che contiene i nuovi valori al Update metodo.

protected void Reset_Click(object sender, EventArgs e)
{
    ListDictionary keyValues = new ListDictionary();
    ListDictionary newValues = new ListDictionary();
    ListDictionary oldValues = new ListDictionary();

    keyValues.Add("ProductID", int.Parse(((Label)DetailsView1.FindControl("IDLabel")).Text));

    oldValues.Add("ProductName", ((Label)DetailsView1.FindControl("NameLabel")).Text);
    oldValues.Add("ProductCategory", ((Label)DetailsView1.FindControl("CategoryLabel")).Text);
    oldValues.Add("Color", ((Label)DetailsView1.FindControl("ColorLabel")).Text);

    newValues.Add("ProductName", "New Product");
    newValues.Add("ProductCategory", "General");
    newValues.Add("Color", "Not assigned");

    LinqDataSource1.Update(keyValues, newValues, oldValues);

    DetailsView1.DataBind();
}
Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim keyValues As New ListDictionary()
    Dim newValues As New ListDictionary()
    Dim oldValues As New ListDictionary()

    keyValues.Add("ProductID", Int32.Parse(CType(DetailsView1.FindControl("IDLabel"), Label).Text))

    oldValues.Add("ProductName", CType(DetailsView1.FindControl("NameLabel"), Label).Text)
    oldValues.Add("ProductCategory", CType(DetailsView1.FindControl("CategoryLabel"), Label).Text)
    oldValues.Add("Color", CType(DetailsView1.FindControl("ColorLabel"), Label).Text)

    newValues.Add("ProductName", "New Product")
    newValues.Add("ProductCategory", "General")
    newValues.Add("Color", "Not assigned")

    LinqDataSource1.Update(keyValues, newValues, oldValues)

    DetailsView1.DataBind()
End Sub

Nell'esempio seguente viene illustrato il markup per l'esempio precedente.

<asp:LinqDataSource  
  ContextTypeName="ExampleDataContext" 
  TableName="Products" 
  EnableUpdate="true" 
  ID="LinqDataSource1" 
  runat="server">
</asp:LinqDataSource>
<asp:DetailsView 
  DataSourceID="LinqDataSource1" 
  AllowPaging="True" 
  ID="DetailsView1" 
  runat="server" 
  AutoGenerateRows="False">
  <Fields>
    <asp:templatefield HeaderText="Product ID">
        <itemtemplate>
            <asp:Label ID="IDLabel" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Product Name">
        <itemtemplate>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Category">
        <itemtemplate>
            <asp:Label ID="CategoryLabel" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Color">
        <itemtemplate>
            <asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
  </Fields>
</asp:DetailsView>
<asp:button 
  ID="Button1"
  Text="Reset with default values" 
  runat="server" 
  onclick="Reset_Click" />
<asp:LinqDataSource  
  ContextTypeName="ExampleDataContext" 
  TableName="Products" 
  EnableUpdate="true" 
  ID="LinqDataSource1" 
  runat="server">
</asp:LinqDataSource>
<asp:DetailsView 
  DataSourceID="LinqDataSource1" 
  AllowPaging="True" 
  ID="DetailsView1" 
  runat="server" AutoGenerateRows="False">
  <Fields>
  <asp:templatefield HeaderText="Product ID">
        <itemtemplate>
            <asp:Label ID="IDLabel" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Product Name">
        <itemtemplate>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Category">
        <itemtemplate>
            <asp:Label ID="CategoryLabel" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
    <asp:templatefield HeaderText="Color">
        <itemtemplate>
            <asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
        </itemtemplate>
    </asp:templatefield>
  </Fields>
</asp:DetailsView>
<asp:button 
  ID="Button1"
  Text="Reset with default values" 
  runat="server" 
  onclick="Add_Click" />

Commenti

In genere, non è necessario chiamare il Update metodo dal codice. Il controllo associato a dati chiamerà automaticamente il Update metodo quando l'utente esegue un'azione per aggiornare un record. Chiamare in modo esplicito il Update metodo quando si vuole creare un processo personalizzato per l'aggiornamento dei dati.

Si applica a