我有一个带有页眉和页脚的3列(产品,数量,价格)的网格视图。我为产品页脚添加了一个下拉列表。现在我想将此下拉列表与数据集中的产品绑定,任何人都可以帮助我吗? 我在cs文件中使用了以下代码,但我在查找控件附近得到错误
“对象引用未设置为对象的实例。
protected void gv_page2_RowDataBound(object sender, GridViewRowEventArgs e)
{
dal = new DAL();
ds = new DataSet();
ds=dal.DALBindFooterDDL();
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddl = (DropDownList)gv_page2.FooterRow.FindControl("ftrDDL");
ddl.DataSource = ds.Tables[0];
ddl.DataBind();
}
}
答案 0 :(得分:3)
使用RowCreated
(或RowDataBound
事件) - 例如
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.FooterRow)
{
// Find the product drop-down list, you can id (or cell number)
var ddlProducts = e.Row.FindControl("Products") as DropDownList;
// var ddlProducts = e.Row.Cells[0].Controls[0]; // Finding by cell number and index
if (null != ddlProducts)
{
// bind to the data
}
}
}
免责声明:未经测试的代码 - 用于获取实际解决方案的想法/提示
答案 1 :(得分:1)
protected void gv_page2_RowDataBound(object sender, GridViewRowEventArgs e)
{
dal = new DAL();
ds = new DataSet();
ds=dal.DALBindFooterDDL();
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddl = e.Row.FindControl("ftrDDL") as DropDownList;
ddl.DataSource = ds.Tables[0];
ddl.DataBind();
}
}