SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Apr 10, 2013, 06:15 #1
- Join Date
- Dec 2012
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Need help in classic asp for retaining the selected value from dropdown after submit.
Here is the sample code :
The dropdown loads from database,I want it to retain the selected value after submit.
<select name="ddlPriority">
<%
DIM strPriority
DO WHILE NOT objRS.EOF
strPriority = objRS.Fields(0)
%>
<option value><%=strPriority%></option>
<%
objRS.MoveNext
Loop
objRS.Close
Set objRS = Nothing
Please help me on this.
%>
</select
-
Apr 10, 2013, 21:51 #2
- Join Date
- Jun 2007
- Posts
- 691
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
example code is here:
http://stackoverflow.com/questions/2...fter-post-back
or google :
classic ASP retain selected value
-
Apr 10, 2013, 22:03 #3
- Join Date
- Dec 2012
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi ,
I have already visited the given site,however my dropdown is loaded from database values,so I do not have the value to compare and the given example is for fixed values.if my database values are 1,2,3,4 .I have selected 2 then when I submit the page,it should show me the 2 as selected along with 1,3,4 as other options.
-
Apr 11, 2013, 02:33 #4
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
The answer IS there, but maybe not obvious to you. Some pseudo-code ...
Code ASP:<% iSel=Request.Form("ddlPriority") %> <select name="ddlPriority"> <% DIM strPriority DO WHILE NOT objRS.EOF strPriority = objRS.Fields(0) sSel="" If strPriority=iSel Then sSel=" selected=""selected""" %> <option value="<%=strPriority%>"<%=sSel%>><%=strPriority%></option> <% objRS.MoveNext Loop objRS.Close Set objRS = Nothing %> </select>
-
Apr 11, 2013, 02:38 #5
- Join Date
- Dec 2012
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I could finally make the solution for this.Here it is
<%
Dim dstrSQL
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("conn")
Set oRs = Server.CreateObject("ADODB.Recordset")
dstrSQL="select Name from ProjectPriority order by Name"
oRs.Open dstrSQL, objConn, 1,3
WHILE NOT oRs.EOF
if request.QueryString("Postback") = "true" and oRs("Name") = request("dropdown_menu") then
prioritytypes = prioritytypes & "<option selected value=" & oRs("Name") & ">" & oRs("Name") & "</option>"
ELSE
prioritytypes = prioritytypes & "<option value=" & oRs("Name") & ">" & oRs("Name") & "</option>"
END IF
oRs.MoveNext
WEND
oRs.Close
objConn.Close
%>
<html><head><title>Dynamic Drop-Down Menu Example</title></head>
<body>
<form method="POST" action="preview.asp?Postback=true">
<p><select size="1" name="dropdown_menu">
<%=prioritytypes%>
</select></p>
<input type=submit value="submit"/>
</form>
</body>
</html>
-
Apr 11, 2013, 05:05 #6
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
selected="selected" is the correct HTML terminology (or in your case selected=""selected"", as you'd be using it inside a string) but yes, what you have done is fine. I would also ...
Dim prioritytypes
prioritytypes=""
... at the top to be syntactically correct and to preload the variable with an empty string.
-
Apr 11, 2013, 05:26 #7
- Join Date
- Dec 2012
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you Siteguru !! will add te dim prioritytypes and initialize with empty string :-)
Bookmarks