如何在asp.net生成的结果中插入脚本或其它文本信息
问题:
前段时间交付一个客户网站,客户提出加载FLASH时影响网页速度,要求做出一定改善
Flash内容我写成控件,添加了一些Flash需要用到的属性,生成脚本形式插入到页面中,原来是直接在页面插入位置中直接执行的
按客户要求,将原来的控件改写,原来的脚本执行位置上只生成一个HTML标签,然后 在页脚处加入相应的脚本进行插入操作
这个就需要对原来的aspx页面生成结果进行操作
解决:
创建自己的Page类,从原来的System.Web.UI.Page继承,生写相关的方法,具体代码如下:
Public Class MyPage
Inherits System.Web.UI.Page
Dim sw As New System.IO.StringWriter
Dim _writer As System.IO.TextWriter
Protected Overrides Function CreateHtmlTextWriter(ByVal tw As System.IO.TextWriter) As System.Web.UI.HtmlTextWriter
_writer = MyBase.CreateHtmlTextWriter(tw)
Return MyBase.CreateHtmlTextWriter(sw)
End Function
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
_writer.Write(Replace(sw.ToString, "</body>", "自定义内容</body>", 1, -1, CompareMethod.Text))
End Sub
End Class
Inherits System.Web.UI.Page
Dim sw As New System.IO.StringWriter
Dim _writer As System.IO.TextWriter
Protected Overrides Function CreateHtmlTextWriter(ByVal tw As System.IO.TextWriter) As System.Web.UI.HtmlTextWriter
_writer = MyBase.CreateHtmlTextWriter(tw)
Return MyBase.CreateHtmlTextWriter(sw)
End Function
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
_writer.Write(Replace(sw.ToString, "</body>", "自定义内容</body>", 1, -1, CompareMethod.Text))
End Sub
End Class