通过使用ScriptManager.RegisterStartupScript,呈现后台多次使用alert方法

在前台HTML中加入alert或者confirm,相信大家已经非常熟悉并且经常使用:

1 <div onclick="alert('hello')">按钮1</div>
2 <asp:Button ID="Button1" runat="server" Text="按钮2" OnClientClick="return confirm('确定提交吗?')" />

在后台C#中也同样知道如何使用:

 using System.Web.UI;

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "id", "<script>alert('hello');</script>", false);

参数介绍:
1)Contrl类型:注册脚本的控件

2)Type类型:注册脚本控件类型

3)string类型:标识脚本块的唯一键

4)string类型:发送到客户端的脚本的内容

5)bool类型:是否添加<script></script>标签

如果经常使用,或多次使用,那就可以写成一个方法:

public void alertMsg(string msg)
{
    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>", false);
}

或者,就封装一下它吧……

原文地址:https://www.cnblogs.com/kandyvip/p/3173413.html