0

我想修复我的 gridview 标题位置,以便在垂直滚动列标题时必须看到。我已经尝试了多种解决方案,但没有一个对我有用。以下是在 asp.net 中修复 gridview 标头最流行的代码,请解决我的问题,为什么它不起作用

css代码

<style type="text/css">

 .WrapperDiv 
    {
        width:560px;height:290px;border: 1px solid black;
    }        


   .WrapperDiv TH 
    {
        /* Needed for IE */
         position: relative;
    }

   .WrapperDiv TR 
    {
        /* Needed for IE */
        height:0px;
    }


</style>  

javascript代码

<script type="text/ecmascript">


    var headerHeight = 8;


    freezeGridViewHeaderForIEAndFF('GridView2', 'WrapperDiv');


    function freezeGridViewHeaderForIEAndFF(gridViewId, wrapperDivCssClass) {
        debugger;
        var grid = document.getElementById(gridViewId);
        if (grid) {
       if (grid.style && grid.style.visibility)
            grid.style.visibility = 'hidden';


          if (grid.parentNode) {
              //Find wrapper div output by GridView
           var  div = grid.parentNode;
              if (div.tagName == "div1") {
                   div.className = wrapperDivCssClass;
                   div.style.overflow = "auto";
               }
           }

            var grid = prepareFixedHeader(grid);
            var tbody = grid.getElementsByTagName('TBODY')[0];

            //Needed for Firefox
            tbody.style.height = (div1.offsetHeight - headerHeight) + 'px';

            tbody.style.overflowX = "hidden";
            tbody.overflow = 'auto';
            tbody.overflowX = 'hidden';

            grid.style.visibility = 'visible';
        }
    }


    function prepareFixedHeader(grid) {
        //Find DOM TBODY element and  and 
        var tags = grid.getElementsByTagName('TBODY');
        if (tags != 'undefined') {
            var tbody = tags[0];

            var trs = tbody.getElementsByTagName('TR');

            if (trs != 'undefined') {
                headerHeight += trs[0].offsetHeight;

                //Remove first TR tag from it        
                var headTR = tbody.removeChild(trs[0]);

                //create a new element called THEAD
                var head = document.createElement('THEAD');
                head.appendChild(headTR);

                //add to a THEAD element instead of TR so CSS styles
                //can be applied properly in both IE and FireFox
                grid.insertBefore(head, grid.firstChild);
            }
        }

        return grid;
    }
</script>

网格视图 asp 代码

<div id="div1"style="overflow-x:hidden;overflow-y:auto;height:400px">
     <table>
    <asp:GridView ID="GridView2" style="table-layout:fixed;" Width="1000px" AutoGenerateColumns ="False" AllowSorting="true"        runat="server"                 
        CellPadding="4" ForeColor="#333333" 
        GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField   HeaderText="Assembly" HeaderStyle-Font-Size="Small" >

            <ItemTemplate >
                <asp:label ID="label23" Font-Size="small"  ToolTip ="click to see assembly page" runat="server" Text='<%# bind("AsbCd") %>' >
                </asp:label>
             </ItemTemplate>
                <HeaderStyle BorderStyle="Outset" />
                <ItemStyle BorderStyle="Outset" HorizontalAlign ="center" />
             </asp:TemplateField>

            <asp:TemplateField HeaderText="AssemblyName" HeaderStyle-Font-Size="small"   >
            <ItemTemplate> 
            <asp:label ID="label24" Font-Size="Small" ToolTip ="click to see assembly page"    runat="server" Text='<%# bind("AsbNm") %>' >
                </asp:label>
                </ItemTemplate>
                <HeaderStyle BorderStyle="Outset" />
                <ItemStyle BorderStyle="Outset" HorizontalAlign="Left" />
            </asp:TemplateField>

            <asp:BoundField HeaderText="Source" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"  >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
            </asp:BoundField>

            <asp:BoundField HeaderText="Operators" DataField="operators" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"  >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
            </asp:BoundField>

            <asp:BoundField HeaderText="Hours" DataField="Days" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"   >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"/>
            </asp:BoundField>

            <asp:BoundField DataField="PlannedDt" HeaderText="Start Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"   
                DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false"  >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
            </asp:BoundField>

            <asp:BoundField DataField="PlancedEnddt" HeaderText="End Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"   
                DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"/>
            </asp:BoundField>

            <asp:BoundField DataField="Operator" HeaderText="Operator" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"   >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
            </asp:BoundField>

            <asp:BoundField DataField="ActualDays" HeaderText="Hours" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"   >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"  />
            </asp:BoundField>
            <asp:BoundField DataField="ActualStartDt" HeaderText="Start Date " ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" 

                DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"  />
            </asp:BoundField>
            <asp:BoundField DataField="ActualEndDt" HeaderText="End Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell "     
                DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="IssuePending" HeaderText="Shortfall" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell " >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="rack" HeaderText="Rack" ItemStyle-Font-Size="Small" HeaderStyle-Font-size="Small" HeaderStyle-CssClass="fixedheadercell"  >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
            </asp:BoundField>

            <asp:BoundField DataField="Remark" HeaderText="Remark" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"  >
            <HeaderStyle BorderStyle="Outset" />
            <ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
            </asp:BoundField>

        </Columns>


    </asp:GridView>
     </table>
    </div>
4

2 回答 2

1

您可以尝试将标题与内容分开,并将标题放在可滚动内容之外。

<div>Some Page Content </div>
    //your heading goes here
    <div style="max-height: 400px; overflow: auto;">
       //your gridview goes here
    </div>
<div>Some Page Content </div>

像这样

 <div style="float: left; width: 100%;">
        <table id="Table1" runat="server" cellpadding="1" border="1" style="background-color: #FFFFFF;
            border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;
            width: 100%;">
            <tr style="background-color: #E5E5FE">
                <th style="width: 15%;">
                    Order
                </th>
                <th style="width: 15%;">
                    Column Title
                </th>
                <th style="width: 15%;">
                    DataType
                </th>

            </tr>
        </table>
    </div>
    <div style="max-height: 200px; overflow: auto; float: left; width: 100%;">
        <asp:GridView ID="GridView2" Style="table-layout: fixed;" Width="100%" AutoGenerateColumns="false"
            AllowSorting="true" runat="server" CellPadding="4" ForeColor="#333333" GridLines="both" ShowHeader="False">
            <AlternatingRowStyle BackColor="White"     />
            <Columns>
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='Teste'>
                        </asp:Label>
                    </ItemTemplate>
                <ItemStyle Width="15%" />
                </asp:TemplateField>
                  <asp:TemplateField >
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='Teste'>
                        </asp:Label>
                    </ItemTemplate>
                      <ItemStyle Width="15%" />

                </asp:TemplateField>
                  <asp:TemplateField >
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='Teste'>
                        </asp:Label>
                    </ItemTemplate>
                      <ItemStyle Width="15%" />

                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
于 2013-10-07T10:28:58.807 回答
0

这是 js 版本。但是在链接中,标题的宽度以像素为单位。所以,它不会随着页面缩小而缩小。

http://www.codeproject.com/Articles/61775/Grid-View-with-Fixed-Header
于 2013-10-08T07:28:27.293 回答