html 在body末尾追加,在/ BODY标签末尾呈现自定义HTML /脚本

Solutions2

You can put JavaScript near the end of your page using the Page.CleintScript.RegisterStartupScript method. This method places the JavaScript at the end of the form tag.

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). The last argument (if true) makes the framework include the script tags so you don't have to put them into your code string.

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.

// 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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值