【发布时间】:2012-07-06 07:01:23
【问题描述】:
我不断收到此错误,不知道为什么。我用谷歌搜索并扫描了 asp.net 网站两天,所以我需要一些帮助。错误是:
Object reference not set to an instance of an object
这是我的代码:
DropDownList DropDownList1 =(DropDownList)ListView1.InsertItem.FindControl("DropDownList1");
string highToLow = DropDownList1.SelectedValue;
string lowToHigh = DropDownList1.SelectedValue;
if (highToLow == "1")
{
var exmapleFilter = from users in testEntities.users
orderby users.id descending
select users;
ListView1.DataBind();
}
我将下拉列表的值设置为 1 表示高,2 表示低,并且在选定的索引发生更改时,我想运行 ADO.net 实体框架代码以返回已排序的数据列表。
我目前正在使用 linq 数据源和列表视图来显示我的数据库中的内容。
谢谢。
编辑:
这是堆栈跟踪
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_s0ked5y3
StackTrace:
at Default.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
in Default:line 120
at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent()
at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.
RaisePostDataChangedEvent()
at System.Web.UI.Page.RaiseChangedEvents()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
【问题讨论】:
-
哪一行抛出异常?
-
这一行被高亮显示为错误 DropDownList DropDownList1 =(DropDownList)ListView1.InsertItem.FindControl("DropDownList1");
-
我如何获得堆栈跟踪? (我跳过书中的调试部分得到了什么)
-
请在 Default.aspx.cs 的第 120 行复制/粘贴您帖子中的代码
-
您确定您的代码“(DropDownList)ListView1.InsertItem.FindControl("DropDownList1");"...确实返回了下拉列表的实例...我建议您放置一个该行的断点并使用即时窗口(使用 Ctrl+Alt+i)运行此代码并查看它是否返回一个实例...
标签: c# asp.net visual-studio-2010 entity-framework drop-down-menu