列表框是ObjectDataSource控件的Select方法无效 [英] ListBox is null in Select method of ObjectDataSource

查看:133
本文介绍了列表框是ObjectDataSource控件的Select方法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能想出一个好题材。我有一个简单的ASP.NET 3.5的形式。如果按照编号的意见,我解释一下我是如何通过code步进,当我在调试模式下运行的页面。两者的下面的功能是在后面的页的文件中的code中声明的方法,两者都在同一_Default类。该ManagersListBox在Default.aspx页声明。为什么,在GetSubordinates的上下文中,ManagersListBox为空?这是因为如果它消失了一会儿,然后从GetSubordinates方法返回后再次出现。显然,解决办法是参数化GetSuborindates,但是这不是我关心的。我想学习ASP.NET是如何工作的,我真的想明白为什么我看到这种行为消失的对象。谢谢你。

 < ASP:列表框ID =ManagersListBox=服务器WIDTH =293px
         DataTextField =为LoginID的DataSourceID =ObjectDataSource2
        DataValueField =雇员的onload =ManagersListBox_Load
        上的AutoPostBack prerender =ManagersListBox_ preRender&GT =真;< / ASP:列表框>
    < ASP:ObjectDataSource控件ID =ObjectDataSource2=服务器
        SelectMethod =GetSubordinates的TypeName =WebApplication._Default>
    < / ASP:ObjectDataSource控件>

隐藏文件code:

 保护无效ManagersListBox_ preRender(对象发件人,EventArgs的发送)
{    如果(ManagersListBox!= NULL)
    {
        // 1。就在这里,ManagersListBox不为空
    }    // 2。该调用导致ObjectDataSource控件调用GetSubordinates
    ObjectDataSource2.Select();
    // 4。走出GetSubordinates,回到这里之后,
    // ManagersListBox又非空。
}公开名单< D​​ataModel.Employee> GetSubordinates()// INT经理ID)
{
    // 3。 ManagersListBox总是空在这里
    使用(DataModel.AdventureWorksEntities实体=新DataModel.AdventureWorksEntities())
    {
        如果(ManagersListBox!= NULL)
        {            返回(从entities.Employees员工
                    其中,employee.Manager.EmployeeID == Convert.ToInt32(ManagersListBox.SelectedValue)
                    选择员工).ToList();
        }
        其他
        {
            返回(从entities.Employees员工
                    选择员工).ToList();
        }
    }
}


解决方案

望着这<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.selectmethod.aspx\"相对=nofollow> Microsoft文章看来,当SelectMethod被称为在code为创建一个新的页面实例,使用这种方法。


  

如果它是一个实例的方法,该
  业务对象被创建并
  每次销毁的是方法
  由SelectMethod属性指定的
  被调用。


由于这是网页类的一个单独的实例,您将无法访问的控件原来的页面上。也因为这是网页类的一个实例,而不是被运行的页面生命周期没有它的相关的控件将被初始化的一部分。结果
它看起来像parameterising这种方法是你最好的选择。

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 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();
        }
    }
}

解决方案

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.

这篇关于列表框是ObjectDataSource控件的Select方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆