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

通常情况下,不需要调用 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" 
 οnclick="Reset_Click" />

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值