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

ASP.NET
用 Asp.Net 建立一个在线 RSS 新闻聚合器的方法
关于前台调用后台事件__doPostBack函数
Bin 和 App_Code 文件夹介绍
.NET 2.0 的压缩功能代码
解决Visual Studio 2005 无法显示设计视图的方法
asp.net(c#)两种随机数的算法,可用抽考题
asp.net下url传递中文的解决方案
XmlReader 读取器读取内存流 MemoryStream 的注意事项
asp.net下创建、查询、修改带名称空间的 XML 文件的例子
使用.NET存储XML数据的方法
XslTransform.Transform将结果输出到字符串里的方法
安装 VS2005 SP1 有关问题的解决办法
asp.net下中文验证码,免费开源代码
自定义应用程序配置文件(app.config)
asp.net下使用DIME协议上传文件
动态改变ASP.net页面标题和动态指定页面样式表的方法
WEB上调用HttpWebRequest奇怪问题的解决方法
HTTP协议下用Web Service上传大文件的解决方案
asp.net下Response.ContentType类型汇总
ASP.NET User Control使用技巧一则

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 150 ::
收藏到网摘: 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>

效果如下: