嗯,這就是我的意見。我決定創建包含鏈接的擴展方法。
更新:我不得不更改ScriptManager擴展,因爲它仍然不能很好地與UpdatePanels配合使用。更新後的代碼如下。
<System.Runtime.CompilerServices.Extension()> _
Public Sub RegisterStylesheetInclude(ByVal scriptManager As ScriptManager, ByVal page As Page, ByVal url As String)
Dim css = "var cssNode = document.createElement('link');" & _
"cssNode.type = 'text/css';" & _
"cssNode.rel = 'stylesheet';" & _
"cssNode.href = '" & url & "';" & _
"document.getElementsByTagName('head')[0].appendChild(cssNode);"
scriptManager.RegisterClientScriptBlock(page, page.GetType(), url, css, True)
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub RegisterStylesheetInclude(ByVal clientScriptManager As ClientScriptManager, ByVal page As Page, ByVal url As String)
Dim found = page.Header.Controls.OfType(Of HtmlLink).Any(Function(m) m.Href = url)
If Not found Then
Dim link As New HtmlLink()
link.Href = url
link.Attributes("type") = "text/css"
link.Attributes("rel") = "stylesheet"
page.Header.Controls.Add(link)
End If
End Sub