An introduction to the role of hidden domain hidden in HTML and examples of its use

Source: Internet
Author: User
Tags tostring

Hidden fields are not visible to the user in the page, the purpose of inserting hidden fields in a form is to collect or send information for use by programs that are processed by the form, and here is a detailed description of how this hidden field is used in practice, and interested friends don't miss the HA

 

Basic Syntax:

<input type= "hidden" name= "Field_name" value= "value" >

function:

1 hidden fields are not visible to the user on the page, and hidden fields are inserted into the form to collect or send information to facilitate use by the program that processes the form. When the viewer clicks the Send button to send the form, the hidden field information is also sent to the server.

2 Sometimes we have to give the user a message that he submits to the form to identify the user, such as SessionKey, and so on. Of course, these things can also be implemented with cookies, but the use of hidden fields is much simpler. And there will be no browser does not support the user to disable cookies annoyance.

3 Sometimes a form has more than one submit button, how to make the program can be divided into the end of the user is the button to submit it? We can write a hidden field and then add onclick= "document.form.command.value=" to each button, and then we check the command value before we get the data and we know that the user is submitting the button.

4 Sometimes there are multiple form in a Web page, we know that multiple form can not be submitted at the same time, but sometimes these form do interact, we can add hidden fields in form to make them connected.

5 JavaScript does not support global variables, but sometimes we have to use global variables, we can put the value in the hidden field first, its value will not be lost.

6 There is an example, such as pressing a button to eject four small windows, when clicked on one of the small windows, the other three automatically closed. However, IE does not support small windows to call each other, so only in the parent window to write a hidden field, when the small window to see that the value of the hidden field is close when you turn off.

Example: Using the hidden Implementation click the Submit button number plus 1

The value is self increasing. htm

Copy Code code as follows:


&lt;form action= "number of ashx" method= "POST" &gt;


&lt;input type= "hidden" name= "_viewstate" value= "a"/&gt;


&lt;input type= "hidden" name= "_div" value= "@n"/&gt;


&lt;!--&lt;input name= "txt" type= "text" value= "@value"/&gt;--&gt;


&lt;div&gt;@n&lt;/div&gt;


&lt;input type= "Submit" value= "click"/&gt;


&lt;/form&gt;


Using a generic handler to implement the

The value is increased by itself. ashx

Copy Code code as follows:


int n = 0; The
public void ProcessRequest (HttpContext context) {
context. Response.ContentType = "text/html";

String path = context. Request.mappath ("Number of self-added. htm");
String html = System.IO.File.ReadAllText (path);
//To determine whether the page was first loaded
string viewstate = context. request.form["_viewstate"];
if (!string. IsNullOrEmpty (ViewState))
{
//click button Post
//Get hidden field value
String s = context. request.form["_div"];
if (int. TryParse (S, out n))
{
n++;
HTML = HTML. Replace ("@n", n.tostring ());
}
}
Else
{
//page is first loaded, the hidden field corresponding to Div and Div is assigned
HTML = HTML. Replace ("@n", n.tostring ());
}
Context. Response.Write (HTML);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.