2014-02-06 36 views
0

以下代碼片段不起作用,有人可以幫助我確定問題。我使用的ASP在IIS 7.0上ASP嵌套如果不工作

代碼:

<%If session("var") <> "" Then 
    If(instr(strSQL("Platform"), session("osversion")) > 0 ) Then %> 
     <input type="image" src="images/download2.gif" name="submit" value="submit" /> 
    <%Else %> 
     <p style="font-weight:bold"> SOME ERROR MESSAGE</p> 
    <%End If %>   
<%Else %> 
    <input type="image" src="images/download2.gif" name="submit" value="submit" /> 
<%End If %> 

是與IIS 7.0配置爲傳統的ASP的問題?

+1

你說的 「不工作」 是什麼意思?怎麼了?預期的結果是什麼?嘗試在語句前輸出什麼會話(「var」) – pee2pee

+0

是否有錯誤?請將其添加到您的帖子中。 – Stuart

回答

0

嘗試刪除嵌套If中的「()」。如下面的代碼:

<%If session("var") <> "" Then 
    If instr(strSQL("Platform"), session("osversion")) > 0 Then %> 
     <input type="image" src="images/download2.gif" name="submit" value="submit" /> 
    <%Else %> 
     <p style="font-weight:bold"> SOME ERROR MESSAGE</p> 
    <%End If %>   
<%Else %> 
    <input type="image" src="images/download2.gif" name="submit" value="submit" /> 
<%End If %> 

現在,什麼都happends第一,如果條件If session("var") <> "" Then,應該永遠是由於<%Else %>輸出。問題將被修剪到nested if。檢查strSQL("Platform")session("osversion")的值。如果這兩個都沒問題,現在檢查instr(,)。你甚至可以做If instr(strSQL("Platform"), session("osversion")) > 0 Then改爲If true Then只是爲了檢查它是否有效。

0
  • 儘量避免<%和%>的雜亂,這使得它無法讀取。
  • 其次,不要在 代碼中的不同位置重複相同的字符串。
  • 第三次嘗試合併邏輯。
  • 四拔出串

代碼...

  • 命題答:會議( 「VAR」)<> 「」
  • 命題B:INSTR(STRSQL(「平臺「),會話(」 OSVERSION「))> 0

您的代碼:

if a 
    if b 
     do alfa 
    else 
     do bravo 
else 
    do alfa 

改寫:

if not a or b 
    do alfa 
else 
    do bravo 

現在在ASP

<% 
dim showButton, errMsg 
showButton = "<input type='image' src='images/download2.gif' name='submit' value='submit' />" 
errMsg = "<p style='font-weight:bold'> SOME ERROR MESSAGE</p>" 
If session("var") <> "" Then 
    If(instr(strSQL("Platform"), session("osversion")) > 0 ) Then 
     response.write showButton 
    Else 
     response.write errMsg 
    End If 
Else 
    response.write showButton 
End If 
%> 



<% 
dim showButton, errMsg 
showButton = "<input type='image' src='images/download2.gif' name='submit' value='submit' />" 
errMsg = "<p style='font-weight:bold'> SOME ERROR MESSAGE</p>" 
If session("var") = "" or (instr(strSQL("Platform"), session("osversion")) > 0) Then 
    response.write showButton 
Else 
    response.write errMsg 
End If 
%> 



<% 
If session("var") = "" or (instr(strSQL("Platform"), session("osversion")) > 0) Then 
    response.write "<input type='image' src='images/download2.gif' name='submit' value='submit' />" 
Else 
    response.write "<p style='font-weight:bold'> SOME ERROR MESSAGE</p>" 
End If 
%>