Page.ClientScript.RegisterClientScriptBlock 有时在前台页面无法注册的可能原因

原因一:
  前台页面缺少<form id="form1" runat="server"></form>标签。加上即可。

原因二:
  如果一个页面顺序执行多个Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "js function")且 RegisterClientScriptBlock方法的第二个参数为脚本的Key都为空或为相同的值,则只执行第一个脚本。此时把key区别开即可。

后台执行js脚本的例子:

protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ThisIsAKey", "AlertWhenLoad();", true);
        }

前端页面有对应的js函数:

<script src="../Scripts/toastr.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    function AlertWhenLoad() {
            toastr.error('Alert When Load');
        }
</script>

效果图:

 

posted @ 2024-01-31 15:04  SilverFox8588  阅读(21)  评论(0编辑  收藏  举报