简体   繁体   中英

How to use global variables in ObjectDataSource.SelectMethod?

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
}

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.

I had the same problem, but i found solution.

Or you can handle the follow event in VB code

 Protected Sub PagingDataSource_ObjectCreating(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs) Handles PagingDataSource.ObjectCreating
        e.ObjectInstance = Me
    End Sub

In this way you will define the current instance of you class and whole his variables. It is work I tested it from myself. Also define the name of metod for ODS

<asp:ObjectDataSource ID="PagingDataSource"    OnObjectCreating="PagingDataSource_ObjectCreating" 

The ASP.NET page lifecycle is probably such that Page_Load is happening after the initialization of your datasource. Try using an earlier event, such as Page_Init or Page_PreInit.

您还可以尝试对象数据源的OnSelect事件,并从代码隐藏处发送相应的参数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM