ClientScriptManager.RegisterHiddenField(String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
向 Page 物件註冊隱藏值。
public:
void RegisterHiddenField(System::String ^ hiddenFieldName, System::String ^ hiddenFieldInitialValue);
public void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue);
member this.RegisterHiddenField : string * string -> unit
Public Sub RegisterHiddenField (hiddenFieldName As String, hiddenFieldInitialValue As String)
參數
- hiddenFieldName
- String
要註冊的隱藏欄位名稱。
- hiddenFieldInitialValue
- String
要註冊的欄位初始值。
例外狀況
hiddenFieldName
為 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>
備註
方法 RegisterHiddenField 會在轉譯的 HTML 頁面上建立隱藏 <input>
專案。