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#中推荐使用。


转载自 沁园春 www.qinychun.com
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值