SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    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

  2. #2
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    675
    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

  3. #3
    SitePoint Member
    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.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    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>
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Member
    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>

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    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.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Member
    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 :-)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •