2010-05-13学习笔记:Asp.net 2.0 动态注册脚本的四种常用方法

动态注册JS脚本时,来自ClientScriptManager类的,常用的四种方法有:

 

(一)、RegisterClientScriptBlock方法

  将js区块添加到页面顶端,你可以以字符串形式创建这些js代码,然后将它传递给添加网页的方法。相对应的,判断是否已注册该脚本的方法为IsClientScriptBlockRegistered()

前台相关代码:

ExpandedBlockStart.gif 代码
< body >
    
< form id = " form1 "  runat = " server " >
        
< div style = " text-align: center; " >
            
< div style = " width: 600px; padding: 20px 50px 20px 50px; " >
                
< table style = " width: 100%; "   class = " Tb_Blank " >
                    
< tr >
                        
< td >
                            ClientScriptManager类常用的四种方法
                        
</ td >
                    
</ tr >
                    
< tr >
                        
< td >
                            
< table style = " width: 100%; "   class = " Tb_Common " >
                                
< tr >
                                    
< td style = " width: 35%; " >
                                        RegisterClientScriptBlock方法
                                    
</ td >
                                    
< td style = " width: 65%; " >
                                        
< asp:Button ID = " btnTest "  runat = " server "  Text = " 检测 "  OnClick = " btnTest_Click "   />
                                    
</ td >
                                
</ tr >
                            
</ table >
                        
</ td >
                    
</ tr >
                
</ table >
            
</ div >
        
</ div >
    
</ form >
</ body >

 

后台相关代码:

ExpandedBlockStart.gif 代码
         #region  JS注册方法检测

        
private   void  TestScriptRegister()
        {
            
// 动态创建js文件 
               string  info  =   @" function showDialog(){alert('测试RegisterClientScriptBlock');} " ;
            
// 判断myName是否已经被注册 
               if  ( ! Page.ClientScript.IsClientScriptBlockRegistered( this .GetType(),  " myName " ))
            {
                
// 动态注册js 
                Page.ClientScript.RegisterClientScriptBlock( this .GetType(),  " myName " , info,  true );
            }
            
this .btnBlock.Attributes[ " OnClick " =   " showDialog() " ;
        }

        
#endregion

        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
            {
                  TestScriptRegister();
            }
        }

 

查看页面源代码为:

ExpandedBlockStart.gif 代码
< body >
    
< form name = " form1 "  method = " post "  action = " ForLinKeNan.aspx "  id = " form1 " >
< div >
< input type = " hidden "  name = " __VIEWSTATE "  id = " __VIEWSTATE "  value = " /wEPDwUKMTQ2OTkzNDMyMQ9kFgICAw9kFgICAQ8PZBYCHgdPbkNsaWNrBQxzaG93RGlhbG9nKClkZA0HIswlstW+qXADqAH+bdo4hzp+ "   />
</ div >


< script type = " text/javascript " >
// <![CDATA[
function showDialog(){alert( ' 测试RegisterClientScriptBlock ' );} // ]]>
</ script >

< div >

    
< input type = " hidden "  name = " __EVENTVALIDATION "  id = " __EVENTVALIDATION "  value = " /wEWAgLJ9K2vDwK14deQBeSN2qREk5Pf28+KMun3E4nhjvXE "   />
</ div >
        
< div style = " text-align: center; " >
            
< div style = " width: 600px; padding: 20px 50px 20px 50px; " >
                
< table style = " width: 100%; "   class = " Tb_Blank " >
                    
< tr >
                        
< td >
                            ClientScriptManager类常用的四种方法
                        
</ td >
                    
</ tr >
                    
< tr >
                        
< td >
                            
< table style = " width: 100%; "   class = " Tb_Common " >
                                
< tr >
                                    
< td style = " width: 35%; " >
                                        RegisterClientScriptBlock方法
                                    
</ td >
                                    
< td style = " width: 65%; " >
                                        
< input type = " submit "  name = " btnBlock "  value = " 检测 "  onclick = " showDialog(); "  id = " btnTest "   />
                                    
</ td >
                                
</ tr >
                            
</ table >
                        
</ td >
                    
</ tr >
                
</ table >
            
</ div >
        
</ div >
    
</ form >
</ body >

 

附注:使用RegisterClientScriptBlock,其JS函数,放置在页面的顶部,一般位于form后

 

(二)、RegisterStartupScript方法

  将js区块添加当网页完成加载,但是在引发网页的onload事件之前,就会执行。相对应的,判断是否已注册该脚本的方法为IsStartupScriptRegistered。

     该方法的使用,可类似上述的RegisterClientScriptBlock。唯一不同的是,相应的函数在页面底部,位于form结束标志前。

 

(三)、RegisterClientScriptInclude方法

  通过外部添加引用js脚本。相对应的,判断是否已注册该脚本的方法为IsClientScriptIncludeRegistered()

 参照(一),前台相关代码:

     < asp:Button ID = " btnInclude "  runat = " server "  Text = " 检测 "  OnClientClick = " return showInclude(); "   />

 

ClientScriptManager.js函数代码为:

    function showInclude()
    {
        alert(
' 检测RegisterClientScriptInclude方法 ' );
    }

 

后台相关代码为:

ExpandedBlockStart.gif 代码
        #region  检测RegisterClientScriptInclude方法

        
if  ( ! ClientScript.IsClientScriptIncludeRegistered( this .GetType(),  " Include " ))
       {
              
// 若没有被注册,则将ClientScriptManager.js动态注册,注意路径
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(),  " Include " , Page.ResolveUrl( " ~/JS/ClientScriptManager.js " ));
       }

       
#endregion

 

(四)、RegisterOnSubmitStatement方法

  添加执行以响应网页onsubmit事件的js。相对应的,判断是否已注册该脚本的方法为IsOnSubmitStatementRegistered

前台相关代码:

        < asp:Button ID = " btnStatement "  runat = " server "  Text = " 检测 "   />

 

后台相关代码:

ExpandedBlockStart.gif 代码
        #region  检测RegisterOnSubmitStatement方法

        
string  infoSubmit  =   " return confirm('你确认删除?'); " ;
       
if  ( ! Page.ClientScript.IsOnSubmitStatementRegistered( this .GetType(),  " Submit " ))
       {
            Page.ClientScript.RegisterOnSubmitStatement(
this .GetType(),  " Submit " , infoSubmit);
       }

       
#endregion

 

 

 

 

转载于:https://www.cnblogs.com/lybohe0807/archive/2010/05/13/1734674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值