我正在创建一个 Page 对象并向其添加一个控件以用于打印目的。该代码有效,但是我找不到将样式表链接添加到标题的方法。在我粘贴的代码中,我尝试向标题添加链接,然后将标题控件添加到页面,但这会导致错误:
请求在此上下文中不可用 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