将样式表链接添加到动态创建的页面对象
我正在创建一个 Page 对象并向其添加一个控件以用于打印目的。该代码有效,但是我找不到将样式表链接添加到标题的方法。在我粘贴的代码中,我尝试添加到标头的链接,然后将标头控件添加到页面,但这会导致错误:
请求在此上下文中不可用 System.Web.UI.Page.get_Request() +8700216 System.Web.UI.HtmlControls.HtmlHead.RenderChildren(HtmlTextWriter编写器)+83
Function getControlHtml() As String
Dim sw As New StringWriter
Dim tw As New HtmlTextWriter(sw)
Dim pg As New Page()
pg.EnableEventValidation = False
Dim cssLink As New HtmlLink
cssLink.Href = "~/css/StyleSheet.css"
cssLink.Attributes.Add("rel", "Stylesheet")
cssLink.Attributes.Add("type", "text/css")
'works without this code
Dim head As New HtmlHead
head.Controls.Add(cssLink)
pg.Controls.Add(head)
Dim frm As New HtmlForm
pg.Controls.Add(frm)
frm.Attributes.Add("runat", "server")
frm.Controls.Add(pnlMACForm)
pg.DesignerInitialize()
pg.RenderControl(tw) ' <--
Return sw.ToString()
End Function
I'm creating a Page object and adding a control to it for printing purposes. The code works, however I can not find a way to add a stylesheet link to the header. In the code I pasted I'm trying to add a link to the header and then add the header control to the page, but this causes an error:
Request is not available in this context
System.Web.UI.Page.get_Request() +8700216
System.Web.UI.HtmlControls.HtmlHead.RenderChildren(HtmlTextWriter writer) +83
Function getControlHtml() As String
Dim sw As New StringWriter
Dim tw As New HtmlTextWriter(sw)
Dim pg As New Page()
pg.EnableEventValidation = False
Dim cssLink As New HtmlLink
cssLink.Href = "~/css/StyleSheet.css"
cssLink.Attributes.Add("rel", "Stylesheet")
cssLink.Attributes.Add("type", "text/css")
'works without this code
Dim head As New HtmlHead
head.Controls.Add(cssLink)
pg.Controls.Add(head)
Dim frm As New HtmlForm
pg.Controls.Add(frm)
frm.Attributes.Add("runat", "server")
frm.Controls.Add(pnlMACForm)
pg.DesignerInitialize()
pg.RenderControl(tw) ' <--
Return sw.ToString()
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实无法按照您的想法动态创建页面。您所做的只是创建 Page 对象,但尚未通过 ASP.net 管道完成此操作。
这意味着 Page 对象尚未设置为请求的
IHttpHandler
(因此尚未获得包含其所需的所有上下文对象的HttpApplication
,例如Request
和Response
),并且调用任何页面生命周期方法(如RenderControl
)都会失败。ASP.Net Webforms 没有一种将页面呈现为字符串的简单方法。不幸的是,创建一个“独立”页面对象并向其添加控件不会让您走得太远。如果由于某种原因您确实需要在页面生命周期之外呈现控件,您可以通过 加载和渲染 .ascx 文件,但这可能不够动态,无法满足您的需求。
我能问一下您想通过从这些控件获取 HTML 来做什么吗?
You really can't create a page dynamically in the way that you're thinking. All you're doing is creating the Page object, but you haven't done it via the ASP.net pipeline.
That means that the Page object hasn't been set as the
IHttpHandler
for the request (and consequently hasn't been handed anHttpApplication
containing all the context objects it needs, likeRequest
andResponse
), and calling any of the page lifecycle methods (likeRenderControl
) are going to fail.ASP.Net webforms does not have an easy way of rendering a page to a string. Creating an "unattached" page object and adding controls to it will unfortunately not lead you very far. If you really need to render controls outside of the page lifecycle for some reason, you may be able to do it by loading and rendering .ascx files, but this may not be dynamic enough for your needs.
Can I ask what you're trying to do by getting the HTML from these controls?