Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include jQuery in ASP.net page?

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:

  • calling this.Page.ClientScript.RegisterClientScriptInclude during Render
  • calling this.Page.ClientScript.RegisterStartupScript during Render
  • calling this.Page.ClientScript.RegisterStartupScript during Page_Load
  • calling this.Page.ClientScript.RegisterStartupScript during a button click
  • calling Page.Header.Controls.Add(new LiteralControl( { Text = "<script type=\"text/javascript\" src=\"...\"></script>";);
  • simply including a <SCRIPT> element in your userControl.ascx file
  • calling RegisterClientScriptBlock during OnPreRender

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" %>
like image 466
Ian Boyd Avatar asked May 28 '12 15:05

Ian Boyd


People also ask

Can we use jQuery in ASP NET?

JQuery is a JavaScript library. It is helpful and make easy to handle HTML DOM (Document Object Model), Events and Animation and Ajax functionalities. JQuery reduce code compared to JavaScript. Mostly we use JQuery or JavaScript for client side activities and make Ajax call to ASP.NET Web form/mvc, Web service and WCF.

How do you add jQuery to a Web page?

Like any other JavaScript library, jQuery is added to a webpage by placing the script tag inside the head tag with the src of the script tag pointing towards the jQuery library, as shown below. There are two main ways to point the src towards the jQuery library: Downloading the jQuery library.

HOW include jQuery in ASP NET MVC?

After the ASP.NET MVC Framework is installed a new ASP.NET MVC Web Application project should be created. Next, download jQuery, get the Packed or Minified version and place it into the Content folder of the the new web application project. Add a reference to the jQuery file put in the Content folder.


3 Answers

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>
like image 134
Waqas Anwar Avatar answered Oct 25 '22 04:10

Waqas Anwar


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.

like image 31
Echilon Avatar answered Oct 25 '22 05:10

Echilon


[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.

like image 27
paulH Avatar answered Oct 25 '22 06:10

paulH