即使DataSource为空时,是否还有一种使FooterTemplate(在GridView中)始终可见的简短方法?始终显示FooterTemplate,即使没有数据
回答
如果您希望它始终显示,无论内容如何,您是否可以将footer html放在GridView
之外,而不是放在FooterTemplate
之内?
如果由于某种原因,这不是一个选项,那么您可以选择add an null row to your data source if it's empty或subclass the GridView
& override the default behaviour。
这些是我所知道的唯一选项(虽然自从上次使用GridView
以来已经有一段时间了)。
我并不在意用Html来做这件事,问题是我想让列适合GridView列的宽度。 当数据存在时,我希望它显示一些摘要,并且在项目模板上按下“新建”按钮时,或者始终显示页脚时,插入项目(我在页脚中实现,知道我的意思? 换句话说: *有没有办法显示页脚(当没有数据时)? *实际上这是什么空数据行的东西,没有getcha(即时通讯使用EntityDataSource,我认为会更复杂或根本不可能)。 感谢哥们。 – Shimmy 2009-06-15 13:32:25
我也遇到过这个问题。来自Alconja的链接帮助很多(感谢Alconja),但GridView.FooterRow然后返回null。我需要它从页脚插入新记录。
这是我的最终解决方案。现在即使网格为空,也可以从页脚插入数据。
GridViewExtended.cs(App_Code文件夹中的类):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace YourNamespace
{
public class GridViewExtended : GridView
{
#region Public Properties
[Category("Behavior")]
[Themeable(true)]
[Bindable(BindableSupport.No)]
public bool ShowFooterWhenEmpty
{
get
{
if (this.ViewState["ShowFooterWhenEmpty"] == null)
{
this.ViewState["ShowFooterWhenEmpty"] = false;
}
return (bool)this.ViewState["ShowFooterWhenEmpty"];
}
set
{
this.ViewState["ShowFooterWhenEmpty"] = value;
}
}
#endregion
private GridViewRow _footerRow2;
public override GridViewRow FooterRow
{
get
{
GridViewRow f = base.FooterRow;
if (f != null)
return f;
else
return _footerRow2;
}
}
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
// no data rows created, create empty table if enabled
if (rows == 0 && (this.ShowFooterWhenEmpty))
{
// create the table
Table table = this.CreateChildTable();
DataControlField[] fields;
if (this.AutoGenerateColumns)
{
PagedDataSource source = new PagedDataSource();
source.DataSource = dataSource;
System.Collections.ICollection autoGeneratedColumns = this.CreateColumns(source, true);
fields = new DataControlField[autoGeneratedColumns.Count];
autoGeneratedColumns.CopyTo(fields, 0);
}
else
{
fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
}
if (this.ShowHeaderWhenEmpty)
{
// create a new header row
GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
this.InitializeRow(headerRow, fields);
// add the header row to the table
table.Rows.Add(headerRow);
}
// create the empty row
GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = fields.Length;
cell.Width = Unit.Percentage(100);
// respect the precedence order if both EmptyDataTemplate
// and EmptyDataText are both supplied ...
if (this.EmptyDataTemplate != null)
{
this.EmptyDataTemplate.InstantiateIn(cell);
}
else if (!string.IsNullOrEmpty(this.EmptyDataText))
{
cell.Controls.Add(new LiteralControl(EmptyDataText));
}
emptyRow.Cells.Add(cell);
table.Rows.Add(emptyRow);
if (this.ShowFooterWhenEmpty)
{
// create footer row
_footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
this.InitializeRow(_footerRow2, fields);
// add the footer to the table
table.Rows.Add(_footerRow2);
}
this.Controls.Clear();
this.Controls.Add(table);
}
return rows;
}
}
}
在ASPX页面,只需添加
<%@ Register TagPrefix="YourPrefix" Namespace="YourNamespace" %>
和替换<asp:GridView
与<YourPrefix:GridViewExtended
希望它能帮助别人。
作为前面提到的评论者之一,RowDataBound事件不会触发页脚。我发现了另一个代码片段addresses this issue,但除了显示页脚外,它还显式创建该行(触发RowCreated事件)并绑定它(触发RowDataBound事件)。
我已经使用代码转换器将上面引用的代码转换为c#并做了一些小的调整。我还包括了我在通过代码分解时所做的评论。 RowCreated和RowDataBound事件现在正在触发,我可以在页脚中填充下拉列表。
using System.Linq;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebUI.Controls
{
//modified from https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid
public class GridViewExtended : GridView
{
private GridViewRow _footerRow;
[DefaultValue(false), Category("Appearance"), Description("Include the footer when the table is empty")]
public bool ShowFooterWhenEmpty { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public override GridViewRow FooterRow {
get {
if ((this._footerRow == null)) {
this.EnsureChildControls();
}
return this._footerRow;
}
}
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
//creates all the rows that would normally be created when instantiating the grid
int returnVal = base.CreateChildControls(dataSource, dataBinding);
//if no rows were created (i.e. returnVal == 0), and we need to show the footer row, then we need to create and bind the footer row.
if (returnVal == 0 && this.ShowFooterWhenEmpty) {
Table table = this.Controls.OfType<Table>().First<Table>();
DataControlField[] dcf = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(dcf, 0);
//creates the footer row
this._footerRow = this.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, null, dcf, table.Rows, null);
if (!this.ShowFooter) {
_footerRow.Visible = false;
}
}
return returnVal;
}
private GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, bool dataBind, object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
{
GridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState);
GridViewRowEventArgs e = new GridViewRowEventArgs(row);
if ((rowType != DataControlRowType.Pager)) {
this.InitializeRow(row, fields);
} else {
this.InitializePager(row, fields.Length, pagedDataSource);
}
//if the row has data, sets the data item
if (dataBind) {
row.DataItem = dataItem;
}
//Raises the RowCreated event
this.OnRowCreated(e);
//adds the row to the gridview's row collection
rows.Add(row);
//explicitly binds the data item to the row, including the footer row and raises the RowDataBound event.
if (dataBind) {
row.DataBind();
this.OnRowDataBound(e);
row.DataItem = null;
}
return row;
}
}
}
- 1. 设计登录页面始终显示即使它没有根。
- 2. 即使没有数据显示日期
- 3. UISearch栏始终显示:没有结果
- 4. 即使数据库显示数据,PyMongo也没有返回
- 5. 即使listView中有元素,也会始终显示listView.SeptEmptyView
- 6. CrystalReport始终显示相同的数据
- 7. mysql_affected_rows()函数始终返回1,即使没有行已更新
- 8. 始终显示使用D3js
- 9. 即使没有数据也显示查询表
- 10. Datagrid即使其boudn也没有显示数据
- 11. 从URLSessionDownloadTask恢复数据始终没有
- 12. Android:ProgressDialog没有立即显示
- 13. 始终显示Highcharts
- 14. 显示LastDateOfMonth即使是没有价值
- 15. 显示,即使没有在SQL
- 16. 显示0,即使没有排
- 17. 即使没有人在听,JavaScript事件是否始终执行?
- 18. 即使没有任何更改,Makefile始终是最新的
- 19. JQGrid没有显示数据
- 20. Dropdownlist没有显示数据
- 21. TableView没有显示数据
- 22. 始终显示4个小数位数
- 23. Telerik RadGrid始终显示X行,即使最后一页上只有Y个数据行,其中Y <X
- 24. 数据库更新没有立即显示
- 25. NG2-图表始终显示没有在我的angular2 JS项目
- 26. 使用Javascript - 格式号码始终显示原始小数
- 27. .GetChanges始终显示为空
- 28. 平均始终显示3
- 29. UIActivityIndicatorView始终显示
- 30. FileUpload.HasFile始终显示为false
为什么你想这样做? – 2009-06-15 08:02:24
请看下面我评论的内容,我解释了我需要的内容。 – Shimmy 2009-06-15 13:34:39