【发布时间】:2016-05-26 11:51:47
【问题描述】:
我正在对旧的 ASP.Net WebForms 应用程序的 UI 进行现代化改造。我利用 Bootstrap 3 来实现这一点。大多数控件很容易重新设置样式,但 GridView 控件会带来一些麻烦。 Bootstrap 需要表头位于 thead 部分,但 GridView 控件将表头呈现在表体部分内。 我能够使用以下代码强制 GridView 在 thead 元素内呈现标题:
public class AdvancedGridView : GridView
{
#region Overrides of GridView
protected override void OnPreRender( EventArgs e )
{
if ( ( ShowHeader && Rows.Count > 0 ) || ShowHeaderWhenEmpty )
HeaderRow.TableSection = TableRowSection.TableHeader;
if ( ShowFooter && Rows.Count > 0 )
FooterRow.TableSection = TableRowSection.TableFooter;
base.OnPreRender( e );
}
#endregion
}
但我还是有问题。一旦用户按任何列对表格进行排序,GridView 控件将再次呈现并在表格主体元素内移动表头。
当用户对 GridView 进行排序时,如何让 GridView 在 thead 元素中呈现其标题?
【问题讨论】:
-
我不知道您的排序方式如何,而且我从未使用过 PreRender,但我总是在 RowDataBound 事件中找到了成功:
if (e.Row.RowType == DataControlRowType.Header) e.Row.TableSection = TableRowSection.TableHeader; -
非常感谢!这个解决方案非常适合我。如果您将其添加为答案,我将接受它作为正确的解决方案。
标签: asp.net gridview twitter-bootstrap-3 webforms