Microsoft KB Archive/300105

= How To Maintain HTTP State with Hidden Form Fields in Active Server Pages =

Article ID: 300105

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q300105





IN THIS TASK
SUMMARY
 * How to Create the ASP Page That Maintains HTTP State

REFERENCES



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
 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.  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 %>  <%   Dim counter counter = Request.Form(&quot;PostCount&quot;) if counter = &quot;&quot; then counter = 0 else counter = counter + 1 end if %> Number of Times Hitting the Page: <%=counter%> &quot;>    On the File menu, click Save HiddenTag.asp. Right-click the page in any blank area, and then click View In Browser to run HiddenTag.asp.</li></ol>

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

<div class="references_section">