Gridview在动态标题行中查找控件

时间:2016-05-13 15:39:46

标签: c# asp.net gridview

这可能与页面生命周期有关,但即使找到关于该主题的每篇帖子,也似乎无法使其正常工作。

在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。 我在这里看不到什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

标题行由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 Rows集合仅包含属于DataSet的行。 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" );

AddHeaderRow()

    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 );
    }

答案 1 :(得分:0)

要找到控件,请尝试: -

foreach (Control c in gvNotes.Controls[0].Controls[0].Controls)
        {
            CheckBox chk = (CheckBox)c.FindControl("chk");
        }
  

gvNotes.Controls [0] .Controls - &gt; System.Web.UI.WebControls.Table。的 RowControlCollection

     

gvNotes.Controls [0] .Controls [0] .Controls - &gt; System.Web.UI.WebControls.TableRow。的 CellControlCollection

首先添加了TableRow,然后添加了TableCell

跳这将有帮助