To preserve the data during the page post back and page load hidden fields are used . In ASP.NET it is used by view state . Hidden fields are input fields and not visible to the user . You can add hidden fields on your page. You can also store the data in these fields but the data is stored in its value property . The information which is stored in the hidden fields can be rendered for a single page . Using the hidden fields the page should be submitted to the server .
An example to use the hidden field :
Default.aspx
<
asp:Content
ID
=
"HeaderContent"
runat
=
"server"
ContentPlaceHolderID
=
"HeadContent"
>
asp:Content
>
<
asp:Content
ID
=
"BodyContent"
runat
=
"server"
ContentPlaceHolderID
=
"MainContent"
>
<
asp:HiddenField
ID
=
"Countinghiddenfield"
runat
=
"server"
/>
<
table
>
<
tr
>
<
td
>
<
asp:Label
ID
=
"lbltext"
runat
=
"server"
Text
=
"Label"
>asp:Label
>
td
>
tr
>
<
td
>
<
asp:Button
ID
=
"Submitbutton"
runat
=
"server"
Text
=
"Submit"
/>
In the code behind file
Default.aspx.cs
using
System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
namespace
Clientstatehidden
{
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
int
clicks;
int
.TryParse(Countinghiddenfield.Value,
out
clicks);
clicks++;
Countinghiddenfield.Value = clicks.ToString();
lbltext.Text =
"Count of Submit Button "
+ Countinghiddenfield.Value;
}
}
}
The output screen shot of the above program .
