griview绑定数据源后显示调用update方法的实现

来源:互联网 发布:超星网络课答案 编辑:程序博客网 时间:2024/06/03 20:43

通常情况下,不需要调用 Update 在代码中的方法。 数据绑定控件将自动调用 Update 方法时用户执行操作以更新记录。 显式调用 Update 方法如果您想要创建您自己的进程用于更新数据。

语法如下:

public int Update(IDictionary keys,IDictionary values,IDictionary oldValues)

参数

keys
Type: System.Collections.IDictionary

要更新的记录行键值。

values
Type: System.Collections.IDictionary

要在数据源中更新的行值。

oldValues
Type: System.Collections.IDictionary

行值,其计算结果为检测数据冲突。

返回值

Type: System.Int32

更新操作所影响的记录数。


下面的示例演示如何以编程方式更新数据源中的记录,用户单击按钮之后。 该代码将 ListDictionary 对象,其中包含的密钥值, ListDictionary 对象,其中包含的原始值和一个 ListDictionary 对象,其中包含这些新值与 Update 方法。

C#代码:

要加上using System.Collections.Specialized;

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();}
下面的示例演示上一示例的标记。

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