我是编程的新手。我想使用ASP Classic构建一个Web应用程序,从SQL Server中提取数据并将其显示在网页上。
在SQL Server Express的测试数据库中,我有3个具有以下结构的表:
在Person
表中,有FirstName
和LastName
列。我使用从Internet获得的以下脚本使用ASP Classic创建了选择菜单:
<%
'Set Variable
Dim objCN 'ADO Connection Object
Dim objRS 'ADO Recordset Object
'Create a Connection Object
Set objCN = Server.CreateObject("ADODB.Connection")
'Connect to SQL Server Database, connection string in Global.asa file
objCN.Open Application("ConnStrTestDB")
'SQL Query for select Person Name
strSQLPerson="SELECT FirstName, LastName, PersonID FROM Persons ORDER BY FirstName ASC"
'Create Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQLPerson, objCN
%>
<form method="post" action="testDBSelectProductType.asp">
Choose Person Name <select name="FirstName">
<%
Do Until objRS.EOF
Response.Write("<option")
If objRS.Fields("FirstName")=FirstName Then
Response.Write("selected")
End If
Response.Write(">")
Response.Write(objRS.Fields("FirstName"))
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
%>
</select>
<input type="submit" value="Click to continue..">
</form>
<%
'Close the Connection
objCN.Close
Set objCN = Nothing
%>
目前,Select菜单只能拔出FirstName
。
第一个问题,如何将LastName
添加到Select菜单?我希望选择菜单为“FirstName LastName”。
第二个问题,我如何将PersonID
(它是主键)包含在表单中?所以我可以将它用作下一个ASP页面的查询字符串参数。
真的适合任何帮助。
谢谢
<%
Do Until objRS.EOF
Response.Write("<option value=""" & objRS.Fields("PersonID") & """")
If objRS.Fields("PersonID")=PersonID Then
Response.Write("selected")
End If
Response.Write(">")
Response.Write(objRS.Fields("FirstName") & " " & objRS.Fields("LastName"))
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
%>
我喜欢这样。当选择项目时,我会在选项标签中插入“选定”一词。
<%
Do Until objRS.EOF
sSel="":If objRS.Fields("PersonID")=PersonID Then sSel=" selected "
Response.Write "<option value="""&objRs.Field("PersonID")&""" "&sSel&">"
Response.Write(objRS.Fields("FirstName") & " " & objRS.Fields("LastName"))
Response.Write "</option>"
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
%>
玩得开心Michael
你好,你可以尝试代码
<select>
<%
get_select=objRS.Fields("id")
Do while not objRS.EOF
%>
<option value="<%=objRS.Fields("id")%>" <%if get_select=objRS.Fields("id")then%> selected <%end if%>><%=objRS.Fields("FirstName")%> <%=objRS.Fields("LastName")%></option>
<%
objRS.MoveNext
Loop
%>
</select>
非常感谢Michael和A. Fabiani,你的解决方案都在运作。
这是Michael的解决方案:
<%
'Set Variable
Dim objCN 'ADO Connection Object
Dim objRS 'ADO Recordset Object
'Create a Connection Object
Set objCN = Server.CreateObject("ADODB.Connection")
'Connect to SQL Server Database, connection string in Global.asa file
objCN.Open Application("ConnStrTestDB")
'SQL Query for select Person Name
strSQLPerson="SELECT FirstName, LastName, PersonID FROM Persons ORDER BY FirstName ASC"
'Create Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQLPerson, objCN
%>
<form method="post" action="testDBSelectProductType2.asp">
Choose Person Name <select name="PersonID">
<%
DO Until objRS.EOF
sSel="":If objRS.Fields("PersonID")=PersonID Then sSel=" selected "
Response.Write "<option value="""&objRS.Fields("PersonID")&""" "&sSel&">"
Response.Write(objRS.Fields("FirstName") & " " & objRS.Fields("LastName"))
Response.Write "</option>"
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
%>
</select>
<input type="submit" value="Click to continue..">
</form>
<%
'Close the Connection
objCN.Close
Set objCN = Nothing
%>
以下是A Fabiani的解决方案:
<%
'Set Variable
Dim objCN 'ADO Connection Object
Dim objRS 'ADO Recordset Object
'Create a Connection Object
Set objCN = Server.CreateObject("ADODB.Connection")
'Connect to SQL Server Database, connection string in Global.asa file
objCN.Open Application("ConnStrTestDB")
'SQL Query for select Person Name
strSQLPerson="SELECT FirstName, LastName, PersonID FROM Persons ORDER BY FirstName ASC"
'Create Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQLPerson, objCN
%>
<form method="post" action="testDBSelectProductType2.asp">
Choose Person Name <select name="PersonID">
<%
Do Until objRS.EOF
Response.Write("<option value=""" & objRS.Fields("PersonID") & """")
If objRS.Fields("PersonID")=PersonID Then
Response.Write("selected")
End If
Response.Write(">")
Response.Write(objRS.Fields("FirstName") & " " & objRS.Fields("LastName"))
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
%>
</select>
<input type="submit" value="Click to continue..">
</form>
<%
'Close the Connection
objCN.Close
Set objCN = Nothing
%>
然后我使用查询表单在下一个ASP页面上获取PersonID,如下所示:
'Get PersonID value from Form
PersonID = Request.Form("PersonID")
我希望上面的代码可以帮助那些像我一样面对同样问题的人。