经典ASP:使用If语句时为空

时间:2012-06-01 14:53:29

标签: asp-classic

我有一些简单的代码填充了db表中的select对象。我想检查一个值以将条目设置为选中,但是当我检查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>"  

感谢您的帮助!

2 个答案:

答案 0 :(得分: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>" 
   ...

答案 1 :(得分: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