在C#中可以使用RegisterClientScriptBlock、 RegisterStartupScript和RegisterClientScriptInclude来向客户端注册脚本,它们的区别是:
RegisterClientScriptBlock:在页面内容前注册脚本;
RegisterStartupScript:在页面内容后注册脚本;
RegisterClientScriptInclude:在页面内容前引用外部脚本;
例如下边脚本:
![向客户端注册脚本 - 天新 - 天新的博客 程序代码](https://i-blog.csdnimg.cn/blog_migrate/58c53beb33fc757b1f6036e6c94a1479.gif)
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "a01", "<script>alert('a01');</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "a02", "alert('a02');",true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "b01", "<script>alert('b01');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "b02", "alert('b02');", true);
Page.ClientScript.RegisterClientScriptInclude("c01", "http://www.mzwu.com/js/my.js");
}
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "a01", "<script>alert('a01');</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "a02", "alert('a02');",true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "b01", "<script>alert('b01');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "b02", "alert('b02');", true);
Page.ClientScript.RegisterClientScriptInclude("c01", "http://www.mzwu.com/js/my.js");
}