Working With Hidden Field Control In ASP.NET Using VB.NET

In this article you will learn that how to use Hidden Field control to store a value as well as state.
  • 9852
Download Files:
 

Introduction
 
The Hidden Field control Provides you with a way to store information in the page Without displaying it. This control enables a developer to store a non Displayed value. You can use the Hidden Field control to store state value. The value of Hidden Field control is rendered to the Client Browser. So its not suitable for storing security sensitive value. this control is used to store a value that needs to be persisted across posts to the server. It is rendered as an element. Normally View State, Session State and Cookies are used to maintain the state of a web Form page. However if these methods are disabled or are not available you can use the Hidden Field control to store state value. To specify the value for Hidden Field control, use the value property. You can provide a routine that gets called every time the value of the Hidden Field control changes between posts to the server by creating an Event Handler for the ValueChanged Event.
 
Getting Started
 
Here we are discussing an example in which TextBox value will store in Hidden Field and when we click Button than TextBox value is displayed in Label.

  • Simply create a new ASP.NET web application. 
  • Drag  a Hidden Field, TextBox, Button, Label control on your web page. The page will look like below.

    HiddenField1.gif
     
  • Then write the below code in source view of the  page.

    <%@ Page Language="VB" %>
    <script runat="server" language="vb">   
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            HiddenField1.Value = TextBox1.Text
        End Sub
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Label1.Text = HiddenField1.Value
        End Sub
    </script>
    <
    html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>HiddenField Server Control</title>
    </head>
    <
    body>
        <form id="form1" runat="server">
           <asp:HiddenField ID="HiddenField1" Runat="Server" />
            <asp:TextBox  ID="TextBox1"  runat="server"></asp:TextBox>
           <br />
           <br />
           <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
           <br />
    &nbsp;<br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    &nbsp;</form>
    </
    body>
    </
    html>
     
  • Now run your application.
Output:-

HiddenField2.gif

HiddenField3.gif

HiddenField4.gif

Summary

In this article you Learned that How you can use Hidden Field control to store a value as well as state.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.