首页 > 开发 > 综合 > 正文

dataGrid在页面拖动(用表头),不刷新页面

2024-07-21 02:16:10
字体:
来源:转载
供稿:网友

#region 声明
//----------------------------------------------------------------------
//
// 修改: 李淼(nick.lee)
//
// datagrid在页面拖动(用表头),不刷新页面

// 时间:2005-04-17

// [email protected]
// qq:16503096
//注意:引用请标明修改出处,谢谢
//----------------------------------------------------------------------
#endregion
前台文件
<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="webapplication1.webform1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
 <head>
  <title>webform1</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">
 </head>
 <body ms_positioning="gridlayout">
  <form id="form1" method="post" runat="server">
   <asp:datagrid id="datagrid1" runat="server"
    bordercolor="#cc9966" borderstyle="none" borderwidth="1px" backcolor="white" cellpadding="4"
    font-size="9pt">
    <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>
 /// webform1 的摘要说明。
 /// </summary>
 public class webform1 : system.web.ui.page
 {
  internal system.data.sqlclient.sqldataadapter sqldataadapter1;
  protected system.data.sqlclient.sqlcommand sqlselectcommand1;
  protected system.data.sqlclient.sqlconnection sqlconnection1;
  protected webapplication1.dataset1 dataset11;
//  protected gorthcontrols.gridslideheader gridslideheader1;
//  protected gorthcontrols.gridslideheader gridslideheader2;
  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();
   this.datagrid1.attributes.add("onselectstart","return false");
   this.datagrid1.attributes.add("onmousedown","f_mdown(this)");
   this.datagrid1.attributes.add("onmousemove","f_move(this)");
  }

  #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 top 1 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
 }
}


脚本文件:
/**
 * <p>title: datagrid在页面拖动(用表头,不刷新页面)</p>
 * <p>description: 实现datagrid在页面拖动(用表头,不刷新页面)</p>
 * <p>copyright: 2005-2005 by mail_ricklee corporation</p>
 * <p>company: mail_ricklee corporation</p>
 * <p>createtime: 2005-04-17 21:30</p>
 * <p>modifytime:                 </p>
 * @createauthor 李淼    * @version 1.0
 * @modifyauthor         * @version 1.0
 */
<script language="javascript">
<!--
var currentmoveobj = null;    //当前拖动对象
var relleft;    //鼠标按下位置相对对象位置
var reltop;
function f_mdown(obj)
{
    currentmoveobj = obj;        //当对象被按下时,记录该对象
    currentmoveobj.style.position = "absolute";
    relleft = event.x - currentmoveobj.style.pixelleft;
    reltop = event.y - currentmoveobj.style.pixeltop;
}
window.document.onmouseup = function()
{
    currentmoveobj = null;    //当鼠标释放时同时释放拖动对象
}
function f_move(obj)
{
    if(currentmoveobj != null)
    {
        currentmoveobj.style.pixelleft=event.x-relleft;
        currentmoveobj.style.pixeltop=event.y-reltop;
    }
}

//-->
  </script>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表