我是经典asp的新手,并且遇到了字典对象的问题。我想要做的是当我发布一个新的密钥和值。它会动态地将新的键和值添加到字典中并将其显示给浏览器。
每当我发布新密钥和值时,它都不会保存我发布的内容
<html>
<head>
</head>
<body>
<form method="post" action="form.asp">
<input type="text" id="key" name="key" /> <br />
<input type="text" id="value"name="value" />
<input type="submit" name="Submit" value="Submit" />
</form>
<%
Dim key
Dim value
key = request.form("key")
value = request.form("value")
Set d = Server.CreateObject("Scripting.Dictionary")
d.add key,value
Response.Write("<p>The value of the keys are:</p>")
a=d.Keys
for i = 0 To d.Count -1
s = s & a(i) & "<br />"
next
Response.Write(s)
%>
</body>
</html>
答案 0 :(得分:1)
尝试此操作以显示键和值 要保存它,您需要将字典对象放入会话值或将字典值保存为数组。
<html>
<head>
</head>
<body>
<form method="post" action="form.asp">
<input type="text" id="key" name="key" /> <br />
<input type="text" id="value"name="value" />
<input type="submit" name="Submit" value="Submit" />
</form>
<%
Dim key
Dim value
key = request.form("key")
value = request.form("value")
Set d = Server.CreateObject("Scripting.Dictionary")
d.add key,value
Response.Write("<p>The value of the keys are:</p>")
a=d.keys
b=d.items
for i = 0 To d.Count - 1
s = s & a(i) & "=" & b(i) & "<br />"
next
Response.Write(s)
%>
</body>
</html>
答案 1 :(得分:1)
你需要的是将Dictionary存储在Session中,然后每次从那里取出,添加项目并更新Session:
Dim d
If IsObject(Session("d")) Then
Set d = Session("d")
Else
Set d = Server.CreateObject("Scripting.Dictionary")
End If
d.add key, value
Set Session("d") = d
这样,每次用户发布数据时,都会将其添加到全局存储中 注意,默认情况下,会话在用户空闲20分钟后到期。