This demo uses 2 list box's, both are populated by a database. Depending on the selection that is chosen in the first dropdown box, the results will reflect that choice. I've received countless email's how to use 2 listbox's on the same page that are interconnected. I've used frames in this demo, below is both the frames page and the individual pages w/the code.
The main frame page-This page ties all the individual pages together.
Frame1.asp -This contains the dropdown box that people choose the entry's.
<% 'Good coding practices to declare all your local variables Dim conn Dim rs Dim strconn Dim DBQValue
' Automatic DataConnection ' This value is generated on the fly Session("DataConn_ConnectionString") = _ "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("2listbox.mdb") ' End of Automatic DataConnection strconn= Session("DataConn_ConnectionString") set conn= server.createobject("adodb.connection") set rs= server.createobject("adodb.recordset") conn.open strconn strsql="SELECT * FROM dropdown" rs.open strsql, conn, 2, 2 %>
I do an if statement to determine what browser people are using. If a person is using Internet Explorer this will load this version of the html page. Or below in the Else it will load a dropdown with a submit button. I de-test javascript and could have used client code to submit the form, but I'm prefer this way. It might not be the correct way but what the heck this is free code!
<% If Instr(request.servervariables("http_user_agent"), "MSIE") Then %>
<html>
<head> <title>Demo Drop Down</title> </head>
<body> <script language="vbscript"> sub submitthis
form1.submit
end sub </script>
<p>Select from the dropdown menu</p><br> <form ACTION="frame2.asp" METHOD="POST" NAME="form1" target="right"> <p><select ONCHANGE="submitthis" SIZE="1" NAME="one"> <% Do While Not rs.EOF %> <option value="<% = rs("type") %>"> <% = rs("field1") %></option> <% rs.movenext %><% loop %> </select> <br> </p> </form> </body> </html>
'This part of the If statement is loaded if the browser is netscape! Sorry but this person selects 'the information and has to click the submit button! <% Else %>
<html> <head><title>dude></title></head> <body>
<p>Select from the dropdown menu</p><br> <form ACTION="frame2.asp" METHOD="POST" NAME="form1" target="right"> <p><select SIZE="1" NAME="one"> <% Do While Not rs.EOF %> <option value="<% = rs("type") %>"> <% = rs("field1") %></option> <% rs.movenext %><% loop %> </select> <br> <input type="submit" name="b1" value="submit"></p> </form> </body> </html> <% End If %>
<% rs.close conn.close set rs = nothing set conn = nothing
%>
Frame2.asp - This page shows the resultset that is chosen from the first dropdown box.
<% 'Good coding practices to declare all your local variables 'Does an If statement to see if the request("one") dropdown menu has a value or not If request("one") <> "" Then %> <% Dim conn Dim rs Dim strconn Dim DBQValue ' Automatic DataConnection ' This value is generated on the fly Session("DataConn_ConnectionString") = _ "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("swynk2.mdb") ' End of Automatic DataConnection strconn= Session("DataConn_ConnectionString") set conn= server.createobject("adodb.connection") set rs2= server.createobject("adodb.recordset") conn.open strconn strsql="SELECT table1.fielddata FROM table1 WHERE ((table1.fieldkey)='" & request("one") & "');" rs2.open strsql, conn, 2, 2 %> <html> <head> <title>Load when Empty</title> </head> <body> <form> <p>The results of what was selected from the first dropdown.</p><br> <p><select name="d2" size="1"> <% Do While Not rs2.EOF %> <option value="<% = rs2("fielddata") %>"><% = rs2("fielddata") %></option> <% rs2.movenext %><% loop %> </select><br> </p> </form> </body> </html> <% rs2.close conn.close set rs2 = nothing set conn = nothing %>
This part gets loaded when the page first loads, when it first loads it is just a blank dropdown box.
<% Else %> <html> <head> <title>Load when Empty</title> </head> <body>
<form> <p><select name="d1" size="1"> <option value=""></option> </select><br> </p> </form> </body> </html> <% End If %>
Bottom.asp - This is the major portion of the screen, this is just an example of populating something from the 2nd dropdown menu. This could be instructions or whatever!
<html>
<head> <title></title> </head> <body> <center>From the 2nd Drop down results could load something to this page<br>I'm still working on this demo so check back for enhancements! </center> </body> </html>