在asp.net项目中,有时候会用到后台直接调用前台js方法或者后台输出js脚本等操作。

例如在点击保存按钮的时候,希望弹出一个js的保存成功。

在旧版本的vs中使用如下:

 this.RegisterStartupScript(“msg”, “alert(‘测试!’);”);

在vs2008以上版本中会提示以上方法已过时,当然也可以使用。它对应的有了替代方法,如下:

ClientScript.RegisterStartupScript(this.GetType(), “msg”, “alert(‘测试弹出!’);”);

与此同时会有如下操作:

window.open的一个页面,点击保存后,想要关闭当前窗体,并刷新父窗体,则使用如下代码:

ClientScript.RegisterStartupScript(this.GetType(), “msg”, “alert(‘保存成功!’);opener.location.href=opener.location.href;window.close();”);