Classic ASP. Show new key and value in dictionary
I 'm new to classic asp and got a problem with the dictionary object. What i'm trying to do is when i POST a new key and value. it will dynamically add a new key and value into the dictionary and show it to the browser.
Whenever i POST a new key and value it doesn't save what i've POSTed
<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("Scripti开发者_C百科ng.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>
What you need is to store the Dictionary in Session then take it from there every time, add the item and update the 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
This way every time user will post data, it will be added to the global storage.
Note, by default Session expires after 20 minutes of user being idle.
Try this to show key and value.
To save it you will need to put the dictionary object into a session value or save the dictionary values as an array.
<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>
精彩评论