当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > DataGrid表头不动,表身动

ASP.NET
为T-SQL添加intellisense功能
SQL Server 2005安装过程中出现错误的解决办法
SQL Server 2005 RTM 安装错误 :The SQL Server System Configuration Checker cannot be executed due to
有关于JSON的一些资料
不能忽略c#中的using和as操作符的用处
JavaScript系列之―同步还是异步?
获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之二(downmoon原创)
ASP.Net中防止刷新自动触发事件的解决方案
asp.net下用js实现鼠标移至小图,自动显示相应大图
Asp.Net 和 AJAX.Net 的区别
提交页面的定位--scrollIntoView的用法
利用AJAX与数据岛实现无刷新绑定
asp.net下判断用户什么时候离开,以什么方式离开
DataSet 添加数据集、行、列、主键和外键等操作示例
读写xml所有节点个人小结 和 读取xml节点的数据总结
收藏的asp.net文件上传类源码
asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
一个可以让.net程序在非WIN平台上运行的软件Mono
使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出

ASP.NET 中的 DataGrid表头不动,表身动


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 188 ::
收藏到网摘: n/a

上次和海东兄讨论了这个问题,但是他的表头没有线,所以用Div形式实现了,实现功能显示表格,代码如下(后台.cs文件只要绑定数据就可以了,字段和前台绑定的一致就OK了,在此不加赘述):

<%@ Page language="c#" Codebehind="dg1.aspx.cs" AutoEventWireup="false" Inherits="netLearn.dg1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>dg1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <script language="javascript">
  function window.onload()
  {
   var dg = document.getElementById('DataGrid1');
   var strHTML = "";
   for( var i=1; i<dg.rows.length;i++)
   {
    strHTML += dg.rows[i].cells[0].innerHTML;
   }  
   dg.rows[1].cells[0].innerHTML = "<div id='dgContent' style='height:250px;overflow-y:scroll'>" + strHTML + "</div>";
   for( var i=dg.rows.length-1; i>1;i--)
   {
    dg.deleteRow(i);
   }
  }
  </script>

 </HEAD>
 <body MS_POSITIONING="FlowLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:datagrid id="DataGrid1" runat="server" Width="576px" Height="192px" AutoGenerateColumns="False"
     BorderWidth="0px" CellPadding="0">
     <Columns>
      <asp:TemplateColumn>
       <HeaderTemplate>
        <table cellpadding="2" cellspacing="1" bgcolor="PaleTurquoise" width="100%">
         <tr>
          <th bgcolor="#006699" width="25%" align="center" style="color:#ffffff">
           序号</td>
           <th bgcolor="#006699" width="25%" align="center" style="color:#ffffff">
            ID号</td>
            <th bgcolor="#006699" width="25%" align="center" style="color:#ffffff">
             姓名</td>
             <th bgcolor="#006699" width="25%" align="center" style="color:#ffffff">
          年龄</td>
         </tr>
        </table>
       </HeaderTemplate>
       <ItemTemplate>
        <table cellpadding="2" cellspacing="1" bgcolor="PaleTurquoise" width="100%">
         <tr>
          <td bgcolor="LightSkyBlue" width="25%" style="padding-left:10px;"><%#Container.ItemIndex+1%></td>
          <td bgcolor="LightSkyBlue" width="25%" style="padding-left:10px;"><%#DataBinder.Eval(Container.DataItem,"ID")%></td>
          <td bgcolor="LightSkyBlue" width="25%" style="padding-left:10px;"><%#DataBinder.Eval(Container.DataItem,"Name")%></td>
          <td bgcolor="LightSkyBlue" width="23%" style="padding-left:10px;"><%#DataBinder.Eval(Container.DataItem,"Age")%></td>
         </tr>
        </table>
       </ItemTemplate>
      </asp:TemplateColumn>
     </Columns>
    </asp:datagrid></FONT></form>


 </body>
</HTML>

效果如下: