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);就会注册成功。

使用ClientScriptManager向客户端注册脚本的更多相关文章

  1. ClientScriptManager与ScriptManager向客户端注册脚本的区别

    使用ClientScriptManager向客户端注册脚本 ClientScriptManager在非异步(就是说非AJAX)环境下使用的.如果要在异步环境下注册脚本应该使用ScriptManager ...

  2. 向Page对象注册脚本

    在madn上ClientScriptManager 类的示例: <%@ Page Language="C#"%> <!DOCTYPE html PUBLIC &q ...

  3. 第8章 动态客户端注册 - IdentityModel 中文文档(v1.0.0)

    OpenID Connect动态客户端注册的客户端库是作为HttpClient扩展方法提供的. 以下代码发送注册请求: var client = new HttpClient(); var respo ...

  4. Eureka客户端注册多网卡下IP选择问题

    在使用Spring Cloud多人协作开发时有一个场景:我本机启动了Eureka注册中心,其他人机器需要将服务注册到我本机的Eureka.(服务端和客户端在不同机器上) 这时出现了一个问题:服务成功注 ...

  5. 记一次eureka客户端注册失败的问题

    最近老大给了一台新服务器,做测试用,从装软件开始,到把整个环境搭起来,因为之前没有完成搭过,所以费了几天时间,最后总算是成功跑起来了.昨天早上来更新下项目,在测试服务器上重新发布了一下,结果发现服务注 ...

  6. 使用TortoiseSVN的客户端钩子脚本触发Jenkins构建

    我们项目在开发过程中使用了Jenkins构建Windows版本,为了通过自动触发使构建的版本保持最新,可以采用的方法如下: Jenkins Poll SCM:设置Jenkins定时检查变更,在SVN版 ...

  7. Watcher详解 工作机制, Watcher客户端注册、Watcher 服务端注册

    Watcher详解.接口 在 ZooKeeper 中, 接口类 Watcher 用于表示一个标注你的事件处理器,其定义了事件通知相关的逻辑,包含 KeeperState 和 EventType 两个枚 ...

  8. 客户端注册Cannot execute request on any known server解决

    在对eureka注册中心服务端添加安全验证后,新版本springcloud出现一个问题就是,在客户端注册到服务中心时报了一个错:Cannot execute request on any known ...

  9. zabbix客户端监控脚本shell

    zabbix客户端监控脚本shell #!/bin/sh sleep 3 zabbixdir=`pwd` zabbix_version=4.2.5 ###指定版本,最好和server端吻合版本,可以自 ...

随机推荐

  1. 2019牛客暑期多校训练营(第九场)The power of Fibonacci——循环节&&CRT

    题意 求 $\displaystyle \sum_{i=1}^n F_i^m $,($1 \leq n\leq 10^9,1 \leq  m\leq 10^3$),答案对 $10^9$ 取模. 分析 ...

  2. NOIP2018 保卫王国(动态DP)

    题意 求最小权值点覆盖. mmm次询问,每次给出两个点,分别要求每个点必须选或必须不选,输出每次的最小权值覆盖或者无解输出−1-1−1 题解 强制选或者不选可以看做修改权值为±∞\pm\infin±∞ ...

  3. Ubuntu 16.04安装Docker-Compose 与 Can't connect to docker from docker-compose

    根据别的 网址做一个记录 :  https://www.linuxidc.com/Linux/2017-01/139574.htm Linux环境 Ubuntu 16.04(LTS)curl安装安装 ...

  4. sublime 不是插件安装越多越好,如xxxsnippet 自动完成插件太多,就非常耗电脑性能,经常性的卡着不动

    sublime 不是插件安装越多越好,如xxxsnippet 自动完成插件太多,就非常耗电脑性能,经常性的卡着不动

  5. 模拟I2C协议学习点滴之原理框架

    I2C是一种串行总线协议. 目前几种常用的串行总线有UART.SPI和I2C协议.UART协议的总线只有两条,发送(Transmit:TX)和接收(Receive:RX),没有时钟信号,这就要求两位数 ...

  6. 国外最受欢迎的BT-磁力网站

    1.海盗湾 The Pirate Bay 2.KickAssTorrents 3.Torrentz 4.zooqle 5.SumoTorrent 6.TorrentDownloads 7.Rarbg ...

  7. 【原创】go语言学习(八)切片

    目录: 切片定义 切片基本操作 切片传参 make和new的区别 切片定义 1. 切片是基于数组类型做的一层封装.它非常灵活,可以自动扩容. var a []int //定义一个int类型的空切⽚ 2 ...

  8. c++ 珊格迷宫问题

    #demo1 #include<iostream> #include<ctime> #include<cstdlib> #include<queue> ...

  9. Codeforces Round #566 (Div. 2)题解

    时间\(9.05\)好评 A Filling Shapes 宽度为\(3\),不能横向填 考虑纵向填,长度为\(2\)为一块,填法有两种 如果长度为奇数则显然无解,否则\(2^{n/2}\) B Pl ...

  10. Apache日志详解

    在渗透测试的工作中,WEB网站的日志是非常重要的,今天总结了一些关于调配Apache日志的一些东西. 0x00 Apache日志文件名称及路径介绍 我们安装好Apache后,Apache的配置文件(h ...