用 gridview 实现插入功能时遇到的问题
在网上看了一篇文章:没有数据时如何显示header和footer:
public class QGridView : GridView
{
#region Properties
/// <summary>
/// Enable or Disable generating an empty table if no data rows in source
/// </summary>
[
Description("Enable or disable generating an empty table with headers if no data rows in source"),
Category("Misc"),
DefaultValue("true"),
]
public bool ShowEmptyTable
{
get
{
object o = ViewState["ShowEmptyTable"];
return (o != null ? (bool)o : true);
}
set
{
ViewState["ShowEmptyTable"] = value;
}
}
/// <summary>
/// Get or Set Text to display in empty data row
/// </summary>
[
Description("Text to display in empty data row"),
Category("Misc"),
DefaultValue(""),
]
public string EmptyTableRowText
{
get
{
object o = ViewState["EmptyTableRowText"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["EmptyTableRowText"] = value;
}
}
#endregion
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.ShowEmptyTable))
{
// 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.ShowEmptyTable)
{
// 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
GridViewRow footerRow = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
this.InitializeRow(footerRow, fields);
// add the footer to the table
table.Rows.Add(footerRow);
//}
this.Controls.Clear();
this.Controls.Add(table);
this.ShowFooter = true;
}
return rows;
}
}
功能实现了,没有数据的时候header和footer都能显示,我把插入数据要输入的项目都放在footer里面,所以没有数据的时候,始终会显示一条插入纪录,但当我提交以后,gridView.FooterRow.FindControl("tb_foot_name") 返回空值。但如果表格里边本身有数据的话 gridView.FooterRow.FindControl("tb_foot_name")能够返回对象,区别是:
但没有数据时,footer是我自己创建的,有数据时footer是系统创建的。
请问大家,有何良策
谢谢