Saturday, December 6, 2014

Explain hidden fields in asp.net with example

Introduction: In this article I am trying to explain Hidden fields in asp.net with example.

Description:

Hidden field is control in Asp.net or input control. It is used to store small amount of data. The data is store on the client side. It store only one value for variable and is saved as string. It is not visible in browser but can see the stored data by using view source operation of the browser. 
   
Example:

Html Markup:
<fieldset style="width:400px">
    <legend>Hidden Field Example</legend>
    <table>
    <tr><td></td><td><asp:HiddenField ID="HiddenField1" runat="server" /></td></tr>
    <tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Button"
            onclick="Button1_Click1" /></td></tr>    
    <tr><td></td><td><asp:Label ID="Label1" runat="server"></asp:Label></td></tr>    
    </table>
    </fieldset>

Write the below given code on .aspx.cs file(C#):

protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField1.Value = "Hidden Field Test";
    }
  
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Label1.Text = HiddenField1.Value;
    }

In VB:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        HiddenField1.Value = "Hidden Field Test"
    End Sub
    Protected Sub Button1_Click1(sender As Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = HiddenField1.Value
    End Sub

Advantages:

1    1.   Server resources are not required
2    2.    Hidden fields are easy to implement
3    3.   All browsers support the hidden filed

Disadvantages:

      1.    Hidden fields are not secure because data is stored on client side.
      2.    Hidden field can store only one value.
      3.   Hidden field slow down the performance of page if use large number of hidden fields

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment