【问题标题】:Gridview find control in dynamic header rowGridview 在动态标题行中查找控件
【发布时间】: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。修复了代码以反映这一点。

标签: c# asp.net gridview


【解决方案1】:

标题行由 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 Caption 中看到:

HeaderRowCount: 1

标题行计数将始终为 1,因为附加标题行不是绑定过程的一部分。 GridView Rows 集合仅包含属于 DataSet 的行。 GridViews 动态生成HeaderRow(和FooterRow)。

任何额外的 Header Rows 只能通过内部 Table 行集合使用,因此您需要在每个 Table Row 中搜索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 );
    }

【讨论】:

    【解决方案2】:

    要找到控件试试这个:-

    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

    这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多