开发者

inject jquery into every ASP.Net page

Is there any wa开发者_运维知识库y to inject JQuery into every page on an ASP.Net site without adding the script tag to every page individually?


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.


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


use document.write

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜