Wednesday, 1 January 2014

What is Hidden Field in Asp.Net

What is Hidden Field in Asp.Net………………………..

The Hidden Field control is used to store a value. It is rendered element. View state, session state and cookies are used to maintain the state of a web forms page. It is a control used to store a value that needs to  be persisted  across posts to the server, but you don’t want the control as its value visible to the user
There are two types of server control hidden fields
  1. System.Web.UI.WebControls.HiddenField
  2. System.Web.UI.HtmlControls.HtmlInputHidden.

Both types has the same primary Value property to hold the value of the hidden field

Hidden Field:
·         Value property of the Hidden Field is used to get or set the values
·         The value is stored as string
·         View State uses Hidden Field to maintain sate across post back
·         Hidden Field is rendered as an <input type=”hidden”/> element

Alternative for Hidden Field
View State ,Query String, Session State and Cookies It can as an alternative for  Hidden Field, Session and Cookies will be accessible from other pages as well, and
Will be available until their time out has reached. When as View State and Hidden Field data is available only on that page and the data is lost when you navigate away from the page

Advantages of Hidden Field:
·         Hidden Field data is lost when you navigate away from the page, Does not require any explicit cleanup task
·         Hidden Field is accessible to client-side scripts
<script type="text/javascript">
//debugger;
         $(function ()
{
var txt3 = document.getElementById("<%=hdntxt.ClientID%>").value;
}
  </script>


Disadvantages of Hidden Field:
Hidden Field data can be seen, by viewing the page source. Never use Hidden Field to store confidential data

 
Example with Hidden Field ……………………………….

 First we take a Web Page which name is Default3.aspx and drag HiddenField TextBox and two Button control from Toolbox for Addition two number and follow this code which are given below

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

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

}
protected void Button1_Click(object sender, EventArgs e)
{
HiddenField1.Value = txtno.Text;
txtno.Text = String.Empty;

}
protected void btnAddition_Click(object sender, EventArgs e)
{
lblresult.Text = "Output Result="+(Convert.ToInt32(HiddenField1.Value) + Convert.ToInt32(txtno.Text)).ToString();
}
}


Another example First we take a Web Page which name is HiddenField.aspx and drag HiddenField and Button control from Toolbox and follow this code which are given below

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

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

}
int i;
protected void Button1_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(HiddenField1.Value))
{
i=int.Parse(HiddenField1.Value);
}
i++;
HiddenField1.Value = i.ToString();
if (i == 3)
{
Response.Write("You have clicked Three times");
}
}

}


0 comments:

Post a Comment