GridView added rows non-persistent

Discussion in 'ASP .Net' started by Flinky Wisty Pomm, Apr 28, 2006.

  1. Hi all, I've got a really annoying problem that I need to fix sharpish.

    I've got a GridView derived control which has a templated header and
    footer. It works wonderfully on the first render but then the
    header/footer vanish into thin air. If I add the header/footer
    onDataBound then they disappear when I do a postback with no
    databinding. If I add them during PreRender, then they persist, but one
    of my data rows is emptied for each time I postback.

    It feels like there's a rowIndex being kicked out of place, but I've
    run reflector over CreateChildControls and given up all hope of
    understanding it in the next week. Can anyone offer some help?

    test code is below, apologies if it's not quite cut/paste ready but
    it's lifted and munged from an active system. ASPX might need a quick
    wrangle, but it looks good to my untrained eye.

    cheers,

    Flinky Wisty Pomm

    --- code--

    //
    // this code is lifted from TwoHeadedGridView by Matt Dotson,
    // license and other code is available @
    //
    http://www.gotdotnet.com/workspaces....aspx?id=1ecb6d2e-a104-48f7-a8fb-1622eba20852
    // I'm using this because it's a simpler form of my grid and has the
    same problem

    //#################################
    // TwoHeadedGridView.cs
    //#################################
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Drawing;

    namespace RealWorld.Grids
    {
    public class TwoHeadedGridView : GridView
    {
    public string HeaderText
    {
    get
    {
    object val = this.ViewState["HeaderText"];
    if (null == val)
    {
    return String.Empty;
    }

    return (String)val;
    }
    set
    {
    this.ViewState["HeaderText"] = value;
    }
    }

    protected Table InnerTable
    {
    get
    {
    if (this.HasControls())
    {
    return (Table)this.Controls[0];
    }

    return null;
    }
    }

    protected override void OnDataBound(EventArgs e)
    {
    base.OnDataBound(e);
    this.CreateSecondHeader();
    }

    private void CreateSecondHeader()
    {
    GridViewRow row = new GridViewRow(0, -1,
    DataControlRowType.Header, DataControlRowState.Normal);
    TableCell th = new TableHeaderCell();

    th.HorizontalAlign = HorizontalAlign.Center;
    th.ColumnSpan = this.Columns.Count;
    th.BackColor = Color.SteelBlue;
    th.ForeColor = Color.White;
    th.Font.Bold = true;
    th.Text = this.HeaderText;
    row.Cells.Add(th);

    this.InnerTable.Rows.AddAt(0, row);
    }
    }
    }



    // #####################################
    // GridTest.cs
    // #####################################

    using System;
    using System.Collections.Generic;
    using System.Web.UI;

    using RealWorld.Grids;

    namespace Pages
    {
    public class GridTest : Page
    {
    protected TwoHeadedGridView testgrid;

    protected void Page_Load()
    {
    if(!IsPostBack)
    BindGrid();
    }

    private void BindGrid()
    {
    Dictionary<string,string> data = new
    Dictionary<string,string>(8);

    data.Add("123","abc");
    data.Add("fred","fredder");
    data.Add("bing","bong");
    data.Add("foo","bar");
    data.Add("baz","quux");
    data.Add("oogle","fooble");
    data.Add("boogle","zork");
    data.Add("quuux","quuuux");
    data.Add("spam","eggs");

    testgrid.DataSource = data;
    testgrid.DataBind();
    }

    protected void BindButtonClicked(object sender, EventArgs e)
    {
    BindGrid();
    }

    }
    }


    // #########################
    // GridTest.aspx
    // #########################
    <%@ Page language="c#" EnableViewState="true" Inherits="Pages.GridTest"
    MasterPageFile="~/MasterPages/Agenda.Master"%>
    <%@ Register TagPrefix="RealGrid" Namespace="RealWorld.Grids"
    Assembly="Cogentic.DSMSWeb.Core.Controls" %>

    <html>
    <head></head>
    <body>
    <form runat="server">

    <RealGrid:TwoHeadedGridView runat="server" id="testgrid"
    HeaderText="Fred" />

    <asp:Button id="btnRefresh" Text="Postback, no databind"
    runat="server" />
    <asp:Button id="btnBind" Text="Postback, rebind"
    onclick="BindButtonClicked" runat="server" />

    </form>
    </body>
    </html>
    Flinky Wisty Pomm, Apr 28, 2006
    #1
    1. Advertising

  2. Flinky Wisty Pomm

    Teemu Keiski Guest

    Hi,

    perhaps you could utilize same idea that I've used in one of my GridView
    samples as I add grouping rows into the GridView (when GridView renders).
    Same idea applies, you are just adding the first and last row where I am
    adding the grouping headers

    GridView Sort Grouping
    http://aspadvice.com/blogs/joteke/archive/2006/02/11/15130.aspx

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "Flinky Wisty Pomm" <> wrote in message
    news:...
    > Hi all, I've got a really annoying problem that I need to fix sharpish.
    >
    > I've got a GridView derived control which has a templated header and
    > footer. It works wonderfully on the first render but then the
    > header/footer vanish into thin air. If I add the header/footer
    > onDataBound then they disappear when I do a postback with no
    > databinding. If I add them during PreRender, then they persist, but one
    > of my data rows is emptied for each time I postback.
    >
    > It feels like there's a rowIndex being kicked out of place, but I've
    > run reflector over CreateChildControls and given up all hope of
    > understanding it in the next week. Can anyone offer some help?
    >
    > test code is below, apologies if it's not quite cut/paste ready but
    > it's lifted and munged from an active system. ASPX might need a quick
    > wrangle, but it looks good to my untrained eye.
    >
    > cheers,
    >
    > Flinky Wisty Pomm
    >
    > --- code--
    >
    > //
    > // this code is lifted from TwoHeadedGridView by Matt Dotson,
    > // license and other code is available @
    > //
    > http://www.gotdotnet.com/workspaces....aspx?id=1ecb6d2e-a104-48f7-a8fb-1622eba20852
    > // I'm using this because it's a simpler form of my grid and has the
    > same problem
    >
    > //#################################
    > // TwoHeadedGridView.cs
    > //#################################
    > using System;
    > using System.Collections.Generic;
    > using System.Text;
    > using System.Web.UI.WebControls;
    > using System.Drawing;
    >
    > namespace RealWorld.Grids
    > {
    > public class TwoHeadedGridView : GridView
    > {
    > public string HeaderText
    > {
    > get
    > {
    > object val = this.ViewState["HeaderText"];
    > if (null == val)
    > {
    > return String.Empty;
    > }
    >
    > return (String)val;
    > }
    > set
    > {
    > this.ViewState["HeaderText"] = value;
    > }
    > }
    >
    > protected Table InnerTable
    > {
    > get
    > {
    > if (this.HasControls())
    > {
    > return (Table)this.Controls[0];
    > }
    >
    > return null;
    > }
    > }
    >
    > protected override void OnDataBound(EventArgs e)
    > {
    > base.OnDataBound(e);
    > this.CreateSecondHeader();
    > }
    >
    > private void CreateSecondHeader()
    > {
    > GridViewRow row = new GridViewRow(0, -1,
    > DataControlRowType.Header, DataControlRowState.Normal);
    > TableCell th = new TableHeaderCell();
    >
    > th.HorizontalAlign = HorizontalAlign.Center;
    > th.ColumnSpan = this.Columns.Count;
    > th.BackColor = Color.SteelBlue;
    > th.ForeColor = Color.White;
    > th.Font.Bold = true;
    > th.Text = this.HeaderText;
    > row.Cells.Add(th);
    >
    > this.InnerTable.Rows.AddAt(0, row);
    > }
    > }
    > }
    >
    >
    >
    > // #####################################
    > // GridTest.cs
    > // #####################################
    >
    > using System;
    > using System.Collections.Generic;
    > using System.Web.UI;
    >
    > using RealWorld.Grids;
    >
    > namespace Pages
    > {
    > public class GridTest : Page
    > {
    > protected TwoHeadedGridView testgrid;
    >
    > protected void Page_Load()
    > {
    > if(!IsPostBack)
    > BindGrid();
    > }
    >
    > private void BindGrid()
    > {
    > Dictionary<string,string> data = new
    > Dictionary<string,string>(8);
    >
    > data.Add("123","abc");
    > data.Add("fred","fredder");
    > data.Add("bing","bong");
    > data.Add("foo","bar");
    > data.Add("baz","quux");
    > data.Add("oogle","fooble");
    > data.Add("boogle","zork");
    > data.Add("quuux","quuuux");
    > data.Add("spam","eggs");
    >
    > testgrid.DataSource = data;
    > testgrid.DataBind();
    > }
    >
    > protected void BindButtonClicked(object sender, EventArgs e)
    > {
    > BindGrid();
    > }
    >
    > }
    > }
    >
    >
    > // #########################
    > // GridTest.aspx
    > // #########################
    > <%@ Page language="c#" EnableViewState="true" Inherits="Pages.GridTest"
    > MasterPageFile="~/MasterPages/Agenda.Master"%>
    > <%@ Register TagPrefix="RealGrid" Namespace="RealWorld.Grids"
    > Assembly="Cogentic.DSMSWeb.Core.Controls" %>
    >
    > <html>
    > <head></head>
    > <body>
    > <form runat="server">
    >
    > <RealGrid:TwoHeadedGridView runat="server" id="testgrid"
    > HeaderText="Fred" />
    >
    > <asp:Button id="btnRefresh" Text="Postback, no databind"
    > runat="server" />
    > <asp:Button id="btnBind" Text="Postback, rebind"
    > onclick="BindButtonClicked" runat="server" />
    >
    > </form>
    > </body>
    > </html>
    >
    Teemu Keiski, Apr 29, 2006
    #2
    1. Advertising

  3. Okay, I can add rows when the table renders, and obviously that works -
    but I've got an ITemplate for my header/footer so I can add buttons,
    links etc.

    These need to be added during init or I can't fire server side events
    from them. All the methods I want to get hold of in order to do that
    are private - so I think I'm back to hunting for workarounds.

    I can add the controls directly to the controlcollection of the
    gridview and they appear in the right places, but they disappear
    again... GAH! I'm beginning to think this is impossible.
    Flinky Wisty Pomm, Apr 29, 2006
    #3
    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. Ryan

    Non persistent page cache

    Ryan, Jan 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    Paul Glavich
    Jan 21, 2004
  2. Bennett Haselton
    Replies:
    1
    Views:
    514
    John M Deal
    Nov 8, 2004
  3. Bobby Edward
    Replies:
    1
    Views:
    4,547
  4. VijayRama
    Replies:
    2
    Views:
    2,322
    Gregory A. Beamer
    Oct 12, 2009
  5. gk
    Replies:
    7
    Views:
    967
    Tom Anderson
    Oct 12, 2010
Loading...

Share This Page