【发布时间】:2016-05-14 02:27:02
【问题描述】:
这可能与页面生命周期有关,但似乎无法使其正常工作,即使在找到有关该主题的所有帖子之后也是如此。
在 gridview 中,使用控件创建新的标题行:
protected void gvNotes_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
CheckBox chk = new CheckBox();
chk.Text = "Show Admin Columns";
chk.ID = "chk";
chk.AutoPostBack = true;
chk.CheckedChanged += new EventHandler(this.chkShowAminColumns_CheckedChanged);
HeaderCell.Controls.Add(chk);
HeaderCell.ColumnSpan = gvNotes.Columns.Count;
HeaderGridRow.Cells.Add(HeaderCell);
gvNotes.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
试图找到这个新控件给我带来了一些麻烦:
public void bindNotesGrid()
{
DataTable dt = BLL.NotesBLL.GetNotes();
gvNotes.DataSource = dt;
gvNotes.DataBind();
if (dt.Rows.Count > 0)
{
//never finds the control
foreach (Control c in gvNotes.HeaderRow.Controls)
{
if (c is CheckBox)
{
string value = ((CheckBox)c).Text;
}
}
//never finds the control
//int current = 0;
//int headerCount = gvNotes.HeaderRow.Cells.Count;
//for (current = 0; current < headerCount; current++)
//{
// CheckBox chk2 = (CheckBox)gvNotes.HeaderRow.Cells[current].FindControl("chk");
//}
//returns null
CheckBox chk = (CheckBox)gvNotes.HeaderRow.FindControl("chk");
}
}
所有 findcontrol 尝试都将复选框返回为 null。 我在这里没有看到什么?
谢谢!
【问题讨论】:
-
首先 - 确保您使用的是同一个表,当您将控件添加到
gvNotes时,为什么还要查看gvCollapseNotes?其次,您要添加第二个标题行,但在这两种情况下,您只查看 1 个标题行,而不是两者 -
另外,你绑定的是
gvCollapseNotes而不是gvNotes -
这是 gvNotes。修复了代码以反映这一点。