CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

一段很牛的代码,高手看看懂不懂,指导一下

楼主rawliu(rawliu)2005-11-30 17:02:34 在 .NET技术 / ASP.NET 提问

using   System;  
  using   System.Web.UI;  
  using   System.Web.UI.WebControls;  
  using   System.ComponentModel;  
  using   System.Diagnostics;  
  using   System.IO;  
  using   System.Web.UI.HtmlControls;  
  using   System.Text;  
  using   System.Drawing;  
   
  [assembly:TagPrefix("PowerDataGrid","grid")]  
  namespace   DataGrid20051130  
  {  
  ///   <summary>  
  ///   Summary   description   for   WebCustomControl1.  
  ///   </summary>  
  //[DefaultProperty("Text"),  
  //ToolboxData("<{0}:WebCustomControl1   runat=server></{0}:WebCustomControl1>")]  
  [ToolboxData("<{0}:WebCustomControl1   runat=server></{0}:WebCustomControl1>")]  
  public   class   WebCustomControl1   :   System.Web.UI.WebControls.DataGrid  
  {  
  // private   string   text;  
  //  
  // [Bindable(true),  
  // Category("Appearance"),  
  // DefaultValue("")]  
  // public   string   Text  
  // {  
  // get  
  // {  
  // return   text;  
  // }  
  //  
  // set  
  // {  
  // text   =   value;  
  // }  
  // }  
   
  ///   <summary>  
  ///   Render   this   control   to   the   output   parameter   specified.  
  ///   </summary>  
  ///   <param   name="output">   The   HTML   writer   to   write   out   to   </param>  
  protected   override   void   Render(HtmlTextWriter   output)  
  {  
   
  //output.Write(Text);  
  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   +   ";"   +  
   
  "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);  
  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)  
   
  {  
  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));  
   
  问题点数:50、回复次数:24Top

1 楼rawliu(rawliu)回复于 2005-11-30 17:02:55 得分 0

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];"+   @"  
   
                                                        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.");  
   
  }  
   
  }  
  }  
  }  
  我得MSN是   logo_liu@hotmail.com        
  欢迎共同学习  
  Top

2 楼ediex(心剑)回复于 2005-11-30 17:36:43 得分 0

顶Top

3 楼likun_vc(www.netin4.com)回复于 2005-11-30 17:40:44 得分 0

我先顶!再慢看Top

4 楼shoutor(www.mouxiao.com)回复于 2005-11-30 17:45:44 得分 0

这是一个名成PowerDataGrid的grid控件Top

5 楼shoutor(www.mouxiao.com)回复于 2005-11-30 17:46:16 得分 0

编译后注册到工具箱就可以使用Top

6 楼baby97(小雕(Asp→.Net中...)回复于 2005-11-30 17:49:08 得分 0

顶一下。慢慢看。Top

7 楼xczgb(TJJTDS)回复于 2005-11-30 17:57:29 得分 0

收藏下来。。。望高手来解答。顶Top

8 楼rawliu(rawliu)回复于 2005-11-30 17:57:35 得分 0

不是啊,他有一个错误,就是在运行时sbRenderedTable为空,导致数据显示不出来。  
  Top

9 楼clxxj(查无此人)回复于 2005-11-30 20:21:00 得分 0

用这个,已在实际项目中应用.  
  http://devuser.cnblogs.com/archive/2005/10/26/262663.htmlTop

10 楼szh3210(/+/=〆)回复于 2005-11-30 20:37:40 得分 0

慢慢看Top

11 楼sp1234(asp.net不是一个语言,是一个操作系统)回复于 2005-11-30 21:00:12 得分 0

这种代码把简单的事做得复杂,懒得看。Top

12 楼dzgwt2004(dzgwt2004)回复于 2005-11-30 21:50:56 得分 0

我是初学者,在建立项目的时候只有WebUserControl1,我怎么找不到WebCustomControl1   啊?这个项目是怎么建立的啊?请教中……Top

13 楼tomtown530(梦想一定会实现!)回复于 2005-11-30 22:34:05 得分 0

这么强?!mark一下Top

14 楼xiedan79(Sam Xie)回复于 2005-12-01 08:55:20 得分 0

自定义的   DataGridTop

15 楼liushui1981()回复于 2005-12-01 09:13:31 得分 0

好长!晕!!!Top

16 楼aarondai(冷侠)回复于 2005-12-01 09:18:07 得分 0

强人阿,想到了以前的自己,哈  
   
  现在都不想看了。。。。。。。。。。。。。Top

17 楼qwerttyy(今天也要快乐!)(每天回十帖)回复于 2005-12-01 09:26:22 得分 0

太长了.看晕Top

18 楼HJ_34(刺桐游子)回复于 2005-12-01 09:41:53 得分 0

...Top

19 楼killsound(小不懂)回复于 2005-12-01 09:56:30 得分 0

没注释!!浪费时间Top

20 楼yooono(Di调)回复于 2005-12-01 12:52:04 得分 0

我看晕了,你赔!Top

21 楼triffang(冲凉不除3)回复于 2005-12-01 12:58:18 得分 0

这控件扩展的太夸张!  
  不好用,性能也不咋地!Top

22 楼BeRush(艾威)回复于 2005-12-01 15:20:14 得分 0

这种代码把简单的事做得复杂,懒得看。  
  Top

23 楼rawliu(rawliu)回复于 2005-12-05 22:00:34 得分 0

呵呵,我作出来了,有点问题,就是水平滚动条出不来,郁闷  
  Top

24 楼losingrose()回复于 2005-12-05 22:11:59 得分 0

ComponentArt_WebUI系列控件不错,还有破解的可以下,怎一个爽字了得  
   
  --------------------------------------------------------------  
  NeT(Nebula   Team)  
  成都理工学生求兼职  
  MSN:losingrose@21cn.com  
  QQ技术群7019839                  
  4329478Top

相关问题

  • 急需帮助!高手来指导一下!(非代码问题)
  • 欢迎API高手莅临指导,请看这段代码有什么错??
  • 有谁懂这段代码
  • 看不懂的代码
  • 这段代码读不懂
  • 一段as代码,没看懂
  • 这段代码搞不懂
  • 与硬件有关的问题,各位给个指导,不需要写代码和分析
  • 想做一个图书馆管理系统,那位高手可以提供指导或源代码
  • 我写的网络流量监控程序的源代码. 诚恳欢迎大家指导! (内祥)

关键词

  • datagrid
  • sbrenderedtable
  • adjustwidthscript
  • render
  • tableheaders
  • pager
  • writeendtag
  • output
  • designmode
  • htw

得分解答快速导航

  • 帖主:rawliu

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo