分享
 
 
 

有滚动条、固定Header的ASP.Net DataGrid实现

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

客户要一个有滚动条的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,

//so that it is on the top. Z-INDEX is more complex 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 databinding.");

string temp = sbRenderedTable.ToString();

if (sbRenderedTable.Length > 0)

{

//AllowPaging at the top?

if ((AllowPaging) && ((PagerPosition.Top == PagerStyle.Position || (PagerPosition.TopAndBottom == 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(@"</tr>") + 5);

Trace.WriteLine(pager);

output.Write(pager);

output.WriteEndTag("table");

//Start of pager's <tr>

int start = temp.ToLower().IndexOf(@"<tr");

//End of pager's </tr>

int end = temp.ToLower().IndexOf(@"</tr>") + 5;

//Remove the <tr> for pager from the string. Prepare 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(@"</tr>") + 5);

Trace.WriteLine(tableHeaders);

output.Write(tableHeaders);

output.WriteEndTag("table");

string headerID = ID + "_Headers";

string pagerID = ID + "_Pager";

string divID = ID + "_div";

string adjustWidthScript = @"

<script language=javascript>

//debugger;

var headerTableRow = " + headerID + @".rows[0];

var originalTableRow = " + ID + @".rows[1];"

//Adjust header row's height.

+ @"

headerTableRow.height = originalTableRow.offsetHeight;

" +

//Adjust pager row's height, width.

pagerID + @".rows[0].height = " + ID + @".rows[0].offsetHeight;

" +

pagerID + @".style.width = " + ID + @".offsetWidth;

for (var i = 0; i < headerTableRow.cells.length; i++) {

headerTableRow.cells[i].width = originalTableRow.cells[i].offsetWidth;

}

" +

//Also needs to adjust the width of the "<div>" at client side in addition to servier side,

//since the Table's actual width can go beyond the width specified at server side under Edit mode.

//The server side width manipulation is mainly for design-time appearance.

divID + @".style.width = " + ID + @".offsetWidth + 20 + 'px';

" +

//The following script is for flow-layout. We cannot get the position of the control

//on server side if the the page is with flow-layout.

headerID + @".style.left = " + divID + @".offsetLeft;

" +

headerID + @".style.top = " + divID + @".offsetTop + " + pagerID + @".offsetHeight;

" +

headerID + @".style.position = 'absolute';

" +

pagerID + @".style.left = " + divID + @".offsetLeft;

" +

pagerID + @".style.top = " + divID + @".offsetTop;

" +

pagerID + @".style.position = 'absolute';

</script>";

Page.RegisterStartupScript("dummyKey" + this.ID, adjustWidthScript);

//output.Write(adjustWidthScript);

}

else

{

//Replace the table's ID with a new ID.

//It is tricky that we must only replace the 1st occurence,

//since the rest occurences can be used for postback scripts for sorting.

sbRenderedTable.Replace(ID,ID + "_Headers", 0, (temp.IndexOf(ID) + ID.Length));

Trace.WriteLine(sbRenderedTable.ToString());

//We only need the headers, stripping the rest contents.

temp = sbRenderedTable.ToString();

string tableHeaders = temp.Substring(0, (temp.ToLower()).IndexOf(@"</tr>") + 5);

Trace.WriteLine(tableHeaders);

output.Write(tableHeaders);

output.WriteEndTag("table");

//Client side script for matching column widths.

//Can't find a way to do this on the server side, since the browser can change widths on the client side.

string adjustWidthScript = @"

<script language=javascript>

//debugger;

var headerTableRow = " + this.ID + @"_Headers.rows[0];

var originalTableRow = " + this.ID + @".rows[0];

headerTableRow.height = originalTableRow.offsetHeight;

for (var i = 0; i < headerTableRow.cells.length; i++) {

headerTableRow.cells[i].width = originalTableRow.cells[i].offsetWidth;

}

" +

//Also needs to adjust the width of the "<div>" at client side in addition to servier side,

//since the Table's actual width can go beyond the width specified at server side under Edit mode.

//The server side width manipulation is mainly for design-time appearance.

this.ID + "_div" + @".style.width = " + this.ID + @".offsetWidth + 20 + 'px';

" +

//The following script is for flow-layout. We cannot get the position of the control

//on server side if the the page is with flow-layout.

this.ID + "_Headers" + @".style.left = " + this.ID + @"_div.offsetLeft;

" +

this.ID + "_Headers" + @".style.top = " + this.ID + @"_div.offsetTop;

" +

this.ID + "_Headers" + @".style.position = 'absolute';

</script>";

Page.RegisterStartupScript("dummyKey" + this.ID, adjustWidthScript);

//output.Write(adjustWidthScript);

}

Height = tempHeight;

Style["TABLE-LAYOUT"] = tempTableStyle;

}

}

}

protected override void OnInit(EventArgs e)

{

if (0 == Width.Value) Width = new Unit("400px");

if (0 == Height.Value) Height = new Unit("200px");

//Transparent header is not allowed.

if (HeaderStyle.BackColor.IsEmpty)

{

HeaderStyle.BackColor = Color.White;

}

//Transparent pager is not allowed.

if (PagerStyle.BackColor.IsEmpty)

{

PagerStyle.BackColor = Color.White;

}

base.OnInit (e);

}

[Browsable(false)]

public override bool ShowHeader

{

get

{

return true;

}

set

{

if (false == value)

throw new InvalidOperationException("Use the original DataGrid to set ShowHeaders to false.");

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有