这是我想出来的(到目前为止):
- Public Sub IncludeStyles(ByVal Page As System.Web.UI.Page)
- 'Don't include the reference if it already exists...
- If Page.Header.FindControl("MyID") Is Nothing Then
- Dim cssUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(Common),StylePath)
- Dim css As New System.Web.UI.HtmlControls.HtmlGenericControl("link")
- With css
- .Attributes.Add("rel","stylesheet")
- .Attributes.Add("type","text/css")
- .Attributes.Add("href",cssUrl)
- .ID = "MyID"
- End With
- Page.Header.Controls.Add(css)
- End If
- End Sub
好的,它的工作原理…但这里的明显缺陷是使用FindControl()来查看控件是否存在于表单上.虽然我使用命名容器,但仍然似乎正在运行,我相信有一些方法可以打破这个.在表单上添加相同ID的另一个控件肯定是一个…
问题:什么是更好的方法来确保标题控件只添加到HTML标题一次?
注意:ClientScript.RegisterClientScriptResource()方法具有接受.NET类型的参数,并且可以使用此类型来确保每页只输出一次代码.不幸的是,此方法仅适用于JavaScript文件引用.如果CSS引用有内置的等效项,那就是我的偏好.
更新:
我发现通过使用Page.ClientScript.RegisterClientScriptBlock并且告诉它包含你自己的脚本标签来做这个here的稍微优雅的方式,但是如Rick指出的那样,这不会将脚本添加到html头标记中不符合xhtml标准.
更新2:
我在this thread看到另一个有趣的想法,但是循环访问控件集合并不是一个很好的解决方案,如果页面上有几个引用和几个控件,会增加很多开销.
Chris Lively想出了一个更好的解决方案,需要更少的代码.这是我的功能改变与新的解决方案:
- Public Sub IncludeStyles(ByVal Page As System.Web.UI.Page)
- If Not Page.ClientScript.IsClientScriptBlockRegistered(StyleName) Then
- Dim cssUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(Common),StylePath)
- Dim css As New System.Web.UI.HtmlControls.HtmlGenericControl("link")
- With css
- .Attributes.Add("href",cssUrl)
- .Attributes.Add("rel","text/css")
- End With
- Page.Header.Controls.Add(css)
- Page.ClientScript.RegisterClientScriptBlock(GetType(Page),StyleName,"")
- End If
- End Sub
关于这个解决方案需要注意的几件事情.在他的原始帖子中,Chris使用了IsClientScriptIncludeRegistered()方法,而不是RegisterClientScriptBlock()方法的相应方法.要使此功能正确,测试必须使用IsClientScriptBlockRegistered()完成.
另外,请注意传递给RegisterClientScriptBlock()的类型.我将自定义数据类型传递给此方法(在所有控件中都是相同的),但没有以IsClientScriptBlockRegistered()测试的方式进行注册.为了使其工作,当前的Page对象必须作为Type参数传入.
尽管诚然,这种解决方案感觉有点像黑客,但它不需要很多代码或开销,b)在页面上产生所需的输出,c)是符合xhtml的代码.
解决方法
- if (!Page.ClientScript.IsClientScriptBlockRegistered("SoPro." + id)) {
- HtmlLink cssLink = new HtmlLink();
- cssLink.Href = cssLink.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(),styleSheet);
- cssLink.Attributes.Add("rel","stylesheet");
- cssLink.Attributes.Add("type","text/css");
- this.Page.Header.Controls.Add(cssLink);
- this.Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page),"SoPro." + id,String.Empty);
- }