程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 將ASP.NET Control轉換為String

將ASP.NET Control轉換為String

編輯:關於ASP.NET

下面的類可以實現將ASP.net的Control(包括aspx頁面)轉換成String字符串,可以用於:

用郵件發送ASP.NET的內容

用XSLT轉換頁面的輸出

ASPX頁面的全局字符串的使用

C#代碼

using System;
using System.IO;
using System.Text;
using System.<a href="http://www.bianceng.cn/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://www.bianceng.cn/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
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved