If condition not working in classic ASP

一笑奈何 提交于 2019-12-23 16:05:09

问题


I am new to classic ASP. What is wrong with the code below: If condition error, don't get it. Please help.

<select NAME="Priority" style="WIDTH:200px"  Id="Priority">
  <option value='0' <%= if(condition) then "selected" end%> 0 </option>
  <option value='1' <%= if(condition) then "selected" end%> 1 </option>
  <option value='2' <%= if(condition) then "selected" end%> 2 </option>
  <option value='3' <%= if(condition) then "selected" end%> 3 </option>
</select>

回答1:


Should be :

<% if condition then response.write("selected") %>

For more info see here:

http://www.codefixer.com/tutorials/If_then_else.asp




回答2:


There is no in-line if function (note I didn't say statement) in VBScript. So I always have this in my toolbox:

function iif(siONo, SiRetval, NoRetval )
    if SiONo then
        iif = SiRetval
    else
        iif = NoRetval
    end if
end function

Which allows you to do:

<option value='0' <%= iif(condition, "selected", "") %> 0 </option>



回答3:


If the select (Priority) is getting its data from a database then you could use a function. Is the page updating or inserting?

<select NAME="Priority" style="WIDTH:200px"  Id="Priority">
<option value="0" <%= isSelected(Priority,"0") %>>0</option>
<option value="1" <%= isSelected(Priority,"1") %>>1</option>
<option value="2" <%= isSelected(Priority,"2") %>>2</option>
<option value="3" <%= isSelected(Priority,"3") %>>3</option>
</select>


    Function isSelected(x,y)
    if Cstr(x) = Cstr(y) then
        isSelected = "selected=""Selected"""
    else
        isSelected = ""
    end if
end Function

The item Priority would be a numeric field and have a default value assigned. Place the function snippet in a page that is used globally.




回答4:


You were missing if in 'end if' and there was a extra closing tag missing in after closing asp code block %>. Hope this works.

<select NAME="Priority" style="WIDTH:200px"  Id="Priority">
  <option value="0" <% if(condition) then Response.write("selected") end if %>>0</option>
  <option value="1" <% if(condition) then Response.write("selected") end if %>>1</option>
  <option value="2" <% if(condition) then Response.write("selected") end if %>>2</option>
  <option value="3" <% if(condition) then Response.write("selected") end if %>>3</option>
</select>


来源:https://stackoverflow.com/questions/8663025/if-condition-not-working-in-classic-asp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!