ASP.NET 2.0:为ObjectDataSource指定对象的实例 [英] ASP.NET 2.0: Specifying an instance of an object for an ObjectDataSource

查看:81
本文介绍了ASP.NET 2.0:为ObjectDataSource指定对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ObjectDataSource将数据绑定到GridView;它可以正常工作,除了它总是创建一个新对象以用作数据源.我可以很好地进行所有设置,但是不能使用现有对象的实例为其指定为数据源".是否有可能做到这一点?如果是这样,怎么办?

I'm using an ObjectDataSource to bind data to a GridView; it works fine except that it always creates a new object to use as a data source. I can do all the setup just fine but I cannot use an instance of an existing object to specify as the "data source" for it. Is it possible to do this? If so, how?

如果不可能,为什么?

这是正在发生的事情(对象类型已更改)的要点:在第一页上,您正在编辑狗的属性.属性之一是有幼犬",如果为真,则在下一页中指定这些幼犬的名称.我的情况是,这些幼犬不是与原始狗联系在一起,而是与新"狗联系在一起. (这是我的问题是女狗"的暗示,这是巧合.;-))

Here's the gist of what's going on (object types changed): On the first page you are editting the attributes for a dog. One of the attributes is "has puppies" and if it's true, the next page you specify the names of those puppies. What's happening in my case is that those puppies are not getting linked to the original dog but to a "new" dog. (The implication that my problem is a "female dog" was coincidental. ;-) )

推荐答案

为ObjectDataSource上的ObjectCreating事件创建事件处理程序.

Create an event handler for the ObjectCreating event on the ObjectDataSource.

您可以使用ObjectDataSourceEventArgs属性将实例分配给

You can assign the instance to using the ObjectDataSourceEventArgs property

protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
    e.ObjectInstance = myObject;
}

也将此事件关联到标记中

Wire this event up in the markup too

<asp:ObjectDataSource OnObjectCreating="ObjectDataSource1_ObjectCreating" />

这篇关于ASP.NET 2.0:为ObjectDataSource指定对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆