Self Submitting Forms
ASPAlliance.com: The #1 ASP.NET Community
The ASPSmith
Search
D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | Self Submitting Forms
Self Submitting Forms

By Steven Smith

[Example]

A very important coding method to understand when developing ASP pages is the page that submits to itself. This technique is very powerful and can drastically reduce the number of files you have to maintain and the number of places in which you need to make changes when maintaining code. The concept is simple. Using forms, set the action value to the URL of the page in which the form resides. Then use ASP code to determine what the current state of the page is, and present the proper output to the user.

A typical ASP page constructed to submit to itself will have a large SELECT CASE or IF THEN statement taking up most of the page. I prefer the IF THEN version, because it is more flexible, allowing for sections of code that appear on 2 or 3 out of 10 different page states to be written in only one place. To keep track of the page state within the page, I have found that the following variable convention is quite useful:

	'Initialize State Variables
	fNew	=	Request.Form("btnSubmit")	= ""
	fAdd	=	Request.Form("btnSubmit")	= "Add"
	fModify =	Request.Form("btnSubmit")	= "Modify"
	fClose	=	Request.Form("btnSubmit")	= "Close"
	fHelp	=	Request.Form("Help")		= "Help"

Typically, most of your state variables will use the same form item, such as btnSubmit, above. You can test for a null value to determine if this is the user's initial entry into the page/site. You can also look for other form values to modify the content of your basic pages. For instance, you might have "Concise" and "Verbose" output controlled by a form variable. Lastly, note that the fXxxx variables are all boolean. They receive the boolean value of the comparison between the Request variable and the string. This saves you from having to write If Request("x") = "y" Then fNew = True Else fNew = False End If for every flag, making your code far more readable. Below is the source code for the example listed at the top of this page.

   <% OPTION EXPLICIT %>
   <!-- #INCLUDE VIRTUAL="/stevesmith/include/articleformat.asp" -->
   <%
   'Declare Variables
   Dim fNew 'Page State Flag
   Dim fHelp 'Help flag
   Dim fSubmit 'Page State Flag
   Dim fCancel 'Page State Flag
   Dim fEndHelp 'Page State Flag
10   
11   'Set Page State
12   fNew = Request.Form("btnSubmit") = ""
13   fSubmit = Request.Form("btnSubmit") = "Submit"
14   fCancel = Request.Form("btnSubmit") = "Cancel"
15   fEndHelp = Request.Form("btnHelp") = "Help Off"
16   fHelp = (Request.Form("btnHelp") = "Help" Or Request.Form("Help") = "True") _
17    AND Not fEndHelp
18   
19   Call ArticleHeader("File Access Example -- Directory Listing","","")
20   %>

21   <FORM ACTION="<%=Request.Servervariables("URL")%>" METHOD="POST" id="form1" name="form1">
22   
23   <% If fHelp Then %>
24   <B>Help:</B>
25   <P>
26    This page demonstrates how you can control page state easily by submitting back to the
27    same page over and over again. Simply choose different buttons and see how it affects
28    the outcome of the page.
29   </P>
30   <% End If %>
31   
32   
33   <% If fNew Then %>
34    <P>
35    Welcome to the self submitting page example. You have just entered this page for the
36    first time, or you have requested help, which I can tell because the form variable for
37    the main submit button is empty.
38    </P>
39   <% ElseIf fSubmit Then %>
40    <P>
41    Thank you for your submission. Submit again?
42    </P>
43   <% ElseIf fCancel Then %>
44    <P>
45    Your request has been cancelled. Please make another selection.
46    </P>
47   <% End If %>
48   
49   <HR>
50   <INPUT TYPE="SUBMIT" NAME="btnSubmit" VALUE="Submit">
51   <INPUT TYPE="SUBMIT" NAME="btnSubmit" VALUE="Cancel">
52   <% If Not fHelp Then %>
53   <INPUT TYPE="SUBMIT" NAME="btnHelp" VALUE="Help">
54   <% Else %>
55   <INPUT TYPE="SUBMIT" NAME="btnHelp" VALUE="Help Off">
56   <% End If %>
57   
58   <% If fHelp Then %>
59   <INPUT TYPE="HIDDEN" NAME="Help" VALUE="True">
60   <% Elseif fEndHelp Then %>
61   <INPUT TYPE="HIDDEN" NAME="Help" VALUE="False">
62   <% End If %>
63   </FORM>
64   <%
65   Call ArticleFooter()
66   %>

Self submitting pages are not the answer to every application. Certainly you do not want to have a single ASP file for an entire web site with multiple applications and thousands of lines of code. However, for a single application, it is often far better to create a single page that calls itself than a separate page for every possible state the application can be in(which is the alternative). Further, by making pages submit to themselves and then redirect to the next page after they have finished their processing, it encapsulates your code in individual files as distinct modules, making it much easier to move and manage your files.

One last tip -- set up all of your self-submitting forms to have an action of REQUEST.SERVERVARIABLES("URL"). This will always submit the page to itself, and negates the errors caused when renaming the file.





ASP.NET Developer's Cookbook, By Steven Smith, Rob Howard, ASPAlliance.com 

ASP.NET By Example, By Steven Smith 




Steven Smith, MCSE + Internet (4.0)
Last Modified: 6/12/2009 10:58:23 AM
History: 6/12/2009 10:58:23 AM