ClientScriptManager.RegisterArrayDeclaration 方法

ClientScriptManager.RegisterArrayDeclaration 方法:使用数组名称和数组值向Page对象注册JavaScript数组声明。

protected void Page_Load(object sender, EventArgs e)
        
{
            
// 定义数组的名称和值
            String arrName = "MyArray";
            String arrValue 
= "\"1\", \"2\", \"text\"";

            
// 定义隐藏域的名称和初始值
            String hiddenName = "MyHiddenField";
            String hiddenValue 
= "3";

            
// 定义脚本的名称和类型
            String csname = "ConcatScript";
            Type cstype 
= this.GetType();

            
//获取一个ClientScriptManager类型的引用
            ClientScriptManager cs = Page.ClientScript;

            
// 在页面中注册数组
            cs.RegisterArrayDeclaration(arrName, arrValue);

            
// 在页面中注册隐藏域
            cs.RegisterHiddenField(hiddenName, hiddenValue);

            
// 判断脚本是不是已经注册到了页面中
            if (!cs.IsClientScriptBlockRegistered(cstype, csname))
            
{
                StringBuilder cstext 
= new StringBuilder();
                cstext.Append(
"<script type=text/javascript> function DoClick() {");
                cstext.Append(
"Form1.Message.value='Sum = ' + ");
                cstext.Append(
"(parseInt(" + arrName + "[0])+");
                cstext.Append(
"parseInt(" + arrName + "[1])+");
                cstext.Append(
"parseInt(" + this.Form1.Name + "." + hiddenName + ".value));} </");
                cstext.Append(
"script>");
                cs.RegisterClientScriptBlock(cstype, csname, cstext.ToString(), 
false);
            }

        }

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="Form1" runat="server">
    
<div>
    
<input type="text" id="Message" />
     
<input type="button" onclick="DoClick()" value="Run Script">
    
</div>
    
</form>
</body>
</html>

参见博客文章:http://hi.baidu.com/csharpfather/blog/item/0cb19935f68a6d1191ef394f.html

posted on 2008-07-05 12:41  王丹小筑  阅读(907)  评论(0)    收藏  举报

导航