我刚刚开发了一个平台,可以在使用如下所示的表格提交 ID 号后立即打印单个 ASP 文档:
<form action="http://localhost/teg/f1.asp" method="POST">
<input type="number" name="number" placeholder="Please Enter ID" />
<button type="submit">Print</button>
</form>
在本例中,它打印一个名为 f1.asp 的文档。但是,我也有 f2.asp,因此我决定编写一次表单并包含一个下拉列表,让用户选择要打印的内容,而不是为两个单独的字段编写两次表单,但为此我想我需要另一页我称之为重定向。表格变化如下:
<form action="http://localhost/teg/redirect.asp" method="POST" id="new">
<input type="number" name="number" placeholder="Please Enter ID" />
<button type="submit">Print</button>
</form>
<label> Type </label>
<select name="list" form="new">
<option value="1">f1</option>
<option value="2">f2</option>
</select>
重定向 ASP 页面看起来:
<html>
<body>
<%
Dim list
list = request.form("list")
If list=1 Then response.redirect("f1.asp") Else If list=2 Then response.redirect("f2.asp") End If
%>
最后,当使用下拉列表单击“打印”时,它可以完美重定向,但 ID 号字段中的“number”值未达到用户选择的文档类型,因此文档无法正确显示。相反,会出现以下数据库引擎错误:“80040e10”。任何帮助,将不胜感激。
请务必记住,HTTP 是无状态的,只有通过 Cookie 等技术,经典 ASP 才能跨 HTTP 请求将值存储在
Session
对象中。
目前,由于
Response.Redirect()
,发送新请求时,Request.Form("number")
的值会丢失。有几种方法可以解决这个问题,我在这里重点介绍三种。
最简单的方法是传递使重定向的页面正常工作所需的值。
<%
'Assign our form values to local variables.
Dim list: list = Request.Form("list") & ""
Dim id: id = Request.Form("number") & ""
'Check the value contain data and they are valid numeric values.
If Len(list) > 0 And IsNumeric(list) Then list = CLng(list) Else list = 0
If Len(id) > 0 And IsNumeric(id) Then id = CLng(id) Else id = 0
'***** Only process if we have valid values to build the redirect. *****
Dim url
If list > 0 And id > 0 Then
'We have valid values build our URL that we will use in
'the redirect call.
url = "f" & list & ".asp?id=" & id
Else
'Something not right with request go back to the form page.
'Setting this will send the page back to the form page when the
'redirect is called.
url = "formpage.asp"
End If
'Make the redirect request.
Call Response.Redirect(url)
%>
然后在重定向页面中使用
id
调用 Request.QueryString("id")
查询字符串值。
Server.Transfer()
方法在某些情况下你可以使用这种方法来简化代码。简而言之,它不是将其重定向,而是将请求的页面“缝合”到当前请求的底部,从而允许表单参数仍然可以访问,因为从技术上来说,就互联网浏览器而言,它仍然是相同的请求。
<%
Dim list: list = Request.Form("list") & ""
Dim id: id = Request.Form("number") & ""
If Len(list) > 0 And IsNumeric(list) Then list = CLng(list) Else list = 0
If Len(id) > 0 And IsNumeric(id) Then id = CLng(id) Else id = 0
If list > 0 And id > 0 Then
Call Server.Transfer("f" & list & ".asp")
Else
'Handle invalid request here
End If
%>
您将能够在传输的页面中使用
Request
对象中的任何值,例如 .Form
和 QueryString
,而不会产生错误,因为就服务器而言,它们仍然是同一页面。
Session
对象如果您在 IIS 中将
Enable Session State
设置为 True
,您可以在 Session
对象内跨请求存储值(经典 ASP 使用会话 Cookie 来维护服务器内存中的会话状态)。
<%
Dim list: list = Request.Form("list") & ""
Dim id: id = Request.Form("number") & ""
If Len(list) > 0 And IsNumeric(list) Then list = CLng(list) Else list = 0
If Len(id) > 0 And IsNumeric(id) Then id = CLng(id) Else id = 0
If list > 0 Then
'Store the value we want to persist in the Session.
Session("PrintId") = id
Call Response.Redirect("f" & list & ".asp")
Else
'Handle invalid request here.
End If
%>
然后要将值返回到重定向页面内,请使用
varname = Session("PrintId")
。
虽然已经很晚了,但稍微简单一点。无需进行重定向,只需使用操作 URL 和所有数据填充新表单,然后在验证后使用 JS 提交触发提交。缺点是 JS 可能会被破坏,但随后会做一个 catch,然后,好吧,替代选项。之所以迟回复,是因为我一直在寻找类似的东西,直到我意识到重定向是不必要的。
您的表格关闭得太早了:
<form action="http://localhost/teg/redirect.asp" method="POST" id="new">
<input type="number" name="number" placeholder="Please Enter ID" />
<button type="submit">Print</button>
</form> <---- THIS
<label> Type </label>
<select name="list" form="new">
<option value="1">f1</option>
<option value="2">f2</option>
</select>
</form> <---- SHOULD BE HERE
其他答案都可以;然而,这回答了你的问题 为什么“你的”代码不起作用。