专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » asp.net生成html:通过User Control生成HTML-asp.net页面的换皮肤方案 »正文

asp.net生成html:通过User Control生成HTML-asp.net页面的换皮肤方案

来源: 发布时间:星期四, 2008年11月6日 浏览:164次 评论:0
前些日子看了园友Jeffrey Zhao的关于User Control生成HTML的两篇文章. 因为我不喜欢看到我们的工程中有比较多的ashx文件(同时对于IHttpHandler接口,我的意见是尽量尝试不用IHttpHandler),就琢磨了一下如何不用这个ashx和IHttpHandler也能做到同样的功能,有点发现.在这里提出另外一个方法,和大家分享.核心就是覆盖Page类的Render方法.

先来看我的一个用来做皮肤的HTML文件HTMLPage1.htm:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
</HEAD>
<BODY>
<P><FONT face="宋体">test</FONT></P>
<P><FONT face="宋体">testee</FONT></P>
<!--$MyUserControl1$-->
<P><FONT face="宋体">teest</FONT></P>
<P><FONT face="宋体">Tttttest</FONT></P>
<!--$MyUserControl2$-->
<P><FONT face="宋体">Ttttessssttt</FONT></P>
<P>&nbsp;</P>
</BODY>
</HTML>



这个HTML文件十分简单.当然实际中你可以做得很复杂,做试验就简单点.只要原理通就可以了. 其中有两个特别得地方:<!--$MyUserControl1$-->是用来标记在此处要嵌入一个叫MyUserControl1的UserControl, <!--$MyUserControl2$-->也类似.就是说这个HTML文件有两块地方的内容要由两个UserControl来提供.这是一个特殊的HTML文件,需要一个特殊的呈现器来将其解释并呈现出来.我们来看这个特殊的呈现器:一个覆盖了Render方法的aspx页面.

WebForm1.aspx:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="True" EnableEventValidation="false" Inherits="WebApplication1.WebForm1" validaterequest="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>

其实就是一个空的aspx页面,有一个服务器端的Form而已. 稍有不同的是要加上: EnableEventValidation="false"和validaterequest="false",原因稍后再说.

再来看这个页面的后端代码WebForm1.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, System.EventArgs e)
{
MyUserControl1 ctl1;
MyUserControl2 ctl2;
ctl1 = (MyUserControl1)this.Page.LoadControl("MyUserControl1.ascx");
ctl2 = (MyUserControl2)this.Page.LoadControl("MyUserControl2.ascx");
this.Page.Controls.Add(ctl1);
this.Page.Controls.Add(ctl2);
}

public override void VerifyRenderingInServerForm(Control control)
{
return;
}

protected override void Render(HtmlTextWriter writer)
{
StringBuilder strBuilder;
System.IO.StringWriter strWriter;
System.Web.UI.HtmlTextWriter htmlWriter;
string strResponse;
string strControl;

System.IO.FileStream fileStream = System.IO.File.OpenRead(Server.MapPath("HTMLPage1.htm"));
System.IO.TextReader rd = new System.IO.StreamReader(fileStream);
strResponse = rd.ReadToEnd();


strBuilder = new StringBuilder();
strWriter = new System.IO.StringWriter(strBuilder);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
this.Controls[1].RenderControl(htmlWriter);
strControl = strBuilder.ToString();
strControl = strControl.Replace("</form>", string.Empty);
strResponse = strResponse.Replace("<BODY>", "<BODY>" + strControl);
strResponse = strResponse.Replace("</BODY>", "</FORM></BODY>");

strBuilder = new StringBuilder();
strWriter = new System.IO.StringWriter(strBuilder);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
this.Controls[3].RenderControl(htmlWriter);

如果本文没有解决您的问题,请进老妖怪开发者社区提问

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: