本帖最后由 kv4000 于 2014-06-20 19:14:34 编辑

解决方案 »

  1.   

    Refer here:
    http://www.cnblogs.com/insus/archive/2009/03/26/1422675.html
    http://www.cnblogs.com/insus/archive/2009/12/13/1623116.html
      

  2.   

    Refer here:
    http://www.cnblogs.com/insus/archive/2013/05/30/3109486.html方法相关:
    http://www.cnblogs.com/insus/archive/2013/04/17/3025905.html
      

  3.   

    你可以创建一个“空的”用户控件,例如叫做 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的设计页面上即可!
      

  4.   

    如果要判断一下文件存在才注册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);
            }
        }
    }
      

  5.   

    阅读好的代码真是享受,又学习了两个关于文件操作的方法!
    就用鼠标把ascx拖入相应的aspx的设计页面上即可!其实和手动拖个js文件到aspx页面有什么区别呢?我目前就是不想往每个aspx页面添加js引用,所以想偷懒在masterpage中实现!
      

  6.   


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

  7.   

    试试这个:
    string path = Request.Url.AbsolutePath;
                System.IO.FileInfo fi = new System.IO.FileInfo(path);
                return fi.Name;