在线等:在asp.net母版页中如何实现动态从内容页加载js文件? 本帖最后由 kv4000 于 2014-06-20 19:14:34 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Refer here:http://www.cnblogs.com/insus/archive/2009/03/26/1422675.htmlhttp://www.cnblogs.com/insus/archive/2009/12/13/1623116.html Refer here:http://www.cnblogs.com/insus/archive/2013/05/30/3109486.html方法相关:http://www.cnblogs.com/insus/archive/2013/04/17/3025905.html 你可以创建一个“空的”用户控件,例如叫做 IncludeMyJs.ascx,如下<%@ Control Language="C#" AutoEventWireup="true" CodeFile="IncludeMyJs.ascx.cs" Inherits="IncludeMyJs" %>using System;using System.IO;using System.Web.UI;public partial class IncludeMyJs : System.Web.UI.UserControl{ protected void Page_Load(object sender, EventArgs e) { var file = new FileInfo(Request.PhysicalPath); var jsPath = ResolveUrl("~/js/" + file.Name + ".js"); ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType(), jsPath, jsPath); }}然后,哪个页面需要这个功能,就用鼠标把ascx拖入相应的aspx的设计页面上即可! 如果要判断一下文件存在才注册js文件引用,可以这样写using System;using System.IO;using System.Web.UI;public partial class IncludeMyJs : System.Web.UI.UserControl{ protected void Page_Load(object sender, EventArgs e) { var file = new FileInfo(Request.PhysicalPath); var virtualPath = "~/js/" + file.Name + ".js"; if (new FileInfo(Server.MapPath(virtualPath)).Exists) { var jsPath = ResolveUrl(virtualPath); ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType(), jsPath, jsPath); } }} 阅读好的代码真是享受,又学习了两个关于文件操作的方法!就用鼠标把ascx拖入相应的aspx的设计页面上即可!其实和手动拖个js文件到aspx页面有什么区别呢?我目前就是不想往每个aspx页面添加js引用,所以想偷懒在masterpage中实现! protected void Page_Load(object sender, EventArgs e) { string jsPath = string.Format("{0}.js", System.IO.Path.GetFileNameWithoutExtension(Request.FilePath)); jsPath = ResolveUrl(jsPath); Header.Controls.Add(RegisterScript(jsPath)); } public HtmlGenericControl RegisterScript(string src) { System.Web.UI.HtmlControls.HtmlGenericControl script = new System.Web.UI.HtmlControls.HtmlGenericControl(); script.TagName = "script"; script.Attributes.Add("type", "text/javascript"); script.Attributes.Add("src", src); return script; } 试试这个:string path = Request.Url.AbsolutePath; System.IO.FileInfo fi = new System.IO.FileInfo(path); return fi.Name; 如何改变gridview里面被选中行的颜色 关于后台遍历获取Gridview中控件的的值 iBatis有字符长度限制吗,是Oracle 10g数据库 GridView自定义模版TextBox里面的text='<%#Eval("[Name]")%>'如何写在后台? windows 2003 server+iis6.0+asp.net 2.0上传大文件问题 问个关于Page_Load的问题 急问,为什么CSS丢失,而且网页不跳转 求大虾解答.~~~送分70,+刚才那张帖子的30.搞定后,一起结了. 同一个页面多个视频的问题--请高人指点 SQL语句的问题 显示隐藏控件问题 服务器图片无法显示
http://www.cnblogs.com/insus/archive/2009/03/26/1422675.html
http://www.cnblogs.com/insus/archive/2009/12/13/1623116.html
http://www.cnblogs.com/insus/archive/2013/05/30/3109486.html方法相关:
http://www.cnblogs.com/insus/archive/2013/04/17/3025905.html
using System;
using System.IO;
using System.Web.UI;public partial class IncludeMyJs : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var file = new FileInfo(Request.PhysicalPath);
var jsPath = ResolveUrl("~/js/" + file.Name + ".js");
ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType(), jsPath, jsPath);
}
}然后,哪个页面需要这个功能,就用鼠标把ascx拖入相应的aspx的设计页面上即可!
using System.IO;
using System.Web.UI;public partial class IncludeMyJs : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var file = new FileInfo(Request.PhysicalPath);
var virtualPath = "~/js/" + file.Name + ".js";
if (new FileInfo(Server.MapPath(virtualPath)).Exists)
{
var jsPath = ResolveUrl(virtualPath);
ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType(), jsPath, jsPath);
}
}
}
就用鼠标把ascx拖入相应的aspx的设计页面上即可!其实和手动拖个js文件到aspx页面有什么区别呢?我目前就是不想往每个aspx页面添加js引用,所以想偷懒在masterpage中实现!
protected void Page_Load(object sender, EventArgs e) {
string jsPath = string.Format("{0}.js", System.IO.Path.GetFileNameWithoutExtension(Request.FilePath));
jsPath = ResolveUrl(jsPath);
Header.Controls.Add(RegisterScript(jsPath));
}
public HtmlGenericControl RegisterScript(string src) {
System.Web.UI.HtmlControls.HtmlGenericControl script = new System.Web.UI.HtmlControls.HtmlGenericControl();
script.TagName = "script";
script.Attributes.Add("type", "text/javascript");
script.Attributes.Add("src", src);
return script;
}
string path = Request.Url.AbsolutePath;
System.IO.FileInfo fi = new System.IO.FileInfo(path);
return fi.Name;