首页 > 编程 > .NET > 正文

将ASP.NET Control转换为String

2024-07-10 12:57:11
字体:
来源:转载
供稿:网友
下面的类可以实现将asp.net的control(包括aspx页面)转换成string字符串,可以用于:

用邮件发送asp.net的内容
用xslt转换页面的输出
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

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表