将ASP.NET Control转换为String

类别:.NET开发 点击:0 评论:0 推荐:
将ASP.NET Control转换为String
作者:Pro.Net Components 出自:www.pronetcomponents.com 发布日期:2004年6月1日 10点10分8秒

下面的类可以实现将ASP.NET的Control(包括aspx页面)转换成String字符串,可以用于:

  1. 用邮件发送ASP.NET的内容
  2. 用XSLT转换页面的输出
  3. ASPX页面的全局字符串的使用

C#代码

using System; using System.IO; using System.Text; using System.Web; 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 RenderPage(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 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

本文地址:http://com.8s8s.com/it/it43217.htm