以前经常用asp.net写东西,最近因为工作原因需要修改一个asp程序,其中遇到的一个问题是将下拉列表框中的任意一个值,设置为默认值。而一般都是第一个是被选择的。如果是其他的,那么就需要你去编程了。下面是我写的测试代码如下:
<!--#include file="conn.asp"-->
<%
On Error Resume next
response.charset="GB2312"
session.codepage=936
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>test</title>
</head>
<body>
<form name="form1" method=post>
<h1>test</h1>
<select name="my_t" >
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select name="c" style="height:18px;" >
<%set rs=conn.execute("select distinct p_sort as mysort from 56770_pro_log ")
if not rs.eof then
do while not rs.eof
var bol_add_item=0
if Trim(rs("mysort")) ="感冒药" then
bol_add_item = 1
%>
<option value="<%=Trim(rs("mysort"))%>" selected><%=rs("mysort")%></option>
<%
else
%>
<option value="<%=Trim(rs("mysort"))%>"><%=rs("mysort")%></option>
<%
end if
rs.movenext
loop
end if
rs.close:set rs=nothing
%>
<%
if bol_add_item = 0 then
%>
<option selected value="">--请选择--</option>
<%
end if
%>
</select>
</form>
</body>
</html>
====================
下面的部分代码是,我正式程序中应用到的代码:
<select name="p_sort1" onChange="(document.form1.p_sort.value=this.options[this.selectedIndex].value)"> <% set rsa=server.CreateObject("adodb.recordset") rsa.open "select * from 56770_sort order by s_paixu desc", conn, 1, 1 if rsa.eof and rsa.bof then response.write "还没有数据,请添加!" else bol_add_item=0 do while not rsa.eof if rsa("s_name") = rs("p_sort") then bol_add_item=1 %> <option value="<%=rsa("s_name")%>" selected><%=rsa("s_name")%></option> <% else %> <option value="<%=rsa("s_name")%>"><%=rsa("s_name")%></option> <% end if %> <% rsa.movenext loop end if rsa.close set rsa=nothing%> <% if bol_add_item = 0 then %> <option selected value="">--请选择--</option> <% end if %> </select> <input type=text name="p_sort" value="<%=rs("p_sort")%>" size="20">