将样式表链接添加到动态创建的页面对象

发布于 2024-09-25 06:39:12 字数 965 浏览 4 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-10-02 06:39:12

您确实无法按照您的想法动态创建页面。您所做的只是创建 Page 对象,但尚未通过 ASP.net 管道完成此操作。

这意味着 Page 对象尚未设置为请求的 IHttpHandler (因此尚未获得包含其所需的所有上下文对象的 HttpApplication,例如RequestResponse),并且调用任何页面生命周期方法(如 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 an HttpApplication containing all the context objects it needs, like Request and Response), and calling any of the page lifecycle methods (like RenderControl) 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文