Why does GridView not render the header row as thead after postback?
Asked Answered
A

3

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();
        }
    }
}
Aftertaste answered 8/7, 2011 at 1:18 Comment(1)
possible duplicate of How do I get Gridview to render THEAD?Pegeen
R
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.

Robertson answered 8/7, 2011 at 1:41 Comment(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?Aftertaste
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 questionRobertson
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.Robertson
I needed to use this in a Usercontrol, so I used the OnPreRender event and it worked fine.Orderly
Thank you. It still threw exceptions for me, so I expanded your approach to fit my needs. See my answer here: https://mcmap.net/q/181111/-how-do-i-get-gridview-to-render-theadOutbuilding
T
5

You must set TableSection after DataBind method.

 gv1.DataSource = this.Page.Controls;
 gv1.DataBind();
 gv1.HeaderRow.TableSection = TableRowSection.TableHeader; 
Tishtisha answered 21/12, 2012 at 5:50 Comment(2)
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.Aftertaste
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)Purpleness
N
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;
    }
}
Nathanialnathaniel answered 7/5, 2014 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.