注册 登录
编程论坛 ASP.NET技术论坛

[求助]运行到DropDownList处有问题

hinroe 发布于 2007-08-02 16:08, 664 次点击

编辑页面的所有文本框和时间控件还有下拉列表都放在一个DetailView中,当进入编辑页面时,在.cs后台代码页中有如下代码 DropDownList ddl = (DropDownList)DetailsViewFile.Rows[0].FindControl("dropCategory");
ddl.DataSource = MediaCategories.GetInfoByCategoryID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();对第一行代码提示"索引超出范围。必须为非负值并小于集合大小。\r\n参数名: index",这是怎么回事,其中HTML代码:

只有本站会员才能查看附件,请 登录
请大家帮看看
4 回复
#2
冰彩虹2007-08-02 18:24
以下是引用hinroe在2007-8-2 16:08:46的发言:

DropDownList ddl = (DropDownList)DetailsViewFile.Rows[0].FindControl("dropCategory");
ddl.DataSource = MediaCategories.GetInfoByCategoryID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();

这段代码写哪儿在?

#3
hinroe2007-08-02 19:56

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = 0, cid = 0;
try
{
id = int.Parse(Request["id"]);//用来判断它是添加还是修改
ViewState.Add("id", id);
cid = int.Parse(Request["cid"]);//上个list文件传过来的categoryID,用来确定添加的文件属于哪个类别
ViewState.Add("cid", cid);
}
catch
{
this.GoError("页面错误", "无效的参数或链接错误。");
}
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.Rows[0].FindControl("dropCategory");
ddl.DataSource = MediaCategories.GetInfoByCategoryID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = file.CategoryID.ToString();
ViewState.Add("cid",cid);
}
else //添加
{
MediaCategories category = new MediaCategories(cid);
DetailsViewFile.DefaultMode = DetailsViewMode.Insert;
DropDownList ddl = (DropDownList)DetailsViewFile.Rows[0].FindControl("dropCategory");
if (category.Level == 1)
{
ddl.DataSource = MediaCategories.GetInfoByCategoryID(cid);
ddl.DataBind();
}
else
{
ddl.DataSource = MediaCategories.GetInfoByCategoryID(category.ParentID);
ddl.DataBind();
ddl.SelectedValue = category.CategoryID.ToString();
}
}

}
}
就这段代码,修改和添加是一个页面,所以进行了判断,页面加载时的时候,请您帮看看吧!先谢了!

#4
冰彩虹2007-08-02 21:14
DetailsViewFile.Rows类别有很多种的哈,你要做判断的

而且如上编码感觉有点不规范

模板列的数据绑定常规做法是在DetailsViewFile的DataBound事件里
#5
sean882007-08-03 15:53
这样试试:

if(DetailsViewFile.Item.ItemType == ListItemType.Item ||DetailsViewFile.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)DetailsViewFile.Rows[0].FindControl("dropCategory");
}
1