asp.net - 将 &lt;script&gt; 标签注入(inject)到代码后面的 <head> 中

标签 asp.net global-asax code-behind

我需要找到一种全局方法来注入(inject) <script>直接在<head>内标记到我的页面来自后面代码的标记。我可以在 global.asax 中做些什么吗?如果没有,我的所有 aspx 页面都会继承一个类,我有一个实现 System.Web.UI.Page 的“BasePage”。

基本上,我需要一种方法在引用任何其他脚本(在 HEAD 中)“之前”将 jQuery 推送到我的所有页面中。

最佳答案

protected void Application_EndRequest(object sender, EventArgs e)
{
    if (result)
    {
        Page page = HttpContext.Current.CurrentHandler as Page;
        if (page is Page)
        {
            page.ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "HeadScript",
            "<script>alert('error')</script>");
        }
    }
}

也许上面的东西适合你。 Application_EndRequestGlobal.asax 中定义。

关于asp.net - 将 &lt;script&gt; 标签注入(inject)到代码后面的 <head> 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7263001/

相关文章:

php - 在 .net 应用程序中实现 Speller 页面时如何解决错误[不允许用于访问路径的 HTTP 动词 POST]

c# - 单击按钮时使用 JS 更改 asp :HiddenField. 值

c# - ASP.NET MVC session 字典

Javascript:强制aspx父页面服务器刷新

.net - ASP.NET 2.0编译错误: Make sure that the class defined in this code file matches the 'inherits' attribute

c# - 将样式表应用于 gridview 模板字段

javascript - 使用 JavaScript 重定向

Global.asax 中的 Asp.net System.Web.HttpContext.Current.Session null

asp.net - 在 Ninject 模块中初始化 EF DbContext 数据库

c# - 从后面的代码设置 TextBox 的字体