使用动态数据初始化下拉菜单


<%
Dim DataConn
Dim CmdPopulateStates
Dim SQL
Dim CURRENT_STATE_NAME
%>

This is just an example.. when doing this for real you would do a query to determine the existing values for all the form fields. For this example we will simply set a variable to the value for demonstration purposes.

<%
CURRENT_STATE_NAME = "New York"
%>

<%
Set DataConn = Server.CreateObject("ADODB.Connection")
Set CmdPopulateStates = Server.CreateObject("ADODB.Recordset")
%>

Take away the comment on Sytem DSN version below if you want to use a system DSN instead and add a comment to the DSN-LESS connection version below it

<%
' DataConn.Open "DSN=System_DSN_Name"

DataConn.Open "DBQ=" & Server.Mappath("_database/zipcodes.mdb") &   ";Driver={Microsoft Access Driver (*.mdb)};"

SQL = "SELECT DISTINCT STATE_NAME FROM STATES"
CmdPopulateStates.Open SQL, DataConn
%>

<form method="POST" action="somepage.asp">
<Select Name="STATE_NAME" size="1">
<%While Not CmdPopulateStates.EOF%>

<option <% If CURRENT_STATE_NAME = CmdPopulateStates("STATE_NAME") Then %>
<% Response.Write(" selected ") %><% End If %>value="<%= CmdPopulateStates("STATE_NAME") %>"><%= CmdPopulateStates("STATE_NAME") %></option>

<%
CmdPopulateStates.MoveNext
Wend
%>

<%
CmdPopulateStates.Close
Set CmdPopulateStates = Nothing
DataConn.Close
Set DataConn = Nothing
%>

</Select>
<input type="submit" value="Submit">
</form>