DropDownList的绑定问题
if (id > 0) //修改
{
DetailsViewFile.DataSource = MediaFile.GetListByCategoryID(id);
DetailsViewFile.DataBind();
MediaFile file = new MediaFile(id);
MediaCategories category = new MediaCategories(file.CategoryID);
DropDownList ddl = (DropDownList)DetailsViewFile.FindControl ( "dropCategory ");
ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();
ViewState.Add( "cid ", cid);
}
还是这段代码,运行到“ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID);”时,提示“
未将对象引用设置到对象的实例。”可是,我已经在MediaCategories.cs中定义了GetInfoByParentID这个方法,传入的是ParentID参数,查询出MediaCategories表中的所有字段作为ddl的数据源,不知道是什么原因,请大家帮帮忙!
------解决方案--------------------ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID)
既然是这错了,就是没有取出来数据。
------解决方案--------------------id值的问题!~
------解决方案--------------------DropDownList ddl = (DropDownList)DetailsViewFile.FindControl ( "dropCategory ");没有找到这个控件,你调试下看看是不是dll为空
------解决方案--------------------MediaCategories.GetInfoByParentID(category.ParentID);
应该返回了null
------解决方案--------------------你测试一下MediaCategories.GetInfoByParentID(category.ParentID)这一句有没有返回值,
我想应该是你这一行绑定数据出了问题,category.ParentID是不是合法的ID值
------解决方案--------------------ID 取值的问题
------解决方案--------------------ddl 为空,findcontrol 失败
------解决方案--------------------楼主设个断点调试下,监视MediaCategories.GetInfoByParentID(category.ParentID)返回值,然后做个判断
------解决方案--------------------> > > >
DropDownList ddl = (DropDownList)DetailsViewFile.FindControl ( "dropCategory ");
if(ddl == null) throw new Exception( "dropCategory 未找到。 ");
ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID);
------解决方案--------------------DropDownList ddl = (DropDownList)DetailsViewFile.FindControl ( "dropCategory ");
ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();
========================================================
DropDownList ddl = DetailsViewFile.FindControl ( "dropCategory ") as DropDownList;
if(ddl != null)
{
ddl.DataSource = MediaCategories.GetInfoByParentID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();
}