有学员在DetailsView控件编辑模式下使用DropDownList控件时(在实现第三波书店编辑图书时绑定所有图书种类),每每找不到相应的控件,以下便是此问题的解决方案,请大家参考!
protected void dvBook_PreRender(object sender, EventArgs e)
{
if (this.IsPostBack)//此句必须添加,在页面首次加载时给编辑模板下的DropDownList控件绑定数据源
{
if (dvBook.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddlCategory= dvBook.FindControl("ddlCategory") as DropDownList;
ddlCategory.DataSource = new CategoryManager().GetAllCategories();
ddlCategory.DataTextField = "Name";
ddlCategory.DataValueField = "ID";
ddlCategory.DataBind();
}
}
}