C#
protected void Page_Load(object sender, EventArgs e)
{
// adds scope attribute
GridView1.UseAccessibleHeader = true;
//adds <thead> and <tbody> elements
GridView1.HeaderRow.TableSection =
TableRowSection.TableHeader;
GridView1.HeaderRow.CssClass = "someclass";
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
' adds scope attribute
GridView1.UseAccessibleHeader = True
'adds <thead> and <tbody> elements
GridView1.HeaderRow.TableSection = _
TableRowSection.TableHeader
GridView1.HeaderRow.CssClass = "someclass"
End Sub
it's not working in ie 7.
ReplyDeleteTo fix it you need to add your code in Pre_Render:
protected void GridView1_PreRender(object sender, EventArgs e)
{
GridView1.UseAccessibleHeader = false;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
I got it working in IE7 and firefox also with the tip, but prerender is good place to write this.
ReplyDeleteSamuel
Arnold: I agree to both of you that adding the code in Pre_Render() event does good.
ReplyDeleteHowever this code works absolutely fine in IE7 even if it is in the Page_Load(). I checked it again to confirm the same!
After running the code in IE7, here is the markup that got generated with the thead and tbody tags
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<thead>
<tr>
<th scope="col"><a href="javascript:__doPostBack('GridView1','Sort$CustomerID')">CustomerID</a></th><th scope="col"><a href="javascript:__doPostBack('GridView1','Sort$CompanyName')">CompanyName</a></th><th scope="col"><a href="javascript:__doPostBack('GridView1','Sort$ContactName')">ContactName</a></th><th scope="col"><a href="javascript:__doPostBack('GridView1','Sort$Address')">Address</a></th><th scope="col"><a href="javascript:__doPostBack('GridView1','Sort$City')">City</a></th>
</tr>
</thead><tbody>
<tr>
<td>ALFKI</td><td>Alfreds Futterkiste</td><td>Maria Anders</td><td>Obere Str. 57</td><td>Berlin</td>
</tr><tr>
Putting it just in the Page_Load event is not enough. Sometimes the GridView is not populated at that time. Adding it to the PreRender event will take care of this case.
ReplyDeleteAlso, if you allow sorting of the GridView, the thead and tbody tags will no longer be present upon postback. Again, adding the logic to the PreRender event will take care of it.
If you put it in Page_Load, it will work for the most part, but you'll have trouble when sorting or data binding the GridView after Page_Load. If you put it in PreRender, it will work without issue.
To be explicit, you should put the logic in the GridView_PreRender event.
ReplyDelete