2010-10-10 37 views
1

有没有任何方法可以将JQuery注入到ASP.Net站点上的每个页面中,而无需单独向每个页面添加脚本标记?将jquery插入到每个ASP.Net页面

+0

那么,据我所知,用脚本标记,不可能添加javascript – 2010-10-10 03:58:29

回答

3

使用母版页是简单的方法。但是,如果您已经构建了您的网站,那么使用母版页实施这可能是大量工作。取而代之的是,你可以创建一个继承System.Web.UI.Page如下BasePage类:

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); 
     } 
    } 

从BasePage的派生每个页面包含动态该脚本来源。 (见下)

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

但是,如果你还没有创建你的网站,主页是最简单的方法来做到这一点。

3

使用Master Page。只将JQuery注入到MasterPage中,所有的aspx页面将使用MasterPage。

0

使用document.write

+0

从哪里调用? – JoelFan 2010-10-10 11:16:08

+0

使用js文件中的document.write并将其嵌入到你的aspx中,使用 X10nD 2010-10-11 09:37:58

相关问题