2012-06-01 69 views
0

我有一個簡單的代碼填充數據庫表中的選擇對象。我想檢查一個值來將條目設置爲選中狀態,但是當我檢查一個數據庫值時,我會得到一個空的選擇框。此代碼將產生空箱:經典ASP:使用If語句時的空選擇

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>" 

感謝您的幫助!

+0

看這裏 - http://stackoverflow.com/a/2032140/763026 –

回答

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>" 
    ... 
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