C#三种弹出信息窗口

弹出信息框,是浏览器客户端的事件。服务器没有弹出信息框的功能。

方法一:

asp.net页面如果需要弹出信息框,则需要在前台页面上注册一个javascript脚本,使用alert方法。使用ClientScript.RegisterStartupScript( )方法注册脚本。

ClientScript.RegisterStartupScript( )

RegisterStartupScript(type,key,script)

type:脚本事件的类型,一般用this.GetType()获取

key:脚本事件的名字,不能重复。

script:javascript脚本。

示例:

(1) string script=“<script>alert('注册信息')</scritp>”; ClientScript.RegisterStartupScript(this.GetType(),"success",script);

(2)信息框提示后刷新本页面。 string script=“<script>alert('注册信息');location.href=location.href</scritp>”;ClientScript.RegisterStartupScript(this.GetType(),"success",script);

(3)信息框提示后转到新页面。 string script=“<script>alert('注册信息');location.href='index.aspx'</scritp>”; ClientScript.RegisterStartupScript(this.GetType(),"success",script);

(4)在新窗口中打开新页面。string script=“<script>alert('注册信息');window.open('index.aspx')</scritp>”;ClientScript.RegisterStartupScript(this.GetType(),"success",script);

windos.open( )和window.close( )相对应,一个为打开新窗口,一个为关闭当前窗口。

总结:模态窗口。该方法为推荐方法。

因为经常使用,所以可以将该方法放入一个类中。方法是:新建网站---网站根目录右击---添加ASP.NET文件夹---选择APP_Code----右击APP_Code---添加新项---选择类,到此类文件新建完毕。

类中新建方法如下:

//弹出信息,信息内容为info

 public static  void Alert(string info, Page p)
    {
        string script = "<script>alert('"+info+"')</script>";
        p.ClientScript.RegisterStartupScript(p.GetType(),"",script);
    }

//调用该类的方法是:

类名.Alert(注册信息,this);因为该方法是静态方法,所以通过类名直接调用。如果该方法不是静态方法,需要实例化对象后在调用。实例化如下:

类名  a=new  类名();   然后调用:  a.Alert(注册成功,this);

方法二:Response.Write();

string script=“<script>alert('注册信息')</scritp>”;  Response.Write(script);

总结:模态窗口,该弹出窗口不关闭的话,网页不能操作。不建议使用,该弹出窗口会使网页变形。

方法三:MessageBox.Show("注册成功");

使用该方法之前需要做如下准备:

网站目录右击---添加引用---找到System.Windows.Forms,确定。然后在页面中添加:using System.Windows.Forms;然后在页面中使用该方法即可。

总结:C#中经常使用是模态窗口,网站(网页)中不是模态窗口,网页中不推荐使用,C#中推荐使用。

原文地址:https://www.cnblogs.com/zhisong/p/2807123.html