使用 ASP 在单个页面上动态创建和提交多个表单






3.75/5 (5投票s)
2002 年 9 月 18 日
3分钟阅读

117973
如何使用 ASP 测试处理阶段,然后围绕它动态构建您的 ASP 页面。
引言
这是一个非常酷的技术,它为我节省了很多麻烦,我希望它也能让你们的生活更轻松。 但是,我不能为最初的想法而邀功——这是我老板的创意。 幸运的是,他喜欢分享知识,因此本文诞生了。
问题所在
现在我们都知道,要处理用户输入,必须将表单提交到可以处理它的页面。 如果我们不想在每个目录中都有整串文件,都试图处理一个事务怎么办? 下面是一种只用一个页面即可完成此操作的方法。
解决方案
这里要理解的第一个原则是隐藏文本框在字段提交中的价值。 我们还创建了 2 个 ASP 变量 - Action
和 FormAction
。 隐藏文本框将始终保存 FormAction
变量的值,因此
<input type=hidden id=txtHFormAction
name=txtHFormAction value='<%=FormAction%>'>
在页面顶部执行以下操作
<%
Dim Action, FormAction
Action = Request.Form("txtHFormAction")
注意:Action 在第二次进入页面时采用旧的 FormAction
值 - 如果它采用 Action
值,则处理将无法完成,因为它会进入 Select Case
的错误选项。 事实上,它在第一次进入时没有任何值是正确的,不是问题 :)
以下 Select Case
根据 Action
中保存的值确定应处理页面的哪个部分。
Select Case Action
Case ""
'First entry to page - set the FormAction
FormAction = "Submitted"
Case "Submitted"
'Get values submitted from form
Name = Request.Form("txtName")
Email = Request.Form("txtEmail")
'Call function to deal with whatever
'processing you want done Process
End Select
Function Process()
'Whatever processing, emailing, etc
End Function
%>
<html>
<head><title>Action Test Page</title></head>
<body>
<% 'Action can also determine what gets
'displayed in the HTML section of your page %>
<% If Action = "" Then %>
<% 'If this is the first entry to the page and Action
'is equal to nothing, then display the form to be completed %>
<form action = "" method=post id=frmAction name=frmAction>
<%'note that this form submits to the current page, you can also
'put the current page's name here instead of empty quotes %>
Please enter your details below:
<table>
<tr>
<td>Name</td>
<td><input type= text name=txtName id=txtName></td>
</tr>
<tr>
<td>Email</td>
<td><input type= text name=txtEmail id=txtEmail></td>
</tr>
</table>
</form>
<%ElseIf Action = "Submitted then"%>
<%'If this is the second entry to the page display some kind
'of user friendly message to inform the user that his request has
'been successfully processed %>
<center> Thankyou for contacting us <% response.write Name%>,
we shall be in touch with you within 2 working days
</center>
<%End If%>
</body>
</html>
请注意,您绝不限于在单个页面中使用 Action
的次数。 这可用于将非常长的表单分解为逻辑块,以便用户不会感到不知所措 - 例如,提交个人详细信息,然后提交包含居住详细信息的新表单,一旦提交该表单,则显示包含工作详细信息的新表单,等等。
这对于数据库处理特别有用。 您可以将所有这些值存储在隐藏的文本框中,并在最后将它们拉出来进行插入或更新。
下面是代码的完整列表,没有注释中断它。 我希望这里的每个人都觉得它和我一样有用。 一旦你使用过这个方法几次,我相信你们都会找到更多新的和创新的方法来使用它 - 请告诉我当你这样做的时候! 请注意,此教程没有包含要下载的文件,因为这些函数实际上什么也不做,你可以从这里复制 & 粘贴任何你想要的东西。 我试图分享一个想法,而不是执行特定功能的代码。
<%
Dim Action, FormAction
Action = Request.Form("txtHFormAction")
Select Case Action
Case ""
FormAction = "Submitted"
Case "Submitted"
'Get values submitted from form
Name = Request.Form("txtName")
Email = Request.Form("txtEmail")
Process
End Select
Function Process()
'Whatever processing, emailing, etc
End Function
%>
<html>
<head><title>Action Test Page</title></head>
<body>
<% If Action = "" Then %>
<form action = "" method=post id=frmAction name=frmAction>
Please enter your details below: <table border=0>
<tr>
<td>Name</td>
<td><input type= text name=txtName id=txtName></td>
</tr>
<tr>
<td>Email</td>
<td><input type= text name=txtEmail id=txtEmail></td>
</tr>
</table>
</form>
<%ElseIf Action = "Submitted then"%> <center>
Thankyou for contacting us <% response.write Name %>,
we shall be in touch with you within 2 working days
</center>
<%End If%>
</body>
</html>
请注意,这种处理状态的测试也可以用于动态决定页面上应该显示什么。 我目前正在撰写一篇文章,演示如何测试记录集中返回的值,并使用上述方法根据记录集内容选择在页面上显示的内容。 如果您有任何建议,我们将不胜感激,因为我不声称自己是作者 :)。 敬请关注...