LinqDataSource.Update(IDictionary, IDictionary, IDictionary) Метод

Определение

Выполняет операцию обновления.

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

Параметры

keys
IDictionary

Ключевые значения строк для обновляемых записей.

values
IDictionary

Значения строк, для обновления в источнике данных.

oldValues
IDictionary

Значения строк, которые проверяются на наличие конфликтов данных.

Возвращаемое значение

Количество записей, затронутых операцией обновления.

Примеры

В следующем примере показано, как программно обновить запись в источнике данных после нажатия пользователем кнопки. Код передает ListDictionary в метод объект , содержащий значение ключа, ListDictionary объект , содержащий исходные значения, и ListDictionary объект, содержащий новые значения Update .

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

В следующем примере показана разметка для предыдущего примера.

<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" />

Комментарии

Как правило, не требуется вызывать Update метод из кода. Элемент управления с привязкой к данным автоматически вызывает Update метод, когда пользователь выполняет действие по обновлению записи. Метод вызывается явным Update образом, если требуется создать собственный процесс обновления данных.

Применяется к