ClientScriptManager.RegisterHiddenField Method (String, String)


Registers a hidden value with the Page object.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Sub RegisterHiddenField (
	hiddenFieldName As String,
	hiddenFieldInitialValue As String


Type: System.String

The name of the hidden field to register.

Type: System.String

The initial value of the field to register.

Exception Condition

hiddenFieldName is null.

The RegisterHiddenField method creates a hidden <input> element on the rendered HTML page.

The following code example demonstrates the use of the RegisterArrayDeclaration and RegisterHiddenField methods. The example registers an array and a hidden value and defines the OnClick event of an <input> button to calculate the sum of two values of the array and the hidden value.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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));} </")
      cs.RegisterClientScriptBlock(cstype, csname, cstext.ToString(), False)
    End If

  End Sub


<html xmlns="" >
    <title>ClientScriptManager Example</title>
     <form    id="Form1"
     <input type="text"
            id="Message" />
     <input type="button" 
            value="Run Script" />

.NET Framework
Available since 2.0
Return to top