欢迎光临
我们一直在努力

dataGrid在页面拖动行(在表头,用htc文件实现)-.NET教程,数据库应用

建站超值云服务器,限时71元/月

#region 声明
//———————————————————————-
//
// 修改: 李淼(nick.lee)
//
// datagrid在页面拖动行(在表头,用htc文件实现)

// 时间:2005-04-23

// boyorgril@msn.com
// boyorgril@hotmail.com
// qq:16503096
//注意:引用请标明修改出处,谢谢
//———————————————————————-
#endregion

前台页面
<%@ page language=”c#” codebehind=”webform5.aspx.cs” autoeventwireup=”false” inherits=”webapplication1.webform5″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
 <head>
  <title>webform5</title>
  <meta name=”generator” content=”microsoft visual studio .net 7.1″>
  <meta name=”code_language” content=”c#”>
  <meta name=”vs_defaultclientscript” content=”javascript”>
  <meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5“>
  <link href=”xptable.css” type=”text/css” rel=”stylesheet”>
 </head>
 <body>
  <form id=”form1″ method=”post” runat=”server”>
   <asp:datagrid id=”datagrid1″ runat=”server” font-size=”9pt” cellpadding=”4″ style=”behavior:url(grid.htc)”
    bordercolor=”#cc9966″ borderstyle=”none” borderwidth=”1px” backcolor=”white”>
    <selecteditemstyle font-bold=”true” forecolor=”#663399″ backcolor=”#ffcc66″></selecteditemstyle>
    <itemstyle forecolor=”#330099″ backcolor=”white”></itemstyle>
    <headerstyle font-bold=”true” forecolor=”#ffffcc” backcolor=”#990000″></headerstyle>
    <footerstyle forecolor=”#330099″ backcolor=”#ffffcc”></footerstyle>
    <pagerstyle horizontalalign=”center” forecolor=”#330099″ backcolor=”#ffffcc”></pagerstyle>
   </asp:datagrid>
  </form>
 </body>
</html>
后台cs文件
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace webapplication1
{
 /// <summary>
 /// webform5 的摘要说明。
 /// </summary>
 public class webform5 : system.web.ui.page
 {
  protected system.data.sqlclient.sqldataadapter sqldataadapter1;
  protected system.data.sqlclient.sqlcommand sqlselectcommand1;
  protected system.data.sqlclient.sqlconnection sqlconnection1;
  protected webapplication1.dataset1 dataset11;
  protected system.web.ui.webcontrols.datagrid datagrid1;
 
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   sqldataadapter1.fill(this.dataset11);
   this.datagrid1.datasource=this.dataset11.tables[0];
   this.databind();
  }

  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 – 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.sqldataadapter1 = new system.data.sqlclient.sqldataadapter();
   this.sqlselectcommand1 = new system.data.sqlclient.sqlcommand();
   this.sqlconnection1 = new system.data.sqlclient.sqlconnection();
   this.dataset11 = new webapplication1.dataset1();
   ((system.componentmodel.isupportinitialize)(this.dataset11)).begininit();
   //
   // sqldataadapter1
   //
   this.sqldataadapter1.selectcommand = this.sqlselectcommand1;
   this.sqldataadapter1.tablemappings.addrange(new system.data.common.datatablemapping[] {
                           new system.data.common.datatablemapping(“table”, “categories”, new system.data.common.datacolumnmapping[] {
                                                     new system.data.common.datacolumnmapping(“categoryid”, “categoryid”),
                                                     new system.data.common.datacolumnmapping(“categoryname”, “categoryname”),
                                                     new system.data.common.datacolumnmapping(“description”, “description”),
                                                     new system.data.common.datacolumnmapping(“picture”, “picture”)})});
   //
   // sqlselectcommand1
   //
   this.sqlselectcommand1.commandtext = “select categoryid, categoryname, description, picture from categories”;
   this.sqlselectcommand1.connection = this.sqlconnection1;
   //
   // sqlconnection1
   //
   this.sqlconnection1.connectionstring = “workstation id=\”star-nick\”;packet size=4096;user id=sa;data source=\”(local)\”;pers” +
    “ist security info=false;initial catalog=northwind;pwd=sa;”;
   //
   // dataset11
   //
   this.dataset11.datasetname = “dataset1”;
   this.dataset11.locale = new system.globalization.cultureinfo(“zh-cn”);
   this.load += new system.eventhandler(this.page_load);
   ((system.componentmodel.isupportinitialize)(this.dataset11)).endinit();

  }
  #endregion
 }
}

grid.htc文件:
/**
 * <p>title: datagrid在页面拖动行(在表头,用htc文件实现)</p>
 * <p>description: datagrid在页面拖动行(在表头,用htc文件实现)</p>
 * <p>copyright: 2005-2005 by mail_ricklee corporation</p>
 * <p>company: mail_ricklee corporation</p>
 * <p>createtime: 2005-04-23 21:30</p>
 * <p>modifytime:                 </p>
 * @createauthor 李淼    * @version 1.0
 * @modifyauthor         * @version 1.0
 */
<public:component>
 <public:attach event=”oncontentready”   onevent=”fninit()”   />
 <public:attach event=”onclick”   onevent=”fnclick()”   />
 <method name=”menubarpush”/>
 <script language=”jscript”>
  function fninit()
    {
   for(var i=0;i<element.rows.length;i++)
   {
    var _tr = element.rows[i];
    for(var j=0;j<_tr.cells.length;j++)
    {
     if(i==0)
     {
      _tr.attachevent(“onmousedown”, fnmousedown);
      _tr.attachevent(“onmousemove”,fnmousemove);
      _tr.attachevent(“onmouseover”,fnmouseover);
      _tr.attachevent(“onselectstart”,fncancel);
      window.document.attachevent(“onmouseup”, fnmouseup);
      window.document.attachevent(“onmousemove”,fnmousemove);
     }
    }

   }
   var _line = window.document.createelement(“div”);
   _line.style.position = “absolute”;
   _line.style.backgroundcolor=”silver”;
   _line.style.width=1;
   window.document.body.appendchild(_line);
   element.splitline = _line;
   element.splitline.style.display = “none”;
    }
  function fnmouseover()
  {
   return;
  }
  function fnmousemove(){
   if(!element.splitlocked) return;
   fnmousemove();  
  }
  function fnmousemove(){
   var oel = event.srcelement;
   element.splitline.style.left = window.event.x;
   element.splitline.style.top = gettop(element);
   element.splitline.style.height = element.parentelement.clientheight;
   if(element.splitlocked) return;
   if(!ifsplitlocation(oel)) return;
  }
  function fnclick(){
   var oel = event.srcelement;
  }
  function fnmousedown(){
   var oel = event.srcelement;
   if(!ifsplitlocation(oel)) return;
   element.splitline.style.display = “”;
   element.splitlocked  = true;
   window.document.attachevent(“onselectstart”,fncancel);
  }
  function fnmouseup()
  {
   element.splitline.style.display = “none”;
   element.splitlocked  = false;
   element.document.body.style.cursor=default;
   if(element.curresizetd == null) return;
   var otd = element.curresizetd;
   var otdleft = getleft(otd);
   var otdwidth =  element.splitline.style.pixelleft – otdleft
   if(otdwidth < 0) return;
   otd.style.width = otdwidth;
   window.document.detachevent(“onselectstart”,fncancel);
  }
  function ifsplitlocation(oel)
  {
   if(oel.tagname == “div”)
    oel = oel.parentelement;
   if(oel.tagname == “td”)
   {
    if(math.abs(event.offsetx – oel.clientwidth) <= 5)
    {
     element.curresizetd = oel;
     element.document.body.style.cursor=col-resize;
    }
    else if (math.abs(event.offsetx) <= 5 && oel.cellindex>0){
     if(oel.cellindex>0){
      element.curresizetd = oel.parentelement.cells(oel.cellindex-1);
      element.document.body.style.cursor=col-resize;
     }
    }
    else{
     element.curresizetd = null;
     element.document.body.style.cursor=default;
     return false;
    }
   }
   return true;
  }
  function gettop(e){
   var t=e.offsettop;
   while(e=e.offsetparent){
    t+=e.offsettop;
    }
   return t;
  }
  function getleft(e){
   var l=e.offsetleft;
   while(e=e.offsetparent){
    l+=e.offsetleft;
    }
   return l;
  }
    function fncancel()
    {
      window.event.returnvalue = false;
   return false;
  }
   </script>
</public:component>

新修改了拖动线和去掉边框^_^

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » dataGrid在页面拖动行(在表头,用htc文件实现)-.NET教程,数据库应用
分享到: 更多 (0)