简体   繁体   中英

Why can't I add a control the page's header from the an inline ASPX code nugget?

Why can't I add a control the page's header from the an inline ASPX code nugget ? It works from the code behind.

The markup just doesn't get added to the page.

Is it the page life cycle?

In aspx page:

<%
    StyleSheet.AddToHeader(Page, New List(Of String)({
            "~/CSS/bootstrap.min.css",
            "~/CSS/skin.css"
    }))
%>

Imports Microsoft.VisualBasic
Imports System.Web.UI

Public Class StyleSheet
  Public Shared Sub AddToHeader(ByRef currentPage As Page, ByVal pathList As List(Of String))
    For Each singlePath In pathList
        Dim lineBreak As New Literal()
        Dim stylesheet As New HtmlLink()

        lineBreak.Text = Environment.NewLine
        stylesheet.Attributes.Add("rel", "stylesheet")
        stylesheet.Attributes.Add("type", "text/css")
        stylesheet.Href = currentPage.ResolveUrl(singlePath)

        currentPage.Header.Controls.Add(lineBreak)
        currentPage.Header.Controls.Add(stylesheet)
    Next
  End Sub
End Class

The code that is executed inline (inside <% and %> ) is done while it is streaming to the client computer.

You cannot do that and expect the header to be updated because you are altering the object AFTER it has performed it's "RenderControl" internal routine.

Most code behind happens before the rendering takes place which is why you can do it behind the scenes without issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM