DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: dropdown list

  1. #1
    Join Date
    Jan 2006

    Question dropdown list

    I am having trouble with a dropdown list. I have a dropdownlist that updates another dropdown list. It does a refresh on the page and then the first dropdown list I select looses the value I selected..but the second dropdown list does retain the changes from the first. How do i give the first dropdown list that contains the OnChange() function back the value I selected? see code below...

    ' variables
    dim mytime
    dim mySQL
    dim accessdb
    dim conntemp
    dim rstemp
    dim strconn
    dim showblank
    dim shownull
    dim alldata
    dim numcols
    dim numrows
    dim dropdown
    dim SQL_dd
    dim myrs
    dim strClients

    strClients = ""
    mytime = Time
    'on error resume next

    accessdb=server.mappath("New Time sheet Tables.mdb")
    strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
    strconn=strconn & accessDB & ";"

    set conntemp=server.createobject("adodb.connection")
    Set rs = Server.CreateObject("ADODB.Recordset")
    Set rs2 = Server.CreateObject("ADODB.Recordset")
    Set rs3 = Server.CreateObject("ADODB.Recordset")
    Set rs4 = Server.CreateObject("ADODB.Recordset") strconn


    'Get clientcode from querystring
    strClients = Request.QueryString("CList")

    mySQL_DL = "Select ClientCode From Clients2"
    set rs=conntemp.Execute(mySQL_DL)


    <select name="ClientList" size="1" ONCHANGE="HandleChange()">
    <% Do While Not rs.EOF %>
    <option value="<%= rs.Fields("ClientCode").Value %>"><%= rs.Fields("ClientCode").Value %></option>

    set rs=nothing

    'This is where I tried to reset the dropdown list back to what i selected..

    'For Each objOption in ClientList.Options

    For i = 0 to ClientList.Items.Count -1

    If ClientList.InnerText = strClients Then
    ClientList.SelectedIndex = i
    End If


    ' I also tried this... it didn't work either....
    'Dim i
    'If value <> "" Then
    ' For i = 0 To ClientList.Items.Count - 1
    ' If ClientList.Items(i).Value = Request.QueryString("CList") Then
    ' ClientList.SelectedIndex = i
    ' End If
    ' Next
    'End If


    mySQL_DL = "Select Project From Projects"
    set rs2=conntemp.Execute(mySQL_DL)

    <select name="ProjectList" size="1">
    <% Do While Not rs2.EOF %>
    <option value="<%= rs2.Fields("Project").Value %>"><%= rs2.Fields("Project").Value %></option>

    mySQL_DL = "Select Category From [Project Category]"
    set rs3=conntemp.Execute(mySQL_DL)

    <select name="ProjectCategory" size="1">
    <% Do While Not rs3.EOF %>
    <option value="<%= rs3.Fields("Category").Value %>"><%= rs3.Fields("Category").Value %></option>
    <select name="ProjectVendor" size="1">

    mySQL_DL = "Select VendorName, VendorID From Vendors Where ClientCode = '" & strClients & "'"

    set rs4 = conntemp.Execute(mySQL_DL)

    Do While Not rs4.EOF %>
    <option value="<%= rs4.Fields("VendorName").Value %>"><%= rs4.Fields("VendorName").Value %> - <%= rs4.Fields("VendorID").Value %></option>
    <option value=""></option>


    ' close connection
    'set rs=nothing
    'set rs2=nothing
    'set rs3=nothing
    'set rs4=nothing

    set conntemp=nothing


    <Script language="JavaScript">
    function HandleChange()
    box = document.TimeAlloc.ClientList;
    mValue = box.options[box.selectedIndex].value;
    //alert ("xx " + mValue + " xx");
    window.location.href = "TimeAllocation.asp?CList=" + mValue ;


  2. #2
    Join Date
    Dec 2003
    San Diego
    A brief look at your code, it seems you are confusing javascript and ASP/VBScript. SelectedIndex is a property of the Select element in the client-side DOM. You cannot set these values in ASP. However, you have a couple options or techniques you can take advantage of here.

    1) when the user submits the form, capture the selected option of the first list box. You don't need to process it but simply use it when looping thru your option tags. If the value of your option tag matches the value of the option submitted, add the SELECTED attribute to the option tag.

    2) Use ASP to write out the javascript necessary to set the selected index for the proper option in your select element.

    3) You can ride the latest technique train and process all this in AJAX. it will give you constant access to the client side form, process your data in real-time to the client, and avoid a visible round trip to the server (no page reloading). AJAX is actually pretty simple. It just requires a little knowlege of XML and familiarity with javascript parsing of XML and the document object.

    Detailed Article
    Get Right To It Article

    Note: You should make it a practice to *not* switch back and forth between HTML and the ASP shortcut for the write method, <%= %>. This is poor practice for performance as it causes IIS to constantly switch back and forth between the ASP.DLL and the standard output. Anywhere you have the <%= %> directive, preceed that line with repsonse.write and concatenate your literals and variables.

    Good luck,
    Michael Sanchez
    Managing Technical Editor
    Forum Moderator

  3. #3
    Join Date
    Dec 2013
    I happen to see this old thread and find this be an insightful issue to me. I am not quite familiar with the things on the DropDown List control.

Similar Threads

  1. Populating 2 Dropdown List control
    By Nelson in forum ASP.NET
    Replies: 2
    Last Post: 05-08-2014, 11:04 PM
  2. Autocompleting Dropdown List
    By Jeff Johnson in forum .NET
    Replies: 2
    Last Post: 10-29-2002, 03:11 PM
  3. Replies: 1
    Last Post: 08-28-2002, 09:55 AM
  4. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 01:22 PM
  5. Replies: 0
    Last Post: 06-13-2000, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center