1

我有一个带有页眉和页脚的 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();
        }
    }
4

2 回答 2

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
      }
    }
}

免责声明:未经测试的代码 - 提供以获得实际解决方案的想法/提示

于 2013-01-11T10:32:09.430 回答
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();
        }
    }
于 2013-03-08T06:36:52.640 回答