Accessing JQuerry or other scripts within MasterPages

I’ll preface this with the fact that I dabble in ASP.NET. It’s not my passion but I do enjoy working with it from time to time. As with anything this language is susceptible to the “use it or lose it” paradigm which I often experience between projects.  To this point I was hooking up a basic site the other day and the UI designer had provided me with a template of how the basic site should work from a UX standpoint. My role was to take this as the starting point and then flesh out the rest of the site with the business logic/code needed for the web app.

 Where did I place that reference?

I started out by using masterpages to store some of the common elements such as the header and footer of the page, CSS files etc. However in this project UI designer had used JQuery to dictate the look navigation of the sites main toolbar. As I added my references to the JS files and compiled the site I found that at run time the references to the paths were being lost in the masterpage.The end result… broken nav aka no access to the JQuery library from master pages.

The solution:

The solution lies within the use of the ASP.NET Script manager tags. When working with ASP.NET remember that server-side controls are renamed as they are rendered this is why when we view source we see ct100_text….. Through the use of the scriptmanager we can help to ensure that the references to library files or other resources is not lost during the compiling of the page. 

In my example I was able to point to the JQuery libraries by using the ScriptManager  as follows:

<asp:ScriptManager ID="ScriptManager" runat="server">

<Scripts>

<asp:ScriptReference Path="~/Scripts/jquery-1.4.2.min.js" />

<asp:ScriptReference Path="~/Scripts/jquery-ui-1.8.6.custom.min.js" />

</Scripts>

</asp:ScriptManager>

Be sure to remember that the script manager tag has to be placed within side of a FORM tag. So don’t add this to the header place it within the body of the html.

Of course there are many other uses for the script manager especially with regards to AJAX. However for those of you who have tried to use JS files or other scripting syntax within masterpages to no avail. Hopefully this will help.. Happy coding!

For more information on the ScriptManager check out: http://msdn.microsoft.com/en-us/magazine/cc163354.aspx#S5

Advertisement

About randallC

I've been a multimedia developer for over 13 years. Within the past two years I have slowly migrated over to full time .net development with a strong interest and passion for Silverlight, WPF and C#.NET. I've been fortunate to work in a variety of different fields ranging from sports entertainment(NASCAR) to textile, nuclear, and most recently public safety software development. During this time span my love for design, coding and technology has only grown. Fitness, sports, my wife and my two dogs take up the rest of my life when I'm not plugging away on the computer.
This entry was posted in ASP.NET AJAX. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s