views:

33

answers:

3

Like the title says, I'm trying to find out if I need to include a script library that my ASP.net UserControl needs to work. I don't want to include it multiple times per page, but I want my control to be able to be used multiple times on the same page.

How can I, in the codebehind of my control, check to see if a given <script/> tag is present?

This is .Net 2.0, no LINQ.

+1  A: 

No, there isn't.

Instead, you should maintain a set of <script> tags that have already been included, and update it whenever you add a script. (For example, a [ThreadStatic] static List<String>)

SLaks
Interesting. Is this superior to `Page.ClientScript.IsClientScriptIncludeRegistered` ? I'd love to see a short sample of how you include this.
Atømix
@Atomiton: That only works if you use `RegisterClientScriptInclude`. Basically, you would have a static class with a method called `RegisterScript` that adds the script's URL to the `[ThreadStatic]` list and returns false if the script was already included. You would then write a script tag only if `RegisterScript` returns `true`. Make sure to clear the list for every page load.
SLaks
@SLaks. So, I guess the advantage this would have would be more control over where and when your scripts are written to the page. For example, they could be written in the head of the document as opposed to the body, as Asp.Net likes to do.
Atømix
+1  A: 
If !Page.ClientScript.IsClientScriptIncludeRegistered("jQuery")
    Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");

or if you need a script block, And want to include the control's type:

if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myScript"))
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript"
                                       , "<script>alert('xx');</script>", false);
Glennular
A: 

I would suggest re-thinking your problem/solution. It seems like what you are trying to do would couple your user control and the pages that use it. This would require that a page reference that script in order to use a control.

Is there any reason you can't just put the script tag in your user control so that the pages consuming your user control don't need to know that they need to include a specific script tag to work?

I tend to think that if you want to just use a control you should be able to just use it.

Adam
This is true... but what if your control has a dependency on a js function you've written? And what if your control appears multiple times on the page?You can register the js function from within your control, but you just need to check and see if it has been previously registered.
Atømix
That's exactly why I'm doing this. It's to include hotkeys into the page on the server side. Users of my control should be able to place it into another control or anywhere they like without having to worry if a script library is already included. FYI, it's the JQuery hotkeys plugin we're talking about.If I do it your way, the library will be included multiple times if there are multiple instances of the control.
Chris McCall
I would still recommend building your control with the script reference, but to also build your control defensively so that it won't register additional instances of it's scripts when consumed on a page. Something you don't know when writing a control is not just if there are multiple instances of the control, but there may be other controls using the same script library.Edit: I'm not familiar with the jQuery hotkeys script. I would lean towards trying to make the control independent if possible and possibly consider SLaks suggestion or Atomiton's as far as implementation of the solution.
Adam