I have following code in aspx page:
<asp:ObjectDataSource id="odsOuterSource" TypeName="my.namespace.page"
SelectMethod="GetTestObject" DataObjectTypeName="my.namespace.Entities.TestObject" />
<asp:ObjectDataSource id="odsInnerSource" TypeName="my.namespace.page"
SelectMethod="GetAnotherTestObject"
DataObjectTypeName="my.namespace.Entities.AnotherTestObject" />
and following code in code-behind:
public TestObject GetTestObject()
{
Logic myLogic = new MyLogic();
return myLogic.GetTestObject();
}
public AnotherTestObject GetAnotherTestObject()
{
Logic myLogic = new MyLogic();
return myLogic.GetAnotherTestObject(testObject);
}
Now my question is if there is a way to get the TestObject which was returned by the OuterSource so that I could use it for the InnerSource without having to call GetTestObject again.
I hope you can help me with this.
Edit: For better readability than a comment
Thanks for your answers, but i have a question to Mudu’s answer:
Is there a way to do this if my OuterSource has a parameter?
For example
<asp:ObjectDataSource id="odsOuterSource" TypeName="my.namespace.page"
SelectMethod="GetTestObject" DataObjectTypeName="my.namespace.Entities.TestObject">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" DefaultValue="0" />
</SelectParameters>
</asp:ObjectDataSource>
because then even my GetTestObject would have a parameter:
public TestObject GetTestObject(int id)
{
Logic myLogic = new MyLogic();
return myLogic.GetTestObject(id);
}
You could save it in a member, either as
TestObjector as aLazy<TestObject>:With this code, you can receive it any number of times you want. It will be fetched via
MyLogiconce and re-returned on subsequent invocations:As ASP.NET creates a new instance of your page for each request, the
Lazy<TestObject>object is recreated for each request. Therefore, yourTestObjectis actually fetched once per request.