ClientScriptManager.RegisterStartupScript的使用

因为ClientScriptManager是一个密封类,因为平时编程中很少用到密封类,一时用常规的思维去使用,不成功,
的使用,后来我查了msdn,ClientScriptManager cs = Page.ClientScript;  但是密封类还和常规类一样的使用方法只是这个类没有构造函数,不能按照常规方法使用。

public   void  Page_Load(Object sender, EventArgs e)
  {
    
//  Define the name and type of the client scripts on the page.
    String csname1  =   " PopupScript " ;
    String csname2 
=   " ButtonClickScript " ;
    Type cstype 
=   this .GetType();
        
    
//  Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs  =  Page.ClientScript;

    
//  Check to see if the startup script is already registered.
     if  ( ! cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 
=   " alert('Hello World'); " ;
      cs.RegisterStartupScript(cstype, csname1, cstext1, 
true );
    }

    
//  Check to see if the client script is already registered.
     if  ( ! cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 
=   new  StringBuilder();
      cstext2.Append(
" <script type="text/javascript"> function DoClick() { " );
      cstext2.Append(
" Form1.Message.value='Text from client script.'} </ " );
      cstext2.Append(
" script> " );
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), 
false );
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值