首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 图形图象-> FLASH教程
有滚动条、固定Header的ASP.Net DataGrid实现-ASP教程,数据库相关
作者:网友供稿 点击:475
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
客户要一个有滚动条的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.

//dont 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 pagers <tr>

int start = temp.tolower().indexof(@"<tr");

//end of pagers </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 rows height.

+ @"

headertablerow.height = originaltablerow.offsetheight;

" +

//adjust pager rows 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 tables 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 tables 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.

//cant 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 tables 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.");

}

}

}

}



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·adodb.stream对象的方法/属性-ASP教程,数据库相关
·DataGrid - 导出Excel文件-.NET教程,数据库应用
·有滚动条、固定Header的ASP.Net DataGrid实现-ASP教程,数据库相关
·解决Windows 程序界面闪烁问题的一些经验-.NET教程,Windows开发
·C#数据访问类-.NET教程,C#语言
·jsp生成html--replace-JSP教程,Jsp/Servlet
·查询两个表中字段个数不同,名称不同的SQL语句-ASP教程,数据库相关
·【C#】输入汉字字符串索引拼音的首字母-.NET教程,C#语言
·使用SQLServer2005-.NET教程,Web Service开发
·Java语言中字符的处理-JSP教程,Java技巧及代码
最新文章
·flash中作毛笔写字动画效果_flash教程
·用dreamweaver制作网页时关于文字自动换行的小技巧_dreamweaver教程
·seo中十大影响链接权重的因素浅析_seo网站优化
·在windows vista系统下扩大“发送到”的范围_windows vista
·众多网站引入嵌入式广告 网络广告面临拐点_站长访谈
·王怀南:遍地都是黄金 宝宝树创新模式_站长访谈
·幻想着靠蓝海大赚是不可能的_站长心得
·谈谈做站与站长的站德问题_站长心得
·photoshop打造非主流效果—颓废诡异效果_photoshop教程
·google pagerank 技术解密 1_google推广
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭