ASP.NET Control转换为String

分类: asp.net   出处:iocblog整理  更新时间:2009-07-13   添加到收藏  

    下面的类可以实现将asp.net的control(包括aspx页面)转换成string字符串,可以用于:
  
  用邮件发送asp.net的内容
  用xslt转换页面的输出
  aspx页面的全局字符串的使用
  
  c#代码
  
  using system;
  using system.io;
  using system.text;
  using system.<a href="http://dev.21tx.com/web/" target="_blank">web</a>;
  using system.web.ui;
  
  public class render
  {
   public static string rendercontrol(system.web.ui.control control)
   {
   stringbuilder result = new stringbuilder(1024);
   control.rendercontrol(new htmltextwriter(new stringwriter(result)));
   return result.tostring();
   }
   public static string rendercontrol(system.web.ui.templatecontrol control)
   {
   stringbuilder result = new stringbuilder(1024);
   control.rendercontrol(new htmltextwriter(new stringwriter(result)));
   return result.tostring();
   }
   public static string rend<a href="http://dev.21tx.com/corp/solution/erp/" target="_blank">erp</a>age(string pagelocation)
   {
   system.web.httpcontext context = system.web.httpcontext.current;
   stringbuilder result = new stringbuilder(1024);
   context.server.execute(pagelocation,
   new htmltextwriter(new stringwriter(result)));
   return result.tostring();
   }
  }
  
  vb.net代码
  
  imports system [来源www.iocblog.net]
  imports system.io
  imports system.text
  imports system.web
  imports system.web.ui
  
  public class render
   public shared function rendercontrol(byval control as system.web.ui.control)_
   as string
   dim result as stringbuilder = new stringbuilder(1024)
   control.rendercontrol(new htmltextwriter(new stringwriter(result)))
   return result.tostring()
   end function
   public shared function rendercontrol(byval control as system.web.ui.templatecontrol)_
   as string
   dim result as stringbuilder = new stringbuilder(1024)
   control.rendercontrol(new htmltextwriter(new stringwriter(result)))
   return result.tostring()
   end function
   public shared function renderpage(byval pagelocation as string) as string
   dim context as system.web.httpcontext = system.web.httpcontext.current
   dim result as stringbuilder = new stringbuilder(1024)
   context.server.execute(pagelocation, _
   new htmltextwriter(new stringwriter(result)))
   return result.tostring()
   end function
  end class
 [来源www.iocblog.net]



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。