[英]Gridview find control in dynamic header row
这可能与页面的生命周期有关,但是即使在找到有关该主题的所有帖子之后,也似乎无法使其正常运行。
在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。 我在这里没看到什么?
谢谢!
标题行由GridView控件根据您的数据源生成。 它实际上不是数据集的成员,而是根据数据集的内容动态构建的。
考虑以下代码:
int HeaderRowCount = -5; // initialized to a wrong starting point
protected void GridView1_DataBinding( object sender, EventArgs e ) {
HeaderRowCount = 0; // this event starts the binding and row creation process
}
// Each row is created and Bound in the order of the dataset.
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e ) {
// Are we creating the Header Row?
if ( e.Row.RowType == DataControlRowType.Header ) {
// Create your additional Headers here: AddHeaderRow() is defined below
AddHeaderRow( ( GridView ) sender, "Hi I'm a header" );
}
}
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.Header ) {
HeaderRowCount++;
}
}
protected void GridView1_DataBound( object sender, EventArgs e ) {
GridView1.Caption = string.Format( "HeaderRowCount: {0}", HeaderRowCount);
}
绑定完成后,您将在GridView标题中看到以下内容:
HeaderRowCount:1
标头行数将始终为1,因为其他标头行不属于绑定过程的一部分。 GridView行集合仅包含属于数据集的行。 GridViews动态生成HeaderRow
(和FooterRow
)。
任何其他标题行仅可通过内部表行集合提供,因此您需要在每个表行中搜索RowType == Header
:
Table InnerTable = ( Table ) GridView1.Controls[ 0 ];
foreach ( GridViewRow r in InnerTable.Rows ) {
if (r.RowType == DataControlRowType.Header){
CheckBox chk = (CheckBox) r.FindControl( "chk" );
}
}
或者,您可以根据需要直接访问行:
GridViewRow r = ( GridViewRow ) InnerTable.Rows[0];
CheckBox chk = (CheckBox) r.FindControl( "chk" );
private void AddHeaderRow( GridView gv, string HeaderText ) {
Table InnerTable = ( Table ) gv.Controls[ 0 ];
GridViewRow row = new GridViewRow( 0, -1, DataControlRowType.Header, DataControlRowState.Normal );
TableCell th = new TableHeaderCell();
th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = gv.Columns.Count;
th.Font.Bold = true;
th.Text = HeaderText;
row.Cells.Add( th );
InnerTable.Rows.AddAt( 0, row );
}
要找到控件,请尝试以下操作:-
foreach (Control c in gvNotes.Controls[0].Controls[0].Controls)
{
CheckBox chk = (CheckBox)c.FindControl("chk");
}
gvNotes.Controls [0] .Controls-> System.Web.UI.WebControls.Table。 RowControlCollection
gvNotes.Controls [0] .Controls [0] .Controls-> System.Web.UI.WebControls.TableRow。 CellControlCollection
首先添加TableRow,然后添加TableCell
希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.