lin*_*lnk 12 asp.net gridview tableheader
TableSection = TableRowSection.TableHeader网格绑定后的设置最初工作,将标题行放入thead.在回发之后(网格未重新绑定),标题行将恢复为表格主体.我希望标题行保留在thead中; 谁能解释为什么不是这种情况或我做错了什么?
样品:
单击按钮以进行回发.回发后标题不是橙色,因为它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" />
this button is here just to trigger a postback</div>
<asp:GridView ID="gv1" runat="server"></asp:GridView>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
码
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*n P 16
请使用Pre_Render_Complete事件来添加表部分.这将确保thead始终添加该部分.正如您目前在DataBound上执行的那样,只有在绑定数据时才会添加该部分.
protected override void OnPreRenderComplete(EventArgs e)
{
if (gv1.Rows.Count > 0)
{
gv1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以随意更改我用于检查标题行的行检查.
小智 5
您必须在DataBind方法之后设置TableSection。
gv1.DataSource = this.Page.Controls;
gv1.DataBind();
gv1.HeaderRow.TableSection = TableRowSection.TableHeader;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18192 次 |
| 最近记录: |