F
Flinky Wisty Pomm
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>
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>