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 プロセスを作成する場合は、 メソッドを明示的に呼び出します。

適用対象