Page.RegisterHiddenField(String, String) 方法

定义

注意

The recommended alternative is ClientScript.RegisterHiddenField(string hiddenFieldName, string hiddenFieldInitialValue). http://go.microsoft.com/fwlink/?linkid=14202

使服务器控件能够在窗体上自动注册隐藏字段。Allows server controls to automatically register a hidden field on the form. 呈现 Page 服务器控件时,将该字段发送到 HtmlForm 对象。The field will be sent to the Page object when the HtmlForm server control is rendered.

public:
 virtual void RegisterHiddenField(System::String ^ hiddenFieldName, System::String ^ hiddenFieldInitialValue);
public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue);
[System.Obsolete("The recommended alternative is ClientScript.RegisterHiddenField(string hiddenFieldName, string hiddenFieldInitialValue). http://go.microsoft.com/fwlink/?linkid=14202")]
public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue);
abstract member RegisterHiddenField : string * string -> unit
override this.RegisterHiddenField : string * string -> unit
[<System.Obsolete("The recommended alternative is ClientScript.RegisterHiddenField(string hiddenFieldName, string hiddenFieldInitialValue). http://go.microsoft.com/fwlink/?linkid=14202")>]
abstract member RegisterHiddenField : string * string -> unit
override this.RegisterHiddenField : string * string -> unit
Public Overridable Sub RegisterHiddenField (hiddenFieldName As String, hiddenFieldInitialValue As String)

参数

hiddenFieldName
String

要呈现的隐藏字段的唯一名称。The unique name of the hidden field to be rendered.

hiddenFieldInitialValue
String

要以隐藏形式发出的值。The value to be emitted in the hidden form.

属性

示例

下面的代码示例使用 RegisterHiddenField 方法帮助创建被传递到请求浏览器的 ECMAScript 代码。The following code example uses the RegisterHiddenField method to help create ECMAScript code that is passed to the requesting browser. 隐藏字段的名称设置为 myHiddenField ,其值设置为 "欢迎使用 Microsoft!"The name of the hidden field is set to myHiddenField and its value is set to "Welcome to Microsoft!" RegisterStartupScript myHiddenField 当用户单击页面上的按钮时,方法将调用值。The RegisterStartupScript method calls the myHiddenField value when the user clicks a button on the page.

重要

此示例包含一个隐藏字段,这是一个潜在的安全威胁。This example has a hidden field, which is a potential security threat. 默认情况下,你应该验证隐藏字段的值,就像使用文本框的值一样。By default, you should validate the value of a hidden field as you would the value of a text box. ASP.NET 网页验证用户输入不包含脚本或 HTML 元素。ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

void Page_Load(Object sender, EventArgs e) 
{ 
   String scriptString = "<script language=\"JavaScript\"> function doClick() {";
   scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<";
   scriptString += "/" + "script>";
      
   RegisterHiddenField("myHiddenField", "Welcome to Microsoft!"); 
   
   RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')"); 
   
   RegisterStartupScript("startup", scriptString);
} 

     Dim scriptString As String = "<script language=""JavaScript""> function doClick() {"
scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<"
scriptString += "/" + "script>"
   
RegisterHiddenField("myHiddenField", "Welcome to Microsoft!")

RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')")

RegisterStartupScript("startup", scriptString)

注解

RegisterHiddenField 方法已弃用。The RegisterHiddenField method has been deprecated. 使用 RegisterHiddenField 类中的方法 ClientScriptManagerUse the RegisterHiddenField method in the ClientScriptManager class.

适用于

另请参阅