ClientScriptManager.RegisterArrayDeclaration(String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用陣列名稱和陣列值,向 Page 物件註冊 JavaScript 陣列宣告。
public:
void RegisterArrayDeclaration(System::String ^ arrayName, System::String ^ arrayValue);
public void RegisterArrayDeclaration (string arrayName, string arrayValue);
member this.RegisterArrayDeclaration : string * string -> unit
Public Sub RegisterArrayDeclaration (arrayName As String, arrayValue As String)
參數
- arrayName
- String
要註冊的陣列名稱。
- arrayValue
- String
要註冊的一或多個陣列值。
例外狀況
arrayName
為 null
。
範例
下列程式碼範例示範 如何使用 RegisterArrayDeclaration 和 RegisterHiddenField 方法。 此範例會註冊陣列和隱藏值,並定義 OnClick
按鈕的事件,以計算陣列和隱藏值的兩個 <input>
值總和。
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text"
id="Message" />
<input type="button"
onclick="DoClick()"
value="Run Script" />
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the array name and values.
Dim arrName As String = "MyArray"
Dim arrValue As String = """1"", ""2"", ""text"""
' Define the hidden field name and initial value.
Dim hiddenName As String = "MyHiddenField"
Dim hiddenValue As String = "3"
' Define script name and type.
Dim csname As String = "ConcatScript"
Dim cstype As Type = Me.GetType()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = 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 (Not cs.IsClientScriptBlockRegistered(cstype, csname)) Then
Dim cstext As StringBuilder = 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)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text"
id="Message" />
<input type="button"
onclick="DoClick()"
value="Run Script" />
</form>
</body>
</html>
備註
會 RegisterArrayDeclaration 檢查已註冊的陣列是否存在與參數中所 arrayName
指定名稱相同的名稱,如果是的話,請新增 參數中指定的 arrayValue
值。 因為基礎儲存機制是以 為基礎 ArrayList ,所以允許重複專案。 如果名稱與參數相同的 arrayName
已註冊陣列不存在,則會建立該陣列,並在參數中 arrayValue
加入值。
如果您想要在產生的 JavaScript 陣列中使用字串常值,請在 參數中包含單引號 (') 或逸出雙引號 (\「) arrayValue
。 參數的值 arrayValue
應該是單一元素。 如果需要將多個值新增至陣列,請使用 RegisterArrayDeclaration 方法進行多個呼叫。