打印本文 打印本文 关闭窗口 关闭窗口
有滚动条、固定Header的ASP.Net DataGrid实现
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2753  更新时间:2009/4/23 10:43:52  文章录入:mintao  责任编辑:mintao

客户要一个有滚动条的ASP.Net DataGrid控件,只好写了:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.Diagnostics;

using System.IO;

using System.Web.UI.Design.WebControls;

using System.Text;

using System.Drawing;

 

[assembly:TagPrefix("Microsoft.Gtec.Dsv", "gtecdsv")]

namespace Microsoft.Gtec.Dsv

{

  /// <summary>

  /// Summary description for WebCustomControl1.

  /// </summary>

  [ToolboxData("<{0}:ScrollableFixedHeaderDataGrid runat=server></{0}:ScrollableFixedHeaderDataGrid>")]

  public class  ScrollableFixedHeaderDataGrid: System.Web.UI.WebControls.DataGrid

  {

    protected override void Render(HtmlTextWriter output)

    {

      //Use this flag to determine whether the component is in design-time or runtime.

      //The control will be rendered differently in IDE.

      //Don''''t bother to use DataGridDesigner.GetDesignTimeHtml

      bool designMode = ((Site != null) && (Site.DesignMode));

      //Backing up the properties need to change during the render process

      string tempLeft = Style["LEFT"];

      string tempTop = Style["TOP"];

      Unit tempHeight = Height;

      string tempTableStyle = Style["TABLE-LAYOUT"];

 

      //Render a "<div>" container with scrollbars.

      output.WriteBeginTag("div");

      output.WriteAttribute("id",ID + "_div");

      output.WriteAttribute("style",

        "HEIGHT: " + Height + ";" +

        //Leave 20px for the vertical scroll bar,

        //assuming the end-user will not set his scroll bar to more than 20px.

        "WIDTH: " + (Width.Value + 20) + "px;" +

        "TOP: " + Style["TOP"] + ";" +

        "LEFT: " + Style["LEFT"] + ";" +

        "POSITION: " + Style["POSITION"] + ";" +

        "OVERFLOW-X: auto;" +

        "Z-INDEX: " + Style["Z-INDEX"] + ";" +

        //Render the scrollbar differently for design-time and runtime.

        "OVERFLOW-Y: " + (designMode?"scroll":"auto")

        );

      output.Write(HtmlTextWriter.TagRightChar);

                       

      //The DataGrid is inside the "<div>" element, so place it at (0,0).

      Style["LEFT"] = "0px";

      Style["TOP"] = "0px";

      //Render the DataGrid.

      base.Render(output);

      output.WriteEndTag("div");

      //Restore the values

      Style["LEFT"] = tempLeft;

      Style["TOP"] = tempTop;

 

      //The following rendering is only necessary under runtime. It has negative impact during design time.

      if (!designMode)

      {

        //Render another copy of the DataGrid with only headers.

        //Render it after the DataGrid with contents,

&

[1] [2] [3] [4] [5]  下一页

打印本文 打印本文 关闭窗口 关闭窗口