ASP.NET自定义控件 - 仅包括一次嵌入式CSS参考的最佳方法是什么?
问题:我将CSS文件嵌入具有多个控件的自定义控件库中。我想为所有控件共享相同的CSS文件,无论给定表单上有多少个实例。当表单上有多个控件时,我想准确地提到ASP.NET页面HTML标题中的CSS文件。
这是我想到的(到目前为止):
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标头的更好方法是什么?
注意:clientcript.registerclientscriptresource()
方法具有接受.NET类型的参数,并且可以使用此类型来确保代码仅输出一次。不幸的是,此方法仅适用于JavaScript文件参考。如果CSS参考有内置的等效词,那将是我的偏爱。
更新:
我发现了一种更优雅的方法来执行此操作在这里通过使用page.clientscript.registerclientscriptblock并告诉您您包括自己的脚本标签,但是正如里克指出的那样,这不会将脚本添加到HTML HEAD标签中,并且也不符合XHTML符合XHTML。
更新2:
我在此线程,但是通过控件集合循环并不是一个很好的解决方案,如果您在页面上有几个参考和几个控件,则会添加很多开销。
克里斯·莱弗利(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", "stylesheet")
.Attributes.Add("type", "text/css")
End With
Page.Header.Controls.Add(css)
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), StyleName, "")
End If
End Sub
有关此解决方案的几件事。在他的原始帖子中,克里斯使用了isclientscriptIncludereGistered()
方法,该方法不是registerclientscriptblock()
方法的相应方法。为了正确制定此功能,必须使用isclientscriptBlockregistered()
进行测试。
另外,请仔细注意将传递给registerClientscriptBlock()
的类型。我将自定义数据类型传递给了此方法(在我的所有控件中相同),但是它并未以isclientsblockreded()
测试的方式注册。为了使其功能,必须将当前页面对象作为类型参数传递。
尽管诚然,该解决方案感觉有点像一个黑客,但a)不需要大量代码或开销,b)b)在页面上完全产生所需的输出,c)c)符合XHTML兼容的代码。
The problem: I am embedding a CSS file into a custom control library with several controls. I want to share the same CSS file for all of the controls regardless of how many instances of them are on a given form. When more than one control is on the form, I would like exactly 1 reference to the CSS file in the HTML header of the ASP.NET page.
Here is what I have come up with (so far):
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
Ok, it works...but the obvious flaw here is the use of FindControl()
to see if the control exists on the form. Although I am using naming containers and it still seems to be working, I am sure there is some way to break this. Adding another control on the form with the same ID is surely one...
The Question: What is a better way to ensure the header control is only added to the HTML header exactly once?
Note: The ClientScript.RegisterClientScriptResource()
method has a parameter that accepts a .NET type and this type can be used to ensure the code is only output once per page. Unfortunately this method only works with JavaScript file references. If there is a built-in equivalent for CSS references, that would be my preference.
Update:
I discovered a slightly more elegant way to do this here by using Page.ClientScript.RegisterClientScriptBlock and telling it you are including your own script tags, however as Rick points out, this doesn't add the script to the html head tag and is also not xhtml compliant.
Update 2:
I saw another interesting idea on this thread, but looping through the controls collection is not a very good solution and adds a lot of overhead if you have several references and several controls on the page.
Chris Lively came up with a better solution that requires less code. Here is my function altered with the new solution:
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", "stylesheet")
.Attributes.Add("type", "text/css")
End With
Page.Header.Controls.Add(css)
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), StyleName, "")
End If
End Sub
A couple of things to note about this solution. In his original post, Chris used the IsClientScriptIncludeRegistered()
method which was not the corresponding method to the RegisterClientScriptBlock()
method. To make this function correctly, the test must be done with IsClientScriptBlockRegistered()
.
Also, pay careful attention to the type that is passed to RegisterClientScriptBlock()
. I passed a custom datatype to this method (the same across all of my controls), but it didn't register in such a way that the IsClientScriptBlockRegistered()
test would work. In order for it to function, the current Page object must be passed in as the Type argument.
Although admittedly this solution feels a bit like a hack, it a) doesn't require a lot of code or overhead, b) produces exactly the desired output on the page, and c) is xhtml compliant code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了防止从服务器控件发出 css 文件时出现重复,我们执行以下操作:
首先,我们测试指定的脚本是否先前已注册。如果没有,我们将其添加进去。
To prevent duplication when emitting css files from server controls, we do the following:
First we test to see if the named script was previously registered. If not, we add it in.
我不知道为什么,但你的解决方案对我不起作用,ClientScript.IsClientScriptBlockRegistered 调用总是返回 false。但是约翰·布莱索对您已经提供的链接的建议(
I don't know why, but your solution didn't work for me, the ClientScript.IsClientScriptBlockRegistered call always returned false. But John Bledsoe's suggestion on the link you already provided (here) worked for me:
为什么不在控件中创建一个私有布尔变量,在最初创建 CSS 时将其设置为 true。然后,您可以在调用该方法时检查此内容,以查看 css 是否已设置。下面的例子(我的VB很生锈,所以可能有点错误)
Why not just create a private boolean variable in the control which you set to true when the CSS is initially created. You can then check this when the method is called to see if the css has already been set. Example below (my VB is rusty so might be slighty wrong)