Visual Studio .NET - Hidden Field - Asked By Aldo Lin Lin on 14-Aug-08 12:38 AM

vb.net standard control question:- Hidden Field for what purpuse it is using, and how explain my briefly
bye

check here... - Vasanthakumar D replied to Aldo Lin Lin on 14-Aug-08 12:42 AM

Hi,

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.

Read this - ram kumar replied to Aldo Lin Lin on 14-Aug-08 12:55 AM

Hi,

ASP.NET allows you to store information in a HiddenField control,
which renders as a standard HTML hidden field. 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 want
to store directly in the page.

A HiddenField control stores a single variable in its Value property and must be
explicitly added to the page. For more information, see HiddenField Web Server
Control Overview.

In order for hidden-field values to be available during page processing,
you must submit the page using an HTTP POST command. If you use hidden fields
and a page is processed in response to a link or an HTTP GET command, the hidden
fields will not be available. For usage recommendations, see ASP.NET
State Management Recommendations.


The disadvantages of hidden fields?

Increases the HTML size of the page.
You still cannot store structured data
Because you can view page of an HTML page, there is no security
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.

Reply - Binny ch replied to Aldo Lin Lin on 14-Aug-08 01:10 AM

Hidden Fields allow you to send additional information with a submitted form that don't necessarily require a user to "fill them out."

Here are some examples of how hidden fields can be used:

  • Let's say a customer is filling out a form that allows them to purchase a product. You can embed the stock number or ID of the product in a hidden field so that you know which product they are trying to purchase.
  • PayPal "add to cart" buttons are actually form buttons with lots of hidden fields. Their button wizard allows you to define those hidden field values, such as the price of the product, the name of the product, and your PayPal user ID. When someone clicks on the "add to cart" button, the form submits itself to PayPal's web site, and based on the values of the hidden fields, PayPal is able to display a shopping cart page to the user with the product they want to purchase.

Using hidden fields is usually an advanced topic, as it requires you to know how to script a page that accepts and manipulates the submitted information.

go through this link:

http://www.pixelmill.com/support/support_article.aspx?ParentListID=al1021&articleid=kb101356

http://www.learnasp.com/freebook/asp/hidden.aspx

Hidden Field - SP replied to Aldo Lin Lin on 14-Aug-08 01:28 AM

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.

Normally view state, session state, and cookies are used to maintain the state of a Web Forms page. However, if these methods are disabled or are not available, you can use the HiddenField control to store state values.

To specify the value for a HiddenField control, use the Value property. You can provide a routine that gets called every time the value of the HiddenField control changes between posts to the server by creating an event handler for the ValueChanged event.
 Examples

The following example demonstrates how to use the HiddenField control to store the value of a TextBox control between posts to the server.
[Security note] Security Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page language="VB" %>

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

  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

</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>


C#
Copy Code

<%@ 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>


Check this - san san replied to Aldo Lin Lin on 14-Aug-08 02:02 AM
Hi

ASP.NET controls automatically retain their data when a page is sent to the server by a user clicking a submit button. Microsoft calls this persistence of data view state. In the past, developers would have to hack a way to remember the item selected in a drop-down menu or keep the contents of a text box, typically using a hidden form field. This is no longer the case; ASP.NET pages, once submitted to the server for processing, automatically retain all information contained within text boxes, items selected within drop-down menus, radio buttons, and check boxes. Even better, they keep dynamically generated tags, controls, and text. Consider the following ASP page, called sample.asp:

<html>
<head>
 <title>Sample Page using VBScript</title>
</head>
<body>
<form method="post" action="sample.asp">
 <input type="text" name="txtName"/>
 <input type="Submit" name="btnSubmit" text="Click Me"/>
<%
If Request.Form("txtName") <> "" Then
 Response.Write(Request.Form("txtName"))
End If
%>

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

The equivalent page in ASP.NET, ViewState.aspx, demonstrates data persistence using view state:

ViewState.aspx

<html>
<head>
<title>Sample Page using VB.NET</title>
<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
 lblMessage.Text = txtName.Text
End Sub
</script>
</head>

<body>
<form runat="server">
 <asp:TextBox id="txtName" runat="server" />
 <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click"  
     runat="server" />
 <asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

 ViewState.aspx

<html>
<head>
<title>Sample Page using C#</title>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
 lblMessage.Text = txtName.Text;
}
</script>
</head>

<body>
<form runat="server">
 <asp:TextBox id="txtName" runat="server" />
 <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click"
     runat="server" />
 <asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

In this case, the code uses ASP.NET controls with the runat="server" attribute. As you can see in Figure 2.2, the text from the box appears on the page when the button is clicked, but also notice that the data remains in the text box! The data in this example is preserved because of view state

ASP.NET supports view state. When a page is submitted, the information within the controls is preserved in the HTML Hidden fields.

You can see the benefits of view state already. But where is all that information stored? ASP.NET pages maintain view state by encrypting the data within a hidden form field. View the source of the page after you've submitted the form, and look for the following code:

<input type="hidden" name="__VIEWSTATE" value="dDwtMTcyOTAyO
DAwNzt0PDtsPGk8Mj47PjtsPHQ8O2w8aTwzPjs+O2w8dDxwPGw8aW5uZXJodG
1sOz47bDxIZWxsbyBXb3JsZDs+Pjs7Pjs+Pjs+Pjs+d2wl7GlhgweO9LlUihS
FaGxk6t4=" />

This is a standard HTML hidden form field with the value set to the encrypted data from the form element. As soon as you submit the form for processing, all information relevant to the view state of the page is stored within this hidden form field.

---------------

Hope this helps

SAN

Use this - Atul Shinde replied to Aldo Lin Lin on 14-Aug-08 03:25 AM

Hidden Fields allow you to send additional information with a submitted form that don't necessarily require a user to "fill them out."

- Let's say a customer is filling out a form that allows them to purchase a product. You can embed the stock number or ID of the product in a hidden field so that you know which product they are trying to purchase.

- PayPal "add to cart" buttons are actually form buttons with lots of hidden fields. Their button wizard allows you to define those hidden field values, such as the price of the product, the name of the product, and your PayPal user ID. When someone clicks on the "add to cart" button, the form submits itself to PayPal's web site, and based on the values of the hidden fields, PayPal is able to display a shopping cart page to the user with the product they want to purchase.

Using hidden fields is usually an advanced topic, as it requires you to know how to script a page that accepts and manipulates the submitted information.

go through this link:

http://www.pixelmill.com/support/support_article.aspx?ParentListID=al1021&articleid=kb101356

http://www.learnasp.com/freebook/asp/hidden.aspx