使用ClientScriptManager向客户端注册脚本

时间:2023-03-09 17:36:40
使用ClientScriptManager向客户端注册脚本

ClientScriptManager在非异步(就是说非AJAX)环境下使用的。如果要在异步环境下注册脚本应该使用ScriptManager的静态方法来注册(ScriptManager兼容异步于非异步环境下注册脚本)。ClientScriptManager中注册脚本的方法在ScriptManager中都有一一对应的方法,但是有一些区别,ScriptManager中的方法多了一个参数(多了第一个参数),而且使用ScriptManager来注册脚本不是绝对能注册成功的。

.aspx文件代码

<%@ page language="C#" autoeventwireup="true" codefile="ClientScriptManager.aspx.cs" inherits="Demo4_ClientScriptManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ClientScriptManager</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" />
    </form>
</body>
</html>

.aspx.cs文件代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class Demo4_ClientScriptManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
     protected void Button1_Click(object sender, EventArgs e)
     {
         ClientScriptManager cs = this.ClientScript;
         cs.RegisterArrayDeclaration("Hello", "1, 2, 3");//#1
         cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);//#2
         cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");//#3
         cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");//#4
         cs.RegisterHiddenField("hello", "world");//#5
         cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");//#6
         cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");//#7
     }
}

生成页面的HTML代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ClientScriptManager</title>
</head>
<body>
    <form id="form1" name="form1" method="post" action="ClientScriptManager.aspx" onsubmit="javascript:return WebForm_OnSubmit();">
    <div>
        <!--   #5   -->
        <input type="hidden" name="hello" id="hello" value="world" />
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ2OTkzNDMyMWRkdcWxeVaF9qGYsIaDUa9Rcaihgnk=" />
    </div>
    <!--   #2   -->
    <script type="text/javascript">         function helloWorld() { alert(1); }
    </script>     <!--   #3   -->
    <script src="HelloWorld.js" type="text/javascript"></script>
    <!--   #6   -->
    <script type="text/javascript">
        function WebForm_OnSubmit() {
            return window.confirm('Do you really want to submit the form?');
            return true;
        }     </script>
    <input type="submit" name="Button1" value="Button" id="Button1" />      <!--   #1   -->
    <script type="text/javascript">
        var Hello = new Array(1, 2, 3);
    </script>
    <!--   #4   -->
    <script type="text/javascript">
        var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
        Button1.Hello = "World";
    </script>
    <div>
        <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKet4KWBgKM54rGBikWLuZSHp4emnxNA3F0qTbFNfuo" />
    </div>
    <!--   #7   -->
    <script>
        alert('The page has loaded!')
    </script>
    </form>
</body>
</html> 

使用ScriptManager向客户端注册脚本

使用ScriptManager来向客户端注册脚本时,不一定每次都会成功生效,只有UpdatePanel更新了,才会注册成功。因为注册脚本的行为是在UpdatePanel更新后执行的。

.aspx文件代码

<%@ page language="C#" autoeventwireup="true" codefile="ScriptManagerReg.aspx.cs" inherits="Demo5_ScriptManagerReg" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ScriptManagerReg</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:scriptmanager id="ScriptManager1" runat="server"> </asp:scriptmanager>
    <asp:updatepanel id="UpdatePanel1" runat="server">
              <ContentTemplate>
                   <%= DateTime.Now %>
                   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
              </ContentTemplate>
         </asp:updatepanel>
    <asp:updatepanel id="UpdatePanel2" runat="server" updatemode="Conditional">
              <ContentTemplate>
                   <%= DateTime.Now %>
              </ContentTemplate>
         </asp:updatepanel>
    </form>
</body>
</html> 

.aspx.cs文件代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class Demo5_ScriptManagerReg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {     }      protected void Button1_Click1(object sender, EventArgs e)
     {
        //#1
         ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
        //#2
         ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "UpdatePanel2", "alert(2)", true);
     }
}

说明

运行程序后,第一个时间会更新,更新后会alert(1);

因为UpdatePanel2的UpdateMode="Conditional",不是每次都更新,所以不会有alert(2);

只要UpdatePanel2的UpdateMode="Always",则UpdatePanel2每次都更新,因为能更新了,所以alert(2);就会注册成功。