如果条件不适用于经典ASP

时间:2011-12-29 01:10:40

标签: asp-classic

我是经典ASP的新手。下面的代码有什么问题:如果条件错误,请不要得到它。请帮忙。

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

4 个答案:

答案 0 :(得分:4)

应该是:

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

有关详细信息,请参阅此处:

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

答案 1 :(得分:1)

VBScript中没有in-line if函数(注意我没说声明)。所以我总是在我的工具箱中有这个:

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

允许您这样做:

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

答案 2 :(得分:0)

如果select(Priority)从数据库获取数据,则可以使用函数。 页面是更新还是插入?

<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

项目优先级将是一个数字字段,并且已分配默认值。将功能代码段放在全局使用的页面中。

答案 3 :(得分:-1)

如果在'end if'中并且在关闭asp代码块%&gt;之后缺少额外的结束标记,则会丢失。希望这有效。

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