开发者

ListBox is null in Select method of ObjectDataSource

I couldn't come up with a good subject. I have a simple ASP.NET 3.5 form. If you follow the numbered comments, I explain how I am stepping through the code when I run the page in debug mode. Both of the below functions are methods declared in the code behind file of the page, both are in the same _Default class. The ManagersListBox is declared in the Default.aspx page. Why is it that in the context of the GetSubordinates, the ManagersListBox is null? It's as if it disappears for a moment and then reappears after returning from the GetSubordinates method. Obviously the solution is to parameterize GetSuborindates, but that is not my concern. I am trying to learn how ASP.NET works and I would really like to understand why I am seeing this behavior "disappearing object". Thanks.

    <asp:ListBox ID="ManagersListBox" runat="server" Width="293px" 
         DataTextField="LoginID" DataSourceID="ObjectDataSource2"
        DataValueField="EmployeeID" onload="ManagersListBox_Load"                     
        AutoPostBack="true" onprerender="ManagersListBox_PreRender"></asp:ListBox>


    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
        SelectMethod="GetSubordinates" TypeName="WebApplication._Default">
    </asp:ObjectDataSource>

Code behind file:

protected void ManagersListBox_PreRender(object sender, EventArgs e)
{

    if (ManagersListBox != null)
    {
        //1. Right here, ManagersListBox is not null
    }

    //2. This call causes the Object开发者_开发技巧DataSource to call GetSubordinates
    ObjectDataSource2.Select();
    //4. After stepping out of GetSubordinates and back here,
    //   ManagersListBox is again non-null.
}

public List<DataModel.Employee> GetSubordinates()//int ManagerID)
{
    //3. ManagersListBox is always null here
    using (DataModel.AdventureWorksEntities entities = new DataModel.AdventureWorksEntities())
    {                
        if (ManagersListBox != null)
        {

            return (from employee in entities.Employees
                    where employee.Manager.EmployeeID == Convert.ToInt32(ManagersListBox.SelectedValue)
                    select employee).ToList();
        }
        else
        {
            return (from employee in entities.Employees                            
                    select employee).ToList();
        }
    }
}


Looking at this Microsoft article it appears that when the SelectMethod is called in your code a new page instance is created and this method is used.

If it is an instance method, the business object is created and destroyed each time the method that is specified by the SelectMethod property is called.

As this is a separate instance of the page class you will not have access to the controls on the original page. Also as this is an instance of the page class and not being run as part of the page lifecyle none of it's related controls will have been initialised.
It looks like parameterising this method is your best bet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜