Contents

Monday, 18 July 2016


Hidden Field

Hidden Field is another client side state management technique. It is mainly used to store small values. It is not rendered in the browser as it is invisible to the browser. It can hold one value for a variable. 
      To add Hidden Field to the web application we only need to drag and drop the HiddenField component from the ToolBox. Then the source code will have the following code:


  <asp:HiddenField ID="HiddenField1" runat="server" />

This is the way to create a HiddenField. Now we only need to save the value of a variable to this and then access it when required.

Let us look at an example for further clarity.

In this example we just take a TextBox to input the name of the user and two buttons, one for saving the value of the TextBox to the HiddenField and the other button to retrieve the value from it to the TextBox.

Design  


Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace httpstateless
{
    public partial class Hidden : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)    //Save Button Click method
        {
                HiddenField1.Value = TextBox1.Text;      //Saving the TextBox value in the HiddenField
                TextBox1.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)   
//Previous Button Click method 
        {
            if (HiddenField1.Value != null)
            {
                TextBox1.Text = HiddenField1.Value;      //Retrieving the value from the HiddenField
            }
        }
    }
} 

Output

 Before clicking save button:



After clicking save button:


After clicking Previous button:


As you can the username is retrieved from the HiddenField.

Point to Remember

  •  It is used for same values.
  • It is not rendered in the browser as it is invisible to the browser.
  • It is preferable when a variable value changes frequently.


You may click the Content tab to see all the posts that i will cover in this tutorial for easy navigation.
 
 

No comments:

Post a Comment