使用clientscriptmanager向客户端注册脚本

使用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);

因为updatepanel2updatemode="conditional",不是每次都更新,所以不会有alert(2);

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

posted @ 2009-02-24 10:36  chinaifne  阅读(327)  评论(0编辑  收藏  举报