【问题标题】:Binding Dropdown list which is inside footer of gridview绑定下拉列表,它位于gridview的页脚内
【发布时间】:2013-03-08 14:58:02
【问题描述】:

我有一个带有 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();
        }
    }

【问题讨论】:

  • 你应该使用 gridview RowDataBound 事件

标签: asp.net gridview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
     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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      相关资源
      最近更新 更多