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