为什么 GridView 回发后不将标题行渲染为 thead?

发布于 2024-11-18 19:03:11 字数 2036 浏览 3 评论 0原文

网格绑定后设置 TableSection = TableRowSection.TableHeader 最初会起作用,将标题行放在 thead 中。回发后(网格未重新绑定)标题行恢复到表主体。我希望标题行保留在 head 中;有人可以解释为什么情况并非如此或者我做错了什么吗?

示例:

单击按钮引发回发。回发后标头不再是橙色,因为它不再位于 thead 中。

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>

代码

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();
        }
    }
}

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" />
     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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪歌 2024-11-25 19:03:11

使用 Pre_Render_Complete 事件来添加表部分。这将确保始终添加 thead 部分。由于您当前在 DataBound 上执行此操作,因此只有在绑定数据时才会添加该部分。

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

请随意更改我用来检查标题行是否存在的行检查,就像您当前所做的那样。

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.

乄_柒ぐ汐 2024-11-25 19:03:11

您必须在 DataBind 方法之后设置 TableSection。

 gv1.DataSource = this.Page.Controls;
 gv1.DataBind();
 gv1.HeaderRow.TableSection = TableRowSection.TableHeader; 

You must set TableSection after DataBind method.

 gv1.DataSource = this.Page.Controls;
 gv1.DataBind();
 gv1.HeaderRow.TableSection = TableRowSection.TableHeader; 
杯别 2024-11-25 19:03:11

只需将以下代码放入 gridview 的 prerender 事件中

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文