1

Is there any way to inject JQuery into every page on an ASP.Net site without adding the script tag to every page individually?

1
  • well, As far as I know with out a script tag, it is impossible to add javascript Commented Oct 10, 2010 at 3:58

3 Answers 3

3

Using Master Page is easy way to do that. But if you already constructed your site, implementing this with master page may be mass of work. Instead of that you can create BasePage class which is inherited System.Web.UI.Page as below:

Check this out

public class BasePage:System.Web.UI.Page
    {

        protected override void OnInit(EventArgs e)
        {
            // Define the name, type and url of the client script on the page.
            String csname = "ButtonClickScript";
            String csurl = "~/script_include.js";
            Type cstype = this.GetType();

            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;

            // Check to see if the include script exists already.
            if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
            {
                cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
            }

            base.OnInit(e);
        }
    }

Every page which derived from BasePage includes that script source dynamically. (see below)

public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Some code ...
        }
    }

But, if you didn't create your site yet, Master Page is the easiest way to do that.

Sign up to request clarification or add additional context in comments.

Comments

3

Use Master Page. Inject the JQuery into MasterPage only, all your aspx pages will use the MasterPage.

Comments

0

use document.write

1 Comment

use the document.write in the js file and embed it in you aspx, using <script src....></script>

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.