C# ObjectDataSource的Select方法中的ListBox为空

C# ObjectDataSource的Select方法中的ListBox为空,c#,asp.net,C#,Asp.net,我想不出一个好题目。我有一个简单的ASP.NET3.5表单。如果您按照编号的注释进行操作,我将解释在调试模式下运行页面时如何单步执行代码。以下两个函数都是在页面的代码隐藏文件中声明的方法,它们都在同一个默认类中。ManagerListBox在Default.aspx页面中声明。为什么在GetSubstances的上下文中,managerListBox为null?就好像它消失了一会儿,然后在从getsubstances方法返回后重新出现。显然,解决方案是参数化GetSuborindates,但这不

我想不出一个好题目。我有一个简单的ASP.NET3.5表单。如果您按照编号的注释进行操作,我将解释在调试模式下运行页面时如何单步执行代码。以下两个函数都是在页面的代码隐藏文件中声明的方法,它们都在同一个默认类中。ManagerListBox在Default.aspx页面中声明。为什么在GetSubstances的上下文中,managerListBox为null?就好像它消失了一会儿,然后在从getsubstances方法返回后重新出现。显然,解决方案是参数化GetSuborindates,但这不是我关心的问题。我正在努力学习ASP.NET是如何工作的,我真的很想理解为什么我看到这种行为“正在消失的对象”。谢谢

    <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>

代码隐藏文件:

protected void ManagersListBox_PreRender(object sender, EventArgs e)
{

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

    //2. This call causes the ObjectDataSource 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();
        }
    }
}
受保护的void管理器列表框\u预呈现(对象发送器,事件参数e)
{
if(ManagersListBox!=null)
{
//1.在这里,ManagersListBox不为空
}
//2.此调用导致ObjectDataSource调用GetSubstances
ObjectDataSource2.Select();
//4.离开下属回到这里后,
//ManagerListBox再次为非空。
}
public List getsubstances()//int ManagerID)
{
//3.ManagerListBox在此始终为空
使用(DataModel.AdventureWorksEntities entities=new DataModel.AdventureWorksEntities())
{                
if(ManagersListBox!=null)
{
返回(来自实体中的员工。员工
其中employee.Manager.EmployeeID==Convert.ToInt32(managerListBox.SelectedValue)
选择employee.ToList();
}
其他的
{
返回(来自实体中的员工。员工
选择employee.ToList();
}
}
}
看看这个,当在代码中调用SelectMethod时,会创建一个新的页面实例并使用此方法

如果是实例方法,则 业务对象被创建和删除 每次都销毁了该方法 由SelectMethod属性指定 被称为

由于这是page类的一个单独实例,您将无法访问原始页面上的控件。此外,由于这是page类的一个实例,并且不是作为page生命周期的一部分运行,因此它的任何相关控件都不会被初始化。
将此方法参数化似乎是您的最佳选择。

看看这一点,似乎在代码中调用SelectMethod时,会创建一个新的页面实例并使用此方法

如果是实例方法,则 业务对象被创建和删除 每次都销毁了该方法 由SelectMethod属性指定 被称为

由于这是page类的一个单独实例,您将无法访问原始页面上的控件。此外,由于这是page类的一个实例,并且不是作为page生命周期的一部分运行,因此它的任何相关控件都不会被初始化。

看来,将此方法参数化是您的最佳选择。

现在完全有意义了。这意味着我应该在可能的情况下使用静态方法,和/或将其放在一个单独的业务类中,以避免不必要地新建一个新的页面实例。现在完全有意义了。这意味着我应该尽可能使用静态方法,和/或将其放在一个单独的业务类中,以避免不必要地新建一个新的页面实例。不是完整的答案,但您也可以在此处检查我的答案:不是完整的答案,但您也可以在此处检查我的答案: