【发布时间】:2016-02-04 07:06:24
【问题描述】:
我有一个可以按日期搜索的网格视图。 在gridview中,如果日期的gridview为空,我手动添加页脚。 它工作正常,然后首先加载页面。
问题是: 当使用数据加载 gridview 时,页脚显示默认数据。之后,选择没有数据的日期,默认值丢失。如果我选择另一个没有数据的日期,页脚的默认值会再次出现。
我放了断点,它通过了下面的函数,值不为空,但它没有显示在页脚的标签上。
while (Dr.Read())
{
AttendbyFooter.Text = Dr.GetValue(1).ToString();
DepartmentFooter.Text = Dr.GetValue(4).ToString();
}
Gridview 数据绑定:
Label AttendbyFooter = GridView1.FooterRow.FindControl("AttendbyFooter") as Label;
//if the footer is null, do nothing. if the footer is not null, continue for other value
if (AttendbyFooter == null)
{
}
else
{
//set default name
AttendbyFooter.Attributes.Add("readonly", "readonly");
Label DepartmentFooter = GridView1.FooterRow.FindControl("DepartmentFooter") as Label;
DepartmentFooter.Attributes.Add("readonly", "readonly");
//create a connection to mssql server
var Cn = new System.Data.SqlClient.SqlConnection();
Cn.ConnectionString = ConfigurationManager.ConnectionStrings["CMOSConnectionString"].ConnectionString;
Cn.Open();
//create a command object from the connection
var Cm = Cn.CreateCommand();
//Check database using username(id number)
Cm.CommandText = string.Format(@"Select * From Employee WHERE PassNumber='{0}'", User.Identity.Name);
var Dr = Cm.ExecuteReader();
while (Dr.Read())
{
//Name
AttendbyFooter.Text = Dr.GetValue(1).ToString();
DepartmentFooter.Text = Dr.GetValue(4).ToString();
}
Cn.Close();
}
生成页脚(从网站复制)
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;
}
}
}
【问题讨论】:
-
你的意思是说gridview中没有数据要填写的时候,值不清除?
-
抱歉造成混淆,第一张图片是预期的行为,其中包含数据库中的姓名和部门。不知何故,第二个发生在回发之后。
-
if (!IsPostBack) { Date.Attributes.Add("readonly", "readonly"); Date.Text = DateTime.Now.ToString("dd MMM yyyy"); } -
这是唯一的页面加载...
-
那么你的网格填充方法在哪里加载呢?