固定gridview 的标题栏 总结了一下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewText.aspx.cs" Inherits="mylatesttime.GridviewText" %>

<!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">
    <title>无标题页</title>
    <style type="text/css"  >
      .topcss
      {
       background-color:Red;
       position:relative;
       top:expression(document.getElementById("dd").scrollTop-1);
      }
      /*!
      * 思想:把gridview的标题固定死,就需要找一个固定不变的参考点
      * 关键在于把gridview 的标题中的css中位置设置成相对的,relative(这样他就能相对参考点确定位置)
      * 实现方法如下:
      * 1.top:expression(document.getElementById("dd").scrollTop-1)
      *  取所在容器DIV的滚动条的top值 减去1 是因为实践中标题上边会露出来一点头,可以自己不减1试试看
      * 2.expression(document.body.scrollTop-1)
      *  直接取所在的body 的滚动条的top值 推荐,因为每个页面基本上都有滚动条
      * 3.直接付一个值,这个方法太死,不同环境下可能会有不同,不推荐使用
      */
    </style>
</head>
<body >
    <form id="form1" runat="server">
    <div id="dd"  align="center" style=" 100%;height:400px; overflow:scroll;background-color:Gray;">

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="_id" HeaderText="编号" />
                <asp:BoundField DataField="_name" HeaderText="姓名" />
                <asp:BoundField DataField="_sex" HeaderText="性别" />
            </Columns>
            <HeaderStyle CssClass="topcss" BorderStyle="Outset" />
        </asp:GridView>

    </div>
    </form>
</body>
</html>

原文地址:https://www.cnblogs.com/liubaolongcool/p/2011320.html