GridView override InitializePager columnSpan problem when using BoundField Visible=false

Discussion in 'ASP .Net Web Controls' started by J055, Jun 8, 2006.

  1. J055

    J055 Guest

    Hi

    I have an overridden InitializePager method in my custom GridView. I use it
    to add some page information to the pager row by splitting the table cell.
    This works fine until I add either a BoundField or TemplateField where
    visible is set to false.

    This causes the two Table Cells of the pager row to have incorrect
    columnSpans, e.g. where I have 6 columns and one invisible column (total 7)
    the pager row will have a left cell with colspan="6" and a right cell with
    colspan="4". If I change then visible attribute to true I get left cell with
    colspan="3" and a right cell with colspan="4" which is correct.

    I have know idea why this happens. When debugging I have found the
    InitializePager method is called with columnSpan parameter set to the number
    of columns including the invisible one (7).

    Please help.
    Thanks
    Andrew

    protected override void InitializePager(GridViewRow row, int columnSpan,
    PagedDataSource pagedDataSource)

    {

    // call the base method first

    base.InitializePager(row, columnSpan, pagedDataSource);

    // create a new tablecell to contain the new page information

    TableCell cell1 = new TableCell();

    // divide the pager row in half

    int ltSpan = (int)(columnSpan / 2);

    int rtSpan = columnSpan - ltSpan;

    // add the new label control with the new page info

    cell1.Controls.Add(PageInfo(pagedDataSource.DataSourceCount));

    // add the new cell to the pager row and apply the ColumnSpan sizes

    row.Controls.AddAt(0, cell1);

    row.Cells[0].ColumnSpan = ltSpan;

    row.Cells[1].ColumnSpan = rtSpan;

    // pager buttons are now the right and page info on the left

    row.Cells[1].HorizontalAlign = HorizontalAlign.Right;

    }
    J055, Jun 8, 2006
    #1
    1. Advertising

  2. J055

    J055 Guest

    Hi

    I've taken a different approach to get over the previous problem, however I
    now get two new ones. This is the code:

    protected override void OnRowCreated(GridViewRowEventArgs e)

    {

    switch (e.Row.RowType)

    {

    case DataControlRowType.Header:

    if (SortExpression != String.Empty)

    DisplaySortOrderImages(SortExpression, e.Row);

    break;

    case DataControlRowType.Pager:

    Context.Trace.Warn("PagerRow");

    if (DisplayPageInfo)

    {

    Table tbl = new Table();

    tbl.CellSpacing = 0;

    tbl.Width = Unit.Percentage(100);

    tbl.Rows.Add(new TableRow());

    tbl.Rows[0].Cells.Add(new TableCell());

    tbl.Rows[0].Cells.Add(new TableCell());

    tbl.Rows[0].Cells[0].Controls.Add(PageInfo(dataSourceRowCount));

    tbl.Rows[0].Cells[1].HorizontalAlign = HorizontalAlign.Right;



    foreach (Control ctrl in e.Row.Cells[0].Controls)

    {

    tbl.Rows[0].Cells[1].Controls.Add(ctrl);

    }

    e.Row.Cells[0].Controls.Add(tbl);

    }

    break;

    default:

    break;

    }

    base.OnRowCreated(e);

    }


    1. The new table does not get created when there is only one page, i.e. the
    pager controls are not present. I still want to display the new table and
    the contents of tbl.Rows[0].Cells[0].

    2. The VS2005 IDE complains in page design mode - 'There was an error
    rendering the control. Object reference not set to an instance of an
    object'. However the code runs OK in the browser except for (1).

    Even if the above code worked properly I rather have the code in my first
    post work to avoid so many nested tables (It's not really how modern web
    pages should be designed!).

    Please help!
    Andrew
    J055, Jun 11, 2006
    #2
    1. Advertising

  3. J055

    J055 Guest

    Hello

    I've found what was causing the VS IDE to complain. It doesn't like me
    writing out

    Context.Trace.Warn("debug lines")
    in my custom control OnRowCreated method. How would I know that? I still
    need help with the other questions raised in my previous post.

    Thanks

    Andrew
    J055, Jun 12, 2006
    #3
  4. Hello Andrew,

    Thank you for posting.

    It seems that you're still struggling with the custom Gridview with custom
    paging functionality. As for your 3 questions, I've just done some research
    and struggle against the GridView. here are some of the results I got:

    1.This does be an existing issue, and based on my reseach upon gridview's
    code, when there is only one page needed, the GridView's certain PagerRow
    will be set to Visible=False, we can consider set it to true in our
    override "CreateChildControls" method. e.g.

    ========================
    protected override void CreateChildControls()
    {
    base.CreateChildControls();

    if(!this.DesignMode)
    Context.Response.Write("<br/>Count: " + this.PageCount);

    if (this.PageCount == 1)
    {

    if ((this.PagerSettings.Visible) &&
    (this.PagerSettings.Position == PagerPosition.Bottom))
    {

    BottomPagerRow.Visible = true;

    }

    }


    }
    ==============================

    2. As for the context null reference issue, it is because when in IDE, the
    HttpContext is not available, the control is hosted in VS IDE(designer
    host). We can simply add code to determine the current context such as

    if(Context != null)
    {

    }

    or
    if(!this.DesignMode)
    {

    }

    3. As for the further question you mentioned:

    ================
    Even if the above code worked properly I rather have the code in my first
    post work to avoid so many nested tables (It's not really how modern web
    pages should be designed!).
    ===============

    I'm still not quite clear on this, do you mean that the pager need to be
    created each time the control is loaded(in each page request)? And you want
    to only create it once?

    Please feel free to let me know if there is anything I didn't get.


    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Jun 12, 2006
    #4
  5. Hi Andrew,

    Have you got any further ideas on this issue or does my last reply helps
    you a little on this? If there is anything else we can help, please feel
    free to post here.

    Regards,

    Steven Cheng
    Microsoft MSDN Online Support Lead


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Jun 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ajaymehra

    Columnspan in Datagrid ?

    ajaymehra, Feb 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,641
    Alvin Bruney [MVP]
    Feb 6, 2004
  2. jobs
    Replies:
    1
    Views:
    741
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Feb 10, 2007
  3. Andrew Robinson

    GridView overriding InitializePager

    Andrew Robinson, Apr 10, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    2,161
    Steven Cheng[MSFT]
    Apr 13, 2007
  4. Bernal Schooley

    Problem with OnItemCreated and InitializePager being called four times

    Bernal Schooley, Apr 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    175
    Bernal Schooley
    Apr 5, 2004
  5. Vyacheslav Lanovets

    BoundField.HtmlEncode = false does not work

    Vyacheslav Lanovets, Aug 24, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    607
    Vyacheslav Lanovets
    Aug 24, 2006
Loading...

Share This Page