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

ASP.NET
使用函数传递参数来执行相应的数据库操作
如何实现在窗体和窗体之间进行传递数据
ASP.NET中文显示之两种解决方法
ASP.NET、JSP及PHP之间的抉择
ASP.NET 2.0发送电子邮件中存在的问题
谈谈HtmlControl与WebControl的区别与用途
从ASP.NET 1.1升级到ASP.NET 2.0要考虑的Cookie问题
通过系统配置来提高ASP.NET应用程序的稳定性
妙用ASP2.0中的URL映射改变网址
AJAX实现web页面中级联菜单的设计
ASP.NET跨页面传值技巧总结
再议ASP.NET DataGrid控件中的“添加新行”功能
Geometry 对象浅析
重构CollapsibleSplitter
如何利用.NET Framework使用RSS feed
ASP.NET获取IP与MAC地址的方法
在ASP.NET 2.0中使用样式、主题和皮肤
ASP.NET中为GridView添加删除提示框
ASP.NET 2.0,无刷新页面新境界
看看一个.net版对话框控件

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


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

效果如下: