在所有Asp.net站点中包含Javascript标记

在所有Asp.net站点中包含Javascript标记,javascript,asp.net,visual-studio-2010,Javascript,Asp.net,Visual Studio 2010,我在VS2010中使用asp.net。我的项目中有许多站点都使用相同的javascript标记。因此,在添加新页面时,我必须始终包含标签。那么是否有可能总是自动包含javascripttag?一个可能的问题是在添加新页面时忘记标记 使用Javascript标记,我的意思是: <script type="text/javascript" src="jsSource.js"></script> 在母版页中包含javascript文件并始终使用它是一种可能的解决方案。 如果未

我在VS2010中使用asp.net。我的项目中有许多站点都使用相同的javascript标记。因此,在添加新页面时,我必须始终包含标签。那么是否有可能总是自动包含javascripttag?一个可能的问题是在添加新页面时忘记标记

使用Javascript标记,我的意思是:

<script type="text/javascript" src="jsSource.js"></script>

在母版页中包含javascript文件并始终使用它是一种可能的解决方案。 如果未使用母版页,则创建一个基类继承自Page类,并在OnLoad事件中将脚本注册为:

ScriptManager.RegisterClientScriptBlock
(迪克莱默:我没有测试这段代码……这只是猜测)

您可以使用控件适配器来钩住页面呈现,以便将脚本引用注入到javascript文件中

查看以显示工作解决方案的源代码。此解决方案的优点是,它使用HttpModule来注册脚本。因此,您只需修改每个项目的web.config,而不必更改所有页面的基类

根据您的情况,这应该如下所示:

public class ScriptManagerAddModule : IHttpModule
{
    public void IHttpModule.Dispose() { }

    // This is where you can indicate what events in the request processing lifecycle you want to intercept
    public void IHttpModule.Init(System.Web.HttpApplication context)
    {
       context.PreRequestHandlerExecute += new EventHandler(HttpApplication_PreRequestHandlerExecute);
    }

    void HttpApplication_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication httpApplication = sender as HttpApplication;

        if (httpApplication != null)
        {
            Page page = httpApplication.Context.CurrentHandler as Page;
            if (page != null)
            {
                // When standard ASP.NET Pages are being used to handle the request then intercept the
                // PreInit phase of the page's lifecycle since this is where we should dynamically create controls
                page.PreInit += new EventHandler(Page_PreInit);
            }
        }
    }

    void Page_PreInit(object sender, EventArgs e)
    {
        Page page = sender as Page;
        if (page != null)
        {
            // ScriptManagers must be in forms -- look for forms
            foreach (Control control in page.Controls)
            {
                HtmlForm htmlForm = control as HtmlForm;
                if (htmlForm != null)
                {
                    // Look for an existing ScriptManager or a ScriptManagerProxy
                    bool foundScriptManager = false;
                    foreach (Control htmlFormChild in htmlForm.Controls)
                    {
                        if (htmlFormChild is ScriptManager || htmlFormChild is ScriptManagerProxy)
                        {
                            foundScriptManager = true;
                            break;
                        }
                    }

                    // If we didn't find a script manager or a script manager proxy then add one
                    if (!foundScriptManager)
                    {
                        htmlForm.Controls.Add(new ScriptManager());
                    }
                }
            }

            // Addition to the original code
            ScriptManager.GetCurrent().RegisterClientScriptInclude(page, typeof(ScriptManagerAddModule), "myjavascript", "jsSource.js");
            // End of addition to the original code
        }
    }
这段代码既可以确保ScriptManager控件的存在,也可以注册一个自定义javascript文件(请参阅代码底部的我的添加内容)

然后,在所有web.config文件中注册模块:

<httpModules>
    <add name="ScriptManagerAddModule" type="YourLibrary.ScriptManagerAddModule"/>
</httpModules>


请注意,您必须将模块代码放在一个类库中,由所有其他项目引用(或者至少在所有bin文件夹中都有dll,或者GAC,或者.net用来查看dll的任何位置).

您已经在使用母版页了吗?否,但是没有母版页是否有可能?第三种选择可能是使用模块…?是的,但是如果忘记继承基类?是否有机会必须将特定类继承给所有类?您可以在web.config中为所有页面指定基类。别忘了呼吸,否则你会死的;-)@奥斯卡:它在代码隐藏文件中表现如何?如果您的codebehind继承System.Web.UI.Page,此设置有效吗?它是否添加了新的继承级别(web.config->system.web.ui.page中的实际类->基类)?