12

Setting TableSection = TableRowSection.TableHeader after the grid is bound works initially, putting the header row in thead. After a postback (where the grid is not re-bound) the header row reverts to the table body. I expect the header row to stay in thead; can someone explain why this is not the case or what I am doing wrong?

Sample:

Click the button to cause a postback. The header is not orange after the postback since it's not in thead anymore.

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridtest.aspx.cs" Inherits="ffff.gridtest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style type="text/css">
    thead th { background-color:Orange;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div><asp:Button ID="Button1" runat="server" Text="Button" />
    &nbsp;this button is here just to trigger a postback</div>
    <asp:GridView ID="gv1" runat="server"></asp:GridView>
    </form>
</body>
</html>

code

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ffff
{
    public partial class gridtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gv1.DataBound += new EventHandler(gv1_DataBound);
            if (!IsPostBack) { BindGrid(); }
        }
        void Page_PreRender(object sender, EventArgs e)
        {
            if (gv1.HeaderRow != null)
                System.Diagnostics.Debug.WriteLine(gv1.HeaderRow.TableSection); // still TableHeader after postback
        }
        void gv1_DataBound(object sender, EventArgs e)
        {
            if (gv1.HeaderRow != null)
            {
                gv1.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
        }
        private void BindGrid()
        {
            gv1.DataSource = this.Page.Controls;
            gv1.DataBind();
        }
    }
}
1

3 Answers 3

19

Use the Pre_Render_Complete event instead to add the table section. This will ensure the thead section is always added. As you are currently doing it on DataBound the section will only be added when the data is bound.

protected override void OnPreRenderComplete(EventArgs e)
    {
        if (gv1.Rows.Count > 0)
        {
            gv1.HeaderRow.TableSection = TableRowSection.TableHeader;                
        }
    }

Feel free to change the row check I use to checking the Header Row exists as you currently do.

5
  • I'm using a standard GridView rather than inheriting from it so I attached an event handler to PreRender and this works. I don't understand why though- gv1.HeaderRow.TableSection has a value of TableHeader before and after this assignment. Is there something going on within the setter that's responsible for making this render correctly?
    – lincolnk
    Jul 8, 2011 at 14:24
  • Unfortunately I cant give you an answer to why this is the case, only that it works. My OnPrerenderComplete overide is actually at a page level. Often if I have several grids on a page I will load up the all table head sections in one spot. I might have a little investigation/experiment into the mechanics of this in light of your follow up question
    – Jon P
    Jul 10, 2011 at 23:44
  • Interesting. When Stepping through my page OnPreRenderComplete shows HeaderRow.TableSection as TableBody before the assignment, ensuring ViewState was enabled. ViewState is saved after the PreRenderComplete event (msdn.microsoft.com/en-us/library/ms178472.aspx) so that isn't the issue.
    – Jon P
    Jul 11, 2011 at 0:48
  • 2
    I needed to use this in a Usercontrol, so I used the OnPreRender event and it worked fine. Feb 22, 2012 at 18:57
  • Thank you. It still threw exceptions for me, so I expanded your approach to fit my needs. See my answer here: stackoverflow.com/a/19846059/555798
    – MikeTeeVee
    Nov 7, 2013 at 20:35
5

You must set TableSection after DataBind method.

 gv1.DataSource = this.Page.Controls;
 gv1.DataBind();
 gv1.HeaderRow.TableSection = TableRowSection.TableHeader; 
2
  • 1
    Yes, I'm doing that. The question is why does the grid render the default way after a postback, even when TableSection is still set as TableHeader.
    – lincolnk
    Sep 21, 2013 at 0:07
  • From forums.asp.net/t/1004097.aspx?GridView+HeaderRow+null+ - HeaderRow is null when your datatable is empty (or, in this case, this.Page.Controls is empty)
    – jflaga
    Jan 27, 2015 at 13:03
3

Just put the below code in prerender event of gridview

protected void gv_PreRender(object sender, EventArgs e)
{
    if (gv.Rows.Count > 0)
    {
        gv.UseAccessibleHeader = true;
        gv.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.