views:

707

answers:

2

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?

If it's not possible, why?

EDIT: 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. ;-) )

A: 

As I just discovered in my own question here, items stored in the Application Cache are going to pass themselves as a reference for use. You may consider storing your data there (or potentially in the Session as well) and pass items that way.

Dillie-O
The object I want to use is in the Session. Do you know how I can specify that object to be the source for my ObjectDataSource?
Austin Salonen
Simply wrap the session object around a direct cast statement to specify the format for the item. Something along the lines of MyObjectDataSource = DirectCast(Session("MyStoredData"), DataTable) in VB.net
Dillie-O
+4  A: 

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

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" />
George
Exactly what I was looking for -- thanks!
Austin Salonen
Exactly what I was looking for, too! Thank you!
Mark Good
Does this need to be explicitly added as an event (eg during page_load) or will asp take care of that?
Adam Tolley
It needs to be explicitly wired up...see updated answer.
George