ASP.NET2.0中Page.ClientScript.RegisterClientScriptBlock与RegisterClientScriptBlock

ASP.NET2.0中Page.ClientScript.RegisterClientScriptBlock与RegisterClientScriptBlock
2010年07月07日 星期三 18:35

既然我们可以在 HTML 元素内部嵌入部分 JavaScript,甚至可以以动态方式使用 JavaScript 和 Web 服务器控件,那么如何将全部 JavaScript 函数置于您的代码中呢?

可通过多种方法来完成此任务,我们将介绍几种可在 ASP.NET 代码中使用的较为常见的方法。在本文中,我们将介绍如何使用新的 Page.ClientScript 属性。在 ASP.NET 2.0 之前,您需要使用 RegisterStartupScriptRegisterClientScriptBlock 方法。现在,这两个方法已被淘汰。在 ASP.NET 1.x 中注册脚本的两种可能方法均需要使用一组关键字/脚本参数。由于涉及到了两个独立的方法,因此极有可能会出现一些关键字名称冲突。Page.ClientScript 属性本身就可以完成所有的脚本注册,从而使您的代码少出错。

先看一下下面的列子:

//要根据后台取的值初始化页面显示

protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             GetGovShiftSetInfo();//此方法作用:取数据库数据来设置rbTwo状态
             if (rbTwo.Checked)
             {
                 //Page.RegisterStartupScript("", "<script>istwo();</script>"); //1.0的语法构造,已过时

                 Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "istwo();", true);
             }
         }
     }

----------------------------------------------------------------------------------------

//放在前台的JS:

<script type="text/javascript">
  
      function istwo()
      {
             var   obj1=window.document.getElementById("ctl00_MainContent_panelPmB");
             var   obj2=window.document.getElementById("ctl00_MainContent_panelPmE");
             var   obj3=window.document.getElementById("ctl00_MainContent_lbAmB");
             var   obj4=window.document.getElementById("ctl00_MainContent_lbAmE");
             var   obj5=window.document.getElementById("ctl00_MainContent_panelCard");
             obj1.style.visibility = "hidden";
             obj2.style.visibility = "hidden";
             obj3.style.visibility = "hidden";
             obj4.style.visibility = "hidden";
             obj5.style.visibility = "hidden";
             var no3= parseInt(window.document.getElementById("ctl00_MainContent_txtValue").value);
             if (no3 <   5 || no3 > 500 )
              {
                     alert("上下班刷卡有效时限:[5-500]分钟内");
                  window.document.getElementById("ctl00_MainContent_rbFour").focus();
                    window.document.getElementById("ctl00_MainContent_rbFour").checked=true;
                    isfour();
             }               
      }

   --------------------------------------------------------------------------------------------------------

之所以没用Page.ClientScript.RegisterClientScriptBlock而用Page.ClientScript.RegisterStartupScript是因为RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部,用RegisterClientScriptBlock会报错:javascript函数不到对象

如果你的页面中有如下代码:

<asp:TextBox ID=”TextBox1” Runat=”server”>

Hello ASP.NET

</asp:TextBox>

 

c#

protectedvoid Page_Load(object sender, EventArgs e)


{


string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;


Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),


“MyScript”, myScript,
true);


}

此页面运行时会报错,原因是JavaScript function先于text box被安放于浏览器。因此JavaScript function找不到TextBox1。

c#

protectedvoid Page_Load(object sender, EventArgs e)


{


string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;


Page.ClientScript.RegisterStartupScript(
this.GetType(),


“MyScript”, myScript,
true);


}

这段代码把JavaScript function放置于ASP.NET page底部,因此JavaScript运行时它能找到TextBox1。

3.使用Page.ClientScript.RegisterClientScriptInclude
许多开发者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的JavaScript。

c#

string myScript = “myJavaScriptCode.js”


Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);

这将在ASP.NET页面产生如下结构:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值