0

我有一段简单的代码从数据库表中填充一个选择对象。我想检查一个值以将一个条目设置为选中,但是当我检查一个 db 值时,我得到一个空的选择框。此代码生成空框:

response.write "<td><select name='FromDept'>"
Do While not rs.eof
   If rs("DeptID")  = 61 Then 
      response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName")     & "</option>" 
   Else    
      response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
   End If 
   rs.MoveNext     
Loop
rs.close
response.write "</select></td>"

但是,此代码会生成一个带有值的选择框:

response.write "<td><select name='FromDept'>"
LpCnt = 0 
Do While not rs.eof

    If LpCnt = 9 Then 
       response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>" 
    Else    
       response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
    End If 
    rs.MoveNext     
    LpCnt = LpCnt + 1 
Loop
rs.close
response.write "</select></td>"  

谢谢你的帮助!

4

2 回答 2

1

将值分配给临时变量:

response.write "<td><select name='FromDept'>"
Do While not rs.eof
   dept = rs("DeptID")
   If dept  = 61 Then 
      response.write "<option value=" & dept & " selected>" & rs("DeptName")     & "</option>" 
   ...
于 2012-06-01T14:58:45.230 回答
0

空下拉意味着您在该语句中遇到错误,并且很可能由于On Error Resume Next您在某处有行而被忽略。

首先,摆脱那条On Error Resume Next线。

其次,此类代码中的错误意味着类型转换问题。试试这个代码:

Dim curDeptID
Do While not rs.eof
   curDeptID = 0
   If Not IsNull(rs("DeptID")) Then
       curDeptID = CLng(CStr(rs("DeptID")))
   End If
   If curDeptID=61 Then 
      response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName")     & "</option>" 
   Else    
      response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
   End If 
   rs.MoveNext     
Loop
于 2012-06-03T07:16:04.340 回答