C# .NET - hiddenfield - Asked By gnanam gnanam on 28-Jun-08 01:45 AM

what is hiddenfield?how to store the value in the hidden field?

check here... - Vasanthakumar D replied to gnanam gnanam on 28-Jun-08 01:55 AM

Hi,

hidden fileds are used to store the values for persistance purpose in  the same page. Its using viewstate to store the values. The values in the hidden fields are encrypted and cannot be accessed by hackers. you can set the values to hidden fields using below code

Server Side:

HiddenField1.Value = "Test";

Client Side Js

document.getElementById('HiddenField1').value = 'Test';

Hidden Fields - Kalit Sikka replied to gnanam gnanam on 28-Jun-08 02:00 AM

Hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.
ASP.NET provides the HtmlInputHidden control that offers hidden field functionality.


protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value=”this is a test”;
//to retrieve a value
string str=Hidden1.Value;

Note: Keep in mind, in order to use hidden field, you have to use HTTP-Post method to post web page. Although its name is ‘Hidden’, its value is not hidden, you can see its value through ‘view source’ function.

See this - Sagar P replied to gnanam gnanam on 28-Jun-08 02:11 AM

Hidden field comes under state management in asp.net.

You can ues hidden field to store inforrmation of a page.When  a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.

You can add it into your page like this;

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />

And you can access the value and store the value into it like;

Server Side:

HiddenField1.Value = "hiddenField";

Client Side Javascript;

document.getElementById('HiddenField1').value='hiddenField';

and if u r using master page then you can access it like;

document.getElementById('<%=HiddenField1.ClientID%>').value='hiddenField';

Try it.

Go thr this to know more abt it;

http://www.csharphelp.com/archives/archive207.html

Best Luck!!!!!!!!!!!!!!!!!!
Sujit.

RE : Hiidden Field - Shailendrasinh Parmar replied to gnanam gnanam on 28-Jun-08 02:13 AM
A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.
ASP.NET provides the HtmlInputHidden control that offers hidden field functionality.
[c#]
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value=”this is a test”;
//to retrieve a value
string str=Hidden1.Value;

Note: Keep in mind, in order to use hidden field, you have to use HTTP-Post method to post web page. Although its name is ‘Hidden’, its value is not hidden, you can see its value through ‘view source’ function.
store value to hiddenfield - sundar k replied to gnanam gnanam on 28-Jun-08 03:04 AM

The HiddenField control provides you with a way to store information in the page without displaying it. For example, you might store a user-preference setting in a HiddenField control so that it can be read in client script. To put information into a HiddenField control, you set its Value property to the value you want to store between postbacks.

You can use the HiddenField control to:

1. Store data in a hidden field on a page.

2. Detect when data stored in the hidden field has changed between postbacks.

The information in a HiddenField control is not displayed when the browser renders the page. However, it can be read and set in client script. When the page is posted back, the contents of the HiddenField control, which includes any changes made in client script, are available in server code.

It is easy for a malicious user to see and modify the contents of a hidden field. Do not store any information in a hidden field that is sensitive or that your application relies on to work properly.

Detecting Changed Data

The value of a HiddenField control can be changed before the page is posted back to the server. This might occur because you are using the hidden field to share information between server code and client script, and client script has updated the value of the control.

To help you detect changes to data in the control, the HiddenField control raises a ValueChanged event if the value of the control changes between postbacks. You can handle this event to determine whether a value has changed.

Below is the sample code for setting value for hidden field:

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void ValueHiddenField_ValueChanged (Object sender, EventArgs e)
  {

    // Display the value of the HiddenField control.
    Message.Text = "The value of the HiddenField control is " + ValueHiddenField.Value + ".";

  }

</script>

<html  >
    <head runat="server">
    <title>HiddenField Example</title>
</head>
<body>
        <form id="Form1" runat="server">

            <h3>HiddenField Example</h3>

            Please enter a value and click the submit button.<br/>

            <asp:Textbox id="ValueTextBox"
              runat="server"/>

            <br/> 

            <input type="submit" name="SubmitButton"
             value="Submit"
             onclick="PageLoad()" />

            <br/>

            <asp:label id="Message" runat="server"/>   

            <asp:hiddenfield id="ValueHiddenField"
              onvaluechanged="ValueHiddenField_ValueChanged"
              value=""
              runat="server"/>

        </form>
    </body>
</html>

<script type="text/javascript">

  <!--
  function PageLoad()
  {

    // Set the value of the HiddenField control with the
    // value from the TextBox.
    Form1.ValueHiddenField.value = Form1.ValueTextBox.value;

  }
  -->

</script>


hiddenfield - Swapnil Salunke replied to gnanam gnanam on 29-Jun-08 01:40 AM
Hello Gnanam

Hidden fields have been around for a long time. This is where you place a text field control on your html page. Then you set the control to hidden. That means that your user cannot see the control or its value on the page when the page loads. It is sitting silently in the background undetected. Well, not exactly.

Hidden fields are not displayed on the web browser, but if you view source, you can see both the hidden field and it's value. Not very secure. They do allow you to post information to other pages, or back to the same page.

The disadvantages of hidden fields?
  1. Increases the HTML size of the page.
  2. You still cannot store structured data
  3. Because you can view page of an HTML page, there is no security
  4. There is no way to persist the data
So even though hidden fields provide some value to your web page, there are still serious limitations that have to be overcome to make it viable as a safe and secure way to store sensitive data from your app.

The HiddenField control is used to store a value that needs to be persisted across posts to the server. It is rendered as an <input type= "hidden"/> element.

Sub ValueHiddenField_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)

    ' Display the value of the HiddenField control.
    Message.Text = "The value of the HiddenField control is " & ValueHiddenField.Value & "."

  End Sub

Please Check this for reference
http://www.youcanlearnseries.com/Programming%20Tips/CSharp/ViewStatemgmnt.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx

Happy Coding takecare