views:

198

answers:

3

I'm using asp.net 2.0. I am using declarative datasources. For some things in the code behind I want access to the object returned by Foo.Bar (In the following example). the system is caching it so I should be able to acces that version instead of having to re-call Foo.Bar(). How do I do this?

<asp:ObjectDataSource ID="MyLuckDataSource1" runat="server" 
    TypeName="Foo.Bar" SelectMethod="GetMoreFoo" 
    CacheDuration="Infinite" CacheExpirationPolicy="Sliding" 
    EnableCaching="True">
    <SelectParameters>
        <asp:ControlParameter ControlID="BarID" Name="bar_code" Type="String" Direction="Input" DefaultValue="1011" />
    </SelectParameters>
</asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server"  runat="server" DataSourceID="MyLuckDataSource1" ...
+1  A: 

Try OnRowDataBound event of the GridView.

Just like:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var some = (Foo.SomeBar) e.Row.DataItem;
        somelabel.Text = some.Date.ToString();
    }
}

Ps. with try, I mean it works :)

eglasius
A: 

I think Freddy is right about the OnRowDataBound even. Though I think you can only examine the text results of the cells during the bind on the gridview, rather than the underlying object, but I can't remember.

At the least you can store the results in the session, before they are returned from GetMoreFoo().

I do the whole model view presenter (MVP) thing, and I wire the object data source to the presenter, so I can access whatever I want on the View (aspx.cs) or Presenter, when the GetMoreFoo() function is called.

Mark Rogers
A: 

You can also catch your collection results in the ObjectDataSource.Selected event by examining the e.ReturnValue property.

protected void MyLuckDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    List<Foo> x = (List<Foo>)e.ReturnValue;

    // do whatever you need with the results here...
}
Scott Ivey