There is a scroll bar, fixed header ASP.NET DATAGRID implementation

xiaoxiao2021-03-06  50

Customers have an ASP.NET DATAGRID control with a scroll bar, I have to write:

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 Description for WebcustomControl1.

///

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

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 "

" 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 "

" 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 folload 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,

// SO That it is on the top. z-index is more intrlex here.

// set height to 0, SO That IT Will Adjust On ITS OWN.

Height = New Unit ("0px");

StringWriter SW = new stringwriter ();

HTMLTextWriter HTW = New HtmlTextWriter (SW);

// this style is important for matching column widths later.

Style [Table-Layout "] =" fixed ";

Base.render (htw);

Stringbuilder sbrenderedtable = sw.getstringbuilder ();

HTW.CLOSE ();

SW.CLOSE ();

Debug.assert ((sbrenderedtable.length> 0),

"Rendered HTML String is Empty. Check ViewState Usage and Database");

String Temp = sbrenderedtable.toString ();

IF (sbrenderedtable.length> 0) {

// allowpaging at the top?

IF & ((anyposition.top == Pagerstyle.position ||))))))))))))

{

Trace.writeline (TEMP);

SBRENDEREDTABLE.REPLACE (ID, ID "_PAGER", 0, (Temp.IndexOf (ID) id.length));

Temp = sbrenderedtable.toString ();

String Pager = Temp.Substring (0, Temp.tolower (). Indexof (@ "<") 5);

Trace.writeLine (PAGER);

Output.write (PAGER);

Output.writeEndtag ("Table");

// Start of Pager's

INT Start = Temp.tolower (). Indexof (@ "

// end of pager's

INT end = Temp.tolower (). Indexof (@ "") 5;

// remove the forpager from the string. Preted to render the headers.

SbrenderedTable.Remove (start, end-start);

Trace.writeline (sbrenderedtable.toString ());

SBRENDEREDTABLE.REPLACE (ID "_pager", ID "_headers", 0, (Temp.indexof (ID "_ Pager") (ID "_ Pager"). LENGTH)

Temp = sbrenderedtable.toString ();

String TableHeaders = Temp.Substring (0, (Temp.tolower ()). IndexOf (@ "") 5);

TRACE.WRITELINE (TABLEHEADERS);

Output.write (TableHeaders);

Output.writeEndtag ("Table");

String headerid = ID "_headers";

String PagerID = ID "_pager";

String DiVid = ID "_div";

String AdjustWidthscript = @ "