ASP.NET自定义控件 - 什么是只包括一次嵌入CSS参考的最佳方式? [英] ASP.NET Custom Control - What is the best way to include embedded CSS reference only once?

查看:149
本文介绍了ASP.NET自定义控件 - 什么是只包括一次嵌入CSS参考的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我嵌入CSS文件到一个自定义的控件库与几个控制。我想分享的所有控件相同的CSS文件,无论他们有多少实例是一个给定的形式。当多个控制的形式,我想正是1参考ASP.NET页面的HTML头中的CSS文件。

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

好吧,它的工作原理......但这里的明显缺陷是使用中的FindControl(),看是否表上存在的控制。虽然我使用的命名容器,它似乎仍然可以工作,我相信有一些方法来打破这一点。添加具有相同ID窗体上的另一个控制肯定是1 ...

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...

问题:什么是更好的方式来确保头控制仅添加到HTML头只出现一次。

The Question: What is a better way to ensure the header control is only added to the HTML header exactly once?

注意: ClientScript.RegisterClientScriptResource()方法具有一个接受.NET类型的参数和该类型可以用于确保code是仅输出每一次页面。不幸的是这种方法只适用于JavaScript文件的引用。如果有一个内置的等值CSS参考,这将是我的preference。

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.

更新:

我发现了一个稍微更优雅的方式来做到这一点使用Page.ClientScript这里。的RegisterClientScriptBlock并告诉它要包括自己的脚本标签,然而由于里克指出,这并不脚本添加到HTML头标记,也没有符合XHTML。

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.

更新2:

我看到<一个又一个有趣的想法href=\"http://stackoverflow.com/questions/2366605/asp-net-how-to-include-css-only-if-it-isnt-alrealy-included\">this螺纹,而是通过控件集合循环并不是一个很好的解决方案,并增加了大量的开销,如果你有多个引用和几个控件的页面上。

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.

克里斯·莱弗利想出了一个更好的解决方案只需要较少的code。这里是我的功能改变与新的解决方案:

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

几件事情要注意的这一解决方案。在他原来的职位,克里斯使用的是不是到的RegisterClientScriptBlock()方法的相应方法的 IsClientScriptIncludeRegistered()方法。要正确地使该功能,测试必须以 IsClientScriptBlockRegistered进行()

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().

此外,特别注意传递给的RegisterClientScriptBlock类型()。我通过一个自定义的数据类型,这种方法(在所有我控制的一样),但它并没有以这样的方式使 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.

虽然无可否认该解决方案感觉有点像一个黑客,它)并不需要很多code或架空,B)正是产生在页面上所需的输出,以及c)是XHTML兼容code。

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.

推荐答案

要prevent复制从服务器控件发射的CSS文件时,我们做到以下几点:

To prevent duplication when emitting css files from server controls, we do the following:

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);
}

首先我们测试,看看命名剧本pviously注册$ P $。如果没有,我们将它添加。

First we test to see if the named script was previously registered. If not, we add it in.

这篇关于ASP.NET自定义控件 - 什么是只包括一次嵌入CSS参考的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆