【问题标题】:GridView with ObjectDataSource Show Footer on Empty Data带有 ObjectDataSource 的 GridView 在空数据上显示页脚
【发布时间】:2013-04-07 09:40:02
【问题描述】:

GridView 中没有ShowFooterWhenEmpty 属性;(

当我不使用 ObjectDataSource 进行数据绑定时,它很简单:

...
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable data = new DataTable();
    conn.Open();
    adapter.Fill(data);
    conn.Close();

    if (data.Rows.Count > 0)
    {
        grid.DataSource = data;
        grid.DataBind();
    }
    else
    {
        data.Rows.Add(data.NewRow());
        grid.DataSource = data;
        grid.DataBind();

        int TotalColumns = grid.Rows[0].Cells.Count;
        grid.Rows[0].Cells.Clear();
        grid.Rows[0].Cells.Add(new TableCell());
        grid.Rows[0].Cells[0].ColumnSpan = TotalColumns;
        grid.Rows[0].Cells[0].Text = "No Records Found";
    }
...

Page_Load() if (!IsPostBack) {...}调用

现在ObjectDataSource 代表自动绑定和分页。

我应该如何渲染插入按钮所在的页脚?

尝试了ObjectDataSourceOnSelected 事件,但我不知道如何在其中添加一行。

protected void ODS_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    DataSet ds = e.ReturnValue as DataSet;
    // Add empty row here or 'No Records Found' string to force Footer show
}

请帮忙!

【问题讨论】:

  • 如果您有空数据模板,为什么还要坚持在页脚中包含信息?
  • 有相关控件的插入按钮
  • 您可以在空数据模板中复制它们。
  • 不确定像 (DropDownList)grid.FooterRow.FindControl("eventnoteDDL") 这样的调用是否会起作用。需要以其他方式复制它们吗?
  • 这就是我们切换到 ListViews 的原因。您保持简单的使用并绑定到相同的数据源,但您会获得插入模板。

标签: asp.net gridview objectdatasource


【解决方案1】:

您已经知道,当 GridView 为空(无行)时,页脚不会显示。解决方法是确保在没有实际数据存在时从 ObjectDataSource 返回一个虚拟行。

我建议检查GridView DataBound事件中的行数,如果没有,则修改ObjectDataSource的SelectMethod返回一个dummy行的dummy data,然后重新绑定gridview。虚拟数据的示例可能是:

ID : null
Name : 'No data, please add using form below' ...etc...
Description : null
...
etc
...

GridView 将在重新绑定后显示页脚。

【讨论】:

  • 似乎这是唯一的快速-n-dirty SQL 级解决方案。与 gridview OnPreRender 事件配合使用以隐藏空值。
猜你喜欢
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
相关资源
最近更新 更多