简体   繁体   English

在/ BODY标记的末尾渲染自定义HTML /脚本

[英]Render custom HTML/Script at end of /BODY tag

I have the following BasePage class which all pages on the site inherit directly or via a subclass. 我有以下BasePage类,该站点上的所有页面都直接或通过子类继承。 I am overriding the Render method so it outputs a script on every page of the site. 我覆盖了Render方法,因此它在站点的每个页面上输出一个脚本。 The code currently writes the script after the closing /HTML tag. 当前代码在结束/ HTML标记之后编写脚本。

public class BasePage : Page
{
....
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
            base.Render(writer);
            Response.Write("<Script>custom</script>");
    }
}

Subclass example: 子类示例:

public class AdminPage : BasePage {}

How can I get this to write the script before the closing /BODY tag or /FORM tag? 我如何在关闭/ BODY标记或/ FORM标记之前编写脚本? I've tried different variations of RegisterStartUpScript but none of them work globally, ie, I found many cases where the script would not render at all, so I've fallen back to using Response.Write to guarantee it would render the script. 我尝试了RegisterStartUpScript不同变体,但是它们都无法全局运行,即,我发现很多情况下脚本根本无法渲染,因此我不得不使用Response.Write来保证它可以渲染脚本。

Your method accepts the writer . 您的方法接受writer You'd have to use that to write to the stream, not using Response object. 您必须使用它来写入流,而不使用Response对象。

Documentation page 文档页面

However, I believe you should do this using a (separate? inherited?) MasterPage, not injecting via C#. 但是,我相信您应该使用(单独的?继承的?)MasterPage来做到这一点,而不是通过C#进行注入。 It's much more transparent and much, much easier ;) 它更加透明,而且非常容易;)

You can put JavaScript near the end of your page using the Page.CleintScript.RegisterStartupScript method . 您可以使用Page.CleintScript.RegisterStartupScript方法将JavaScript放在页面的Page.CleintScript.RegisterStartupScript This method places the JavaScript at the end of the form tag. 此方法将JavaScript放在form标签的末尾。

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), 
        "MyJavaScript", "custom", true);
}

The first argument must be the type of the current page (note that it will sometimes fail if it is not). 第一个参数必须是当前页面的类型(请注意,有时它会失败)。 The second is a key that uniquely identifies this script on the page (so you can add/update more than one). 第二个是可在页面上唯一标识此脚本的键(因此您可以添加/更新多个脚本)。 The third argument is a string that contains your code (you can use a StringBuilder to make that part easier). 第三个参数是包含您的代码的字符串(您可以使用StringBuilder该部分)。 The last argument (if true) makes the framework include the script tags so you don't have to put them into your code string. 最后一个参数(如果为true)使框架包含script标记,因此您不必将它们放入代码字符串中。

It works fine from a base class as long as you pass the System.Web.UI.Page object to a method defined in the base class. 只要将System.Web.UI.Page对象传递给基类中定义的方法,它就可以在基类中正常工作。

// In base class
protected void LoadJavaScript(System.Web.UI.Page page)
{
    string key = "MyJavaScript";
    if (page.ClientScript.IsStartupScriptRegistered(key))
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine("alert('hello');");

        page.ClientScript.RegisterStartupScript(page.GetType(), 
            key, sb.ToString(), true);
    }
}

// In subclass
protected void Page_Load(object sender, EventArgs e)
{
    this.LoadJavaScript(this.Page);
}

Per MSDN , you also need to ensure you have a unique key for each piece of JavaScript you register. 根据MSDN ,您还需要确保每个注册的JavaScript都有唯一的密钥。

A startup script is uniquely identified by its key and its type. 启动脚本由其键和类型唯一标识。 Scripts with the same key and type are considered duplicates. 具有相同键和类型的脚本被视为重复脚本。 Only one script with a given type and key pair can be registered with the page. 页面上只能注册具有给定类型和密钥对的一个脚本。 Attempting to register a script that is already registered does not create a duplicate of the script. 尝试注册已注册的脚本不会创建该脚本的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM