More in this section

Forums / Developing with Sitefinity / How to create a single instance of CssFileLink

How to create a single instance of CssFileLink

2 posts, 0 answered
  1. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    07 Jul 2010
    Link to this post
    Is there some way to ensure only a single link to a CSS file is inserted into the head controls collection when I use the CssFileLink control?  For example, I can check for a registered client script with:

    if (!this.Page.ClientScript.IsClientScriptBlockRegistered("SomeValue1"))
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValue1",
            arrayDeclaration.ToString(),
            true);

    There must be some way to do this with the CssFileLink control, right?  I've tried looking at the HtmlLink controls in the page head, but it's not there.  I don't really want to recursively process the page controls looking for one of these--there may be a large number of the user controls on the page that need to do this. 

    Thanks,
    Mike
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    07 Jul 2010
    Link to this post
    Hi Mike,

    It is not possible to identify whether a given script has been added by JsFileLink control ( CssFileLink adds import or link attributes) without looping through header controls collection. JsFileLink and CssFileLink controls do not make such check before adding a given tag inside Render method( for embedded scripts ) or CreatechildControls( for custom scripts). Using IsClientScriptBlockRegistered would be useful if it is done inside one of the methods above with conjunction of RegisterClientScriptBlock
    You could override Render of InternalPage, but here you should loop through the controls collection again.

    Basically you could create your own custom control that acts in this way

    protected override void CreateChildControls()
     {
       
       if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "Block")
       {
         var scriptText = new StringBuilder();
         scriptText.Append("<script type=\"text/javascript\"> function click() {");
         scriptText.Append("Form1.Message.value='Hello'} </");
         scriptText.Append("script>");
         Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Block", scriptText.ToString());
       }
     }


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered