dropdown list


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: dropdown list

  1. #1
    Join Date
    Jan 2006
    Posts
    5

    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")

    conntemp.open strconn
    %>

    <%

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

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

    %>

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

    rs.close
    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
    Next

    %>
    </select>
    <br>
    <%

    ' 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)
    %>

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

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

    <b>Category:</b>
    <select name="ProjectCategory" size="1">
    <% Do While Not rs3.EOF %>
    <option value="<%= rs3.Fields("Category").Value %>"><%= rs3.Fields("Category").Value %></option>
    <%
    rs3.MoveNext
    Loop
    %>
    </select>
    <br>
    <b>Vendor:</b>
    <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>
    <%
    rs4.MoveNext
    Loop
    %>
    <option value=""></option>


    </select>


    <%
    ' close connection
    'rs.close
    'set rs=nothing
    'rs2.close
    'set rs2=nothing
    'rs3.close
    'set rs3=nothing
    'rs4.close
    'set rs4=nothing

    conntemp.close
    set conntemp=nothing


    %>


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



    </Script>

  2. #2
    Join Date
    Dec 2003
    Location
    San Diego
    Posts
    202
    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
    FreeVBCode.com

Similar Threads

  1. Populating 2 Dropdown List control
    By Nelson in forum ASP.NET
    Replies: 1
    Last Post: 06-23-2003, 10:03 AM
  2. Autocompleting Dropdown List
    By Jeff Johnson in forum .NET
    Replies: 2
    Last Post: 10-29-2002, 02: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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