Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use global variables in ObjectDataSource.SelectMethod?

Tags:

c#

asp.net

I'm using ListView + DataPager + ObjectDataSource mix.

In the implementation of my SelectMethod of an ObjectDataSource I'd like to access a global variable set in the PageLoad() event (the method will return just the proper number of items per page). Although the variable is initialized in PageLoad() it appears to be null in SelectMethod. How can I change that?

    <asp:ObjectDataSource ID="ItemsObjectDataSource" runat="server" EnablePaging="True"
                        SelectMethod="WrapSearchResults" SelectCountMethod="CountSearchResults"
                        TypeName="Catalogue">
                        <SelectParameters>
                            <asp:QueryStringParameter Name="startRowIndex" QueryStringField="page" Type="Int32" DefaultValue="0" />
                            <asp:Parameter Name="maximumRows" Type="Int32" DefaultValue="10" />
                        </SelectParameters>
    </asp:ObjectDataSource>

SearchOption search;
protected void Page_Load(object sender, EventArgs e)
{
    search = new SearchOption(SessionParameters.Get(Session).User);
}

public IEnumerable<ResultWrapper> WrapSearchResults(int startRowIndex, int maximumRows)
{
    search.Limit = maximumRows; <-- null pointer exception
}
like image 424
friii Avatar asked Sep 10 '10 17:09

friii


1 Answers

This is a common gotcha with the Object Data Source. You always have to remember this:

The ODS will call the specified method through Reflection, not a specific instance of the object. (Unless you specify the instance to use yourself)

The value is null because the ODS called the method directly and it never was part of the asp.net page life cycle.

If you really need to do this, make the global variable STATIC.

protected **static** SearchOption search; 

If you take a look at the stack trace, just before the null exception you should see framework reflection calls. That should tip you off as to what's going on!

Here is the MSDN reference:

http://msdn.microsoft.com/en-us/library/ms227436.aspx

The ObjectDataSource control will create an instance of the source object, call the specified method, and dispose of the object instance all within the scope of a single request, if your object has instance methods instead of static methods (Shared in Visual Basic). Therefore, your object must be stateless. That is, your object should acquire and release all required resources within the span of a single request.

You can control how the source object is created by handling the ObjectCreating event of the ObjectDataSource control. You can create an instance of the source object, and then set the ObjectInstance property of the ObjectDataSourceEventArgs class to that instance. The ObjectDataSource control will use the instance that is created in the ObjectCreating event instead of creating an instance on its own.

like image 73
asawyer Avatar answered Sep 22 '22 17:09

asawyer