Search Your Dot Net Topic

Saturday 10 February 2018

Hidden Field in Asp.Net WebForm



In this post you will learn about the following topics:

  ●  What is HiddenField?
  ●  Using Hidden Field.
  ●  Events of HiddenField.
  ●  How to store the value in HiddenField.
  ●  How to retrieve the value from HiddenField.
  ●  Sample code of using HiddenField.



What is HiddenField?
HiddenField, as name implies, is hidden. This is non visual control in ASP.NET where you can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see HiddenField details by simply viewing the source of document.

HiddenFields are not encrypted or protected and can be changed by any one. However, from a security point of view, this is not suggested. ASP.NET uses HiddenField control for managing the ViewState. So, don’t store any important or confidential data like password and credit card details with this control.

Syntax:
<asp:HiddenFieldID="HiddenField1" runat="server" /> 


Use of HiddenField
We developers mostly do not show an ID value of table like ProductID, MemberID because users are not concerned with this kind of data. We store that information in HiddenFields and complete our process very easily.


Events of HiddenFields
As a control, it should have events. HiddenFields has the following events.

================================================
EVENT             TYPE DESCRIPTION
================================================
DataBinding      Occurs when Server Control binds to a data source.
Disposed         Occurs when Server Control is released from the memory.
Init                 Occurs when Server Control is initialized.
Load         Occurs when server control get loaded on the page.
PreRender         Occurs before Rendering the page.
UnLoad         Occurs when Server Control is unloaded from memory.
ValueChanged Occurs when the value gets changed between the round-trip (postback).


ValueChanged event is server-side control. This event gets executed when the value of HiddenField gets changed between postback to the Server.

Nowadays, people avoid using server side ValueChanged Event because all this things are possible through JavaScript or jQuery very easily.




Store the value in HiddenField -

<asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" />  



Set the value of HiddenField in code behind -

  protected void Page_Load(object sender, EventArgs e)
 {      
        hdnfldCurrentDateTime.Value = DateTime.Now.ToString();  

  }  



You can visit this article to see the right uses of HiddenField.



Retrieve the value from HiddenField -

lblCurrentDateTime.Text = Convert.ToString(hdnfldCurrentDateTime.Value); 




Step by step implementation

Create a new ASP.NET Website project called “HiddenFieldExample”.


What is HiddenField in asp.net
What is HiddenField 




Right click on project and select Add-->Add New Item and select Web Form.

How to use Hidden Field?
How to use Hidden Field?



Add a new Web Form called “Default.aspx”.

Advantage of Hidden Field?
Advantage of Hidden Field?



Now, drag and drop the HiddenField Control on the page.

Advantage of Hidden Field?
Advantage of Hidden Field?


By default, the HiddenField control looks like this.
  1. <asp:HiddenFieldID="HiddenField1" runat="server" />  

==================
Default.aspx Code 
==================

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%> 
    <!DOCTYPEhtml> 
    <htmlxmlns="http://www.w3.org/1999/xhtml"
        <headrunat="server"
            <title></title> 
            </head> 
 
            <body> 
                <formid="form1" runat="server"
                    <div> 
                        <asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" /> 
                        <asp:LabelID="lblCurrentDateTime" runat="server" Text=""></asp:Label> 
                    </div> 
                    </form> 
            </body> 
 
            </html>  

==================
Default.aspx.cs Code

==================

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
public partial class_Default: System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e)
 { 
        hdnfldCurrentDateTime.Value = DateTime.Now.ToString(); 
        lblCurrentDateTime.Text = Convert.ToString(hdnfldCurrentDateTime.Value); 
    } 
}  



=========
  O U T P U T 
=========








That's it. Happy Coding!!! 


No comments:

Post a Comment