Friday, September 20, 2013

How To Use HiddenField in ASP

The HiddenField in ASP.Net give us the facility to store our information in the page without displaying it. i.e. we can get and set the value of hiddenfield but they will not appear on the page.

We can use HiddenField property both in Server Side and Client Side

Note: You should not store sensitive data in HiddenField because even though it is hidden it can still be access able by viewing the page source.

In this post I will show you how to use HiddenField.

In this example I will first change the HiddenField value on client Side using jQuery and than at server side we will access that value

   1:  <html xmlns="">
   2:  <head runat="server">
   3:      <title></title>
   4:      <script type="text/jscript" src="jquery-1.9.1.js"></script>
   5:      <script type="text/jscript">
   6:          $(document).ready(function () {
   7:              $("#hf1").val("Value Changed from Client Side");
   8:          });
   9:      </script>
  10:  </head>
  11:  <body>
  12:      <form id="form1" runat="server">
  13:      <div>
  14:          <asp:Button ID="btn1" runat="server" Text="Click on me" OnClick="btn1_Click" />
  15:          <br />
  16:          <asp:Label ID="lbl1" runat="server"></asp:Label>
  17:          <asp:HiddenField ID="hf1" runat="server" />
  18:      </div>
  19:      </form>
  20:  </body>
  21:  </html>

In the source I define a textbox, a HiddenField, and a Label and than using jQuery I change the value of HiddenField

Now we will try to access this value from Server Side

   1:  protected void btn1_Click(object sender, EventArgs e)
   2:          {
   3:              lbl1.Text = hf1.Value;
   4:          }

When the User click on the button the value of hiddenField will be stored in our Label

I hope this was informative for you and I would like to Thank you for reading

No comments:

Post a Comment