Quantcast
Channel: World Wide What? » Downloads
Viewing all articles
Browse latest Browse all 2

Add JavaScript to The Tag from your UserControls

$
0
0

I’ve developed quite a lot of UserControls in my days and I’ve always been annoyed by the ugly way to add JavaScript to the page header. So I took matter in my own hands and created yet another UserControl, this control will make it easier for you to add JavaScript from your current UserControls though.

The features if this control is:

  • Write syntax highlighted JavaScript directly in your .ascx file.
  • Allow only the same script once in the header even if you have multiple UserControls of the same type.
  • Easy to use, lightweight and no code behind required.

Example:

First you need to register the UserControl as you do with a regular UserControl. You do this either in the UserControls you wish to use it in or in the web.config if you want to be able to use it everywhere.

<%@ Register Src="~/HeaderScripts.ascx" TagPrefix="uc" TagName="HeaderScripts" %>

Next you add the script to you ascx, the SingleInstance property tells the control that only one HeaderScript are allow with the id specified in the ScriptID property. Remove the SingleInstance and ScriptID property if you don’t want the script to be unique.

<uc:HeaderScripts ID="script1" SingleInstance="true" ScriptID="s1" runat="server">
    <script>

        alert('Hello from Script 1');

    </script>
</uc:HeaderScripts>

That’s about it, quite simple to use huh?

Download

You can download the UserControl and a complete example here.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images