日期:2014-05-16  浏览次数:20410 次

我用ClientScript.RegisterArrayDeclaration注册js数组变量,但是变量是在页面最后面,如何让其在页面开始生成?
我用ClientScript.RegisterArrayDeclaration注册js数组变量,但是变量是在页面最后面,如何让其在页面开始生成?
------解决方案--------------------
Page_Load里调用就可以了。

<%@ Page Language="C#"%>

<script runat="server">
 
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the array name and values.
    String arrName = "MyArray";
    String arrValue = "\"1\", \"2\", \"text\"";
    
    // Define the hidden field name and initial value.
    String hiddenName = "MyHiddenField";
    String hiddenValue = "3";
    
    // Define script name and type.
    String csname = "ConcatScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Register the array with the Page class.
    cs.RegisterArrayDeclaration(arrName, arrValue);

    // Register the hidden field with the Page class.
    cs.RegisterHiddenField(hiddenName, hiddenValue);

    // Check to see if the  script is already registered.
    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(" + Form1.Name + "." + hiddenName + ".value));} </");
      cstext.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname, cstext.ToString(), false);
    }
  }
</script>
<html>
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
 <form    id="Form1"
            runat="server">
     <input type="text"
            id="Message" />
     <input type="button" 
            onclick="DoClick()"