How To Maintain HTTP State with Hidden Form Fields in Active Server Pages (300105)



The information in this article applies to:

  • Microsoft Active Server Pages

This article was previously published under Q300105

SUMMARY

In certain situations, you may need to store data as you move through the Web pages of an Active Server Pages (ASP) application so that this information will be available to you in a later page, or so that you can keep track of the current state of a page as it posts to itself. For example, if a user makes a selection in one ASP page, you may want to reference that information in the next page. Or, you may want to keep track of the page state as a page posts to itself for an operation such as performing ActiveX Data Objects (ADO) recordset paging. Although you can use several approaches to maintain state (such as Session variables or a back-end database), you can use hidden tags to achieve this result efficiently. This article demonstrates how to use hidden tags to maintain state.

The sample ASP page in this article keeps track of how many times the page posts back to itself. To demonstrate this, you increment the value of a hidden tag every time you perform an HTTP POST request. Without maintaining the state of the page in some manner, you always reset the page to its original state on every instance of the POST method.

back to the top

How to Create the ASP Page That Maintains HTTP State

  1. In Visual InterDev 6.0, right-click Project in Project Explorer, click Add, and then click Active Server Page. Name the page HiddenTag.asp, and then click Open.
  2. Highlight the following code, right-click the code, and then click Copy. In the newly created HiddenTag.asp page, click Paste as HTML on the Edit menu to replace the default code with the following code:
    <%@ Language=VBScript %>
    <html>
    <head>
    </head>
    <body>
    <form action="" method=POST>
    <%
    	Dim counter
    	counter = Request.Form("PostCount")
    	if counter = "" then
    	  counter = 0
    	else
    	  counter = counter + 1
    	end if
    %>
    Number of Times Hitting the Page: <%=counter%>
    <INPUT type="hidden" id="PostCount" name="PostCount" value="<%=counter%>">
    <BR>
    <INPUT type="submit" value="Submit">
    </form>
    </body>
    </html>
    					
  3. On the File menu, click Save HiddenTag.asp.
  4. Right-click the page in any blank area, and then click View In Browser to run HiddenTag.asp.
Notice that the page keeps track of how many times it posts back to itself; this is an example of the page maintaining its state. The HTML hidden tag is used to retain the current count for the number of page submissions. The Request.Form collection in the server-side code accesses the hidden tag value when the page is submitted through an HTTP POST request back to the server. The value is then incremented in server-side code and inserted into the client-side code to set the updated value for the hidden tag. Although you can use other HTML controls to retain the value for the number of page submissions, if you use the hidden tag approach, you do not have to render a control unnecessarily on the page simply to hold a value. You can apply this technique in various scenarios in which you need to keep track of specific data as a page posts to itself or another ASP page.

back to the top

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

175167 How To Store State in Active Server Pages Applications

back to the top

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbhowto kbHOWTOmaster kbScript KB300105 kbAudDeveloper