12

i have an ASP.net UserControl that requires the containing page to include a reference to jquery.

In the olden days, i would simply have included a reference to jQuery in the containing page:

<HEAD>
   <SCRIPT type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></SCRIPT>
</HEAD>

But my UserControl's dependency on jQuery is an internal implementation detail, that should not be leaking to the outside. How can my userControl dictate that jQuery be included in the final page?


Researching this, i find a lot of confused solutions, calling different functions at different times. i hesitate to mention any of them, because people might think that any of them are valid. i am hoping for the correct answer, not an answer that works.

Different solutions involve calling:

My confusion is centered around:

  • when would i want to use RegisterClientScriptInclude vs RegisterStartupScript?
  • when would i want to call it during Page_Load vs Render vs PreRender vs a button click?
  • how do i give RegisterXxxxScriptXxx the path to "Scripts/jquery-1.7.2.min.js"?

Short version: How do i convert

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %><!DOCTYPE html>
<html>
<head runat="server">
    <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>

for use in a UserControl:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MySuperCoolControl.ascx.cs" Inherits="Controls_MySuperCoolControl" %>
11
  • If I was made a custom control, I will place a boolean variable that base on it my control automatically register or not the full jQuery library. That way I left the final user to decide how it will like to include the jQuery. My custom control automatic way, or his way. Commented May 28, 2012 at 15:30
  • You're confused because you're not considering that each solution you've mentioned handles different situations (more or less). For instance, something like a framework include won't take place in a page load (while initiating caching images would, which could rely on the framework). Commented May 28, 2012 at 15:33
  • I'd say the only three to consider are 1, 5 and 7. The others are either instance or event-based situations, which is not where a framework should be included. Commented May 28, 2012 at 15:39
  • Also, this might be useful to review: developer.yahoo.com/performance/rules.html I'm not a "script tags in the header are horrible anti-patterns" nazi, but at a certain scale, it does make sense. Commented May 28, 2012 at 15:40
  • 1
    @Joze I just forced any page hosting the UserControl to include jQuery themselves. I'm sure WebForms has a very nice intended way to handle this; i just couldn't be bothered to try to guess what it was. Commented Apr 27, 2015 at 18:45

4 Answers 4

3

You can use google hosted jquery as follows:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a ScriptManagerProxy on the UserControl and a ScriptManager on the parent or master page.

See How Do You Use ScriptManagerProxy In a custom ASP.NET Control

This would take care of "Giving RegisterXxxxScriptXxx the path to "Scripts/jquery-1.7.2.min.js" and remove the need to worry about it during the Page_Load/Page_PreRender events.

As for "When would i want to use RegisterClientScriptInclude vs RegisterStartupScript?"

RegisterClientScriptInclude registers an external JS file to be included in the page. RegisterStartupScript includes a block of inline executable script in the page, which is not in an external file.

Comments

1

[this is only relevant if your user control is to be used in-house. If it's for distribution then it won't be of much help]

Take a look at this link:

http://www.codeproject.com/Articles/196727/Managing-Your-JavaScript-Library-in-ASP-NET

The article suggests creating methods to generate references to javascript libraries such as jQuery, so that if you want to use it in a page you simply call JavascriptLoader.IncludeJQuery() [or whatever you have called your method].

Now what I have done is to take it a step further by creating those methods in an assembly that I have placed in the GAC so that it is available to all my .net web applications. Now, wherever I want to use jQuery, that method is already available. The best thing being that if I call the method in a user control, and call it again in another user control, and again on the page, it still only registers the library once. If I decide to upgrade to a newer version of jQuery, I just change my dll, and it's changed everywhere.

Comments

0

If your control is referenced from a separate project, you can embed the javascript in the other assembly. See this old but good example by Scott Mitchell:

https://web.archive.org/web/20211020131200/https://www.4guysfromrolla.com/articles/080906-1.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.