如何获取GridView
控件以呈现<thead>
<tbody>
标签?我知道.UseAccessibleHeaders
使它把<th>
而不是<td>
,但我不能让<thead>
出现。如何让Gridview呈现THEAD?
回答
这应做到:
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
'HeaderRow`属性将是`null`直到`GridView`被数据绑定,所以一定要等到数据绑定发生之后再运行上面的代码。 – bdukes 2009-07-17 14:47:07
至于下面的评论,至少在绑定ASP.NET 4.5后还不够晚 - 但它在OnPreRender中可行。 – philw 2013-08-06 11:16:57
我有一个添加了自定义子标题的gridview。这些子标题中的每一个都显示来自数据源的数据。我想渲染`thead`的原因是在jQuery中使用它。但是在渲染标题之后,`tbody`似乎不可用。我的情况可能会丢失什么? – bonCodigo 2014-09-03 08:54:44
的答案代码需要去Page_Load
或GridView_PreRender
。我在Page_Load
之后调用了一个方法,得到了一个NullReferenceException
。
创建一个函数,并使用该功能在您PageLoad
事件是这样的:
功能是:
private void MakeGridViewPrinterFriendly(GridView gridView) {
if (gridView.Rows.Count > 0) {
gridView.UseAccessibleHeader = true;
gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
的PageLoad
事件是:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
MakeGridViewPrinterFriendly(grddata);
}
}
我使用下面的代码要做到这一点:
我添加的陈述很重要。
否则(取决于你如何使你的网格),你会抛出异常一样:
表必须包含行区域的标题,正文的顺序,然后页脚。
protected override void OnPreRender(EventArgs e)
{
if ((this.ShowHeader == true && this.Rows.Count > 0)
|| (this.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
this.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (this.ShowFooter == true && this.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
this.FooterRow.TableSection = TableRowSection.TableFooter;
}
base.OnPreRender(e);
}
的this
对象是我的GridView控件。
我实际上覆盖了Asp.net GridView来创建我自己的自定义控件,但是您可以将其粘贴到您的页面中,并通过名称引用GridView而不是使用custom-gridview方法。
仅供参考:我尚未测试页脚逻辑,但我确实知道这适用于标题。
这个工作对我来说:
protected void GrdPagosRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.TableSection = TableRowSection.TableBody;
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.TableSection = TableRowSection.TableHeader;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.TableSection = TableRowSection.TableFooter;
}
}
这是在VS2010尝试。
我OnRowDataBound
事件中使用这样的:
if (e.Row.RowType == DataControlRowType.Header)
e.Row.TableSection = TableRowSection.TableHeader;
我知道这是旧的,但是,这里的MikeTeeVee的答案的解释,对于一个标准的GridView:
aspx页面:
<asp:GridView ID="GridView1" runat="server"
OnPreRender="GridView_PreRender">
aspx.cs:
protected void GridView_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if ((gv.ShowHeader == true && gv.Rows.Count > 0)
|| (gv.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (gv.ShowFooter == true && gv.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
gv.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
- 1. gridview呈现在div
- 2. 如何让CKEditor用CSS呈现文字?
- 3. 如何让FsLab表格呈现很好?
- 4. 如何让div在图像上呈现?
- 5. 如何让json数据反应呈现?
- 6. GridView thead,tbody,tfoot渲染命令
- 7. 更改gridview渲染thead元素
- 8. gridview中的图像不会呈现
- 9. 如何让gridview不对称?
- 10. 如何让gridview可搜索?
- 11. 如何呈现OpenStreetMaps?
- 12. 如何呈现bottlepy
- 13. 如何强制asp.net GridView控件包装第一行<thead></thead>标签
- 14. 如何安排THEAD内容
- 15. 如何让Android浏览器以屏幕尺寸呈现文档?
- 16. ASP.NET MVC,如何让网页内容之前呈现
- 17. Asp.Net MVC:如何让Html.ActionLink正确呈现整数值?
- 18. 如何让HAML呈现带有句点的表单标签?
- 19. 如何让JSF呈现符合XHTML 1.0严格?
- 20. 如何让我的JLabel图标在我的JList中呈现?
- 21. 如何让JSON blob中的HTML呈现正确?
- 22. 如何让Rails将html_safe文本呈现为实时链接?
- 23. 如何让我的缩略图呈现良好?
- 24. 如何让SSRS 2008 R2以Excel 2007格式导出/呈现?
- 25. 如何让杰克逊的LocalDateSerializer呈现为ISO日期?
- 26. 如何让CakePHP使用换行呈现纯文本?
- 27. 如何让我的文字在页面上呈现得更高?
- 28. 如何让Ubuntu Firefox呈现类似于Windows/Mac Firefox的网页?
- 29. 如何让我的网站背景呈现渐变色?
- 30. 让jQuery插件来呈现的node.js
仅供参考:默认情况下,UseAccessibleHeader为“true”,因此您无需设置它。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.useaccessibleheader。aspx – MikeTeeVee 2013-11-07 20:14:14