在Response.Write中调用经典ASP子过程

时间:2010-11-22 18:29:02

标签: asp-classic

我有一个经典的ASP问题。

尝试这样做:记录集是从1995年到2020年的简单列表;我试图让2010(当前年份)成为下拉列表中的默认选择。

问题:我试图在“Response.Write”中调用Sub proc,但它一直给我这个错误:

  

“错误'800a000d'类型不匹配:'selectyear'”

以下是代码,Attempt 1可以解决任何问题。但是当我将“if”逻辑移动到子过程并在Request.Write中调用它时,它会给我错误。

任何人都可以解释为什么Attempt1有效并且Attempt2不会。

' Attempt 1:

    rsYEAR.Open qYEAR, objconn, 0, 1 
    response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">" 
    dim selyr
    while not rsYEAR.EOF
        if CINT(rsYEAR.fields("year")) = year(now) then
            selyr = "selected"
        else selyr = ""
        end if

        Response.Write"<option value='" & rsYEAR.fields("year") & "' "& selyr &" >"  & cstr(rsYEAR.Fields("year"))
        rsYEAR.MoveNext
    wend
    response.Write "</select></td></tr>"
    rsYEAR.Close


' Attempt 2: 

    rsYEAR.Open qYEAR, objconn, 0, 1 
    response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">" 
    dim selyr2
    while not rsYEAR.EOF

        Response.Write "<option value='" & rsYEAR.fields("year") & "' " & cstr(selectyear(cint(rsYEAR.fields("year")))) &" >"  & cstr(rsYEAR.Fields("year")) 
        rsYEAR.MoveNext
    wend
    response.Write "</select></td></tr>"

    'close and clean up
    rsYEAR.Close
    set rsYEAR = nothing

非常感谢您的回复。 谢谢, 希瓦

2 个答案:

答案 0 :(得分:0)

我猜测cint(rsYEAR.fields("year"))正在抛出错误,因为有些数据无法转换为int。我希望这两种情况都会发生。

在第二次尝试中,您不应该需要cstr中的cstr(selectyear(cint(rsYEAR.fields("year")))),因为我认为selectyear已经返回了一个字符串。你能展示selectyear的代码吗?

答案 1 :(得分:0)

(如果没有正确答案,这怎么会这么久?)

Sub没有值,因此您无法响应。写入它。您需要使用函数,或将Sub调用放在自己的行上。

rsYEAR.Open qYEAR, objconn, 0, 1 
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">" 
dim y
while not rsYEAR.EOF
    y = rsYEAR.fields("year")
    Response.Write "<option value='" & y & "'" & IsCurr(y) & ">" & y & "</option>"
    rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close

Function IsCurr(yr)
    if Cstr(yr) = Cstr(year(now)) then
        IsCurr = " selected"
    else
        IsCurr = ""
    end if
End Function

使用sub而不是函数,这将成为

rsYEAR.Open qYEAR, objconn, 0, 1 
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">" 
dim y
while not rsYEAR.EOF
    y = rsYEAR.fields("year")
    Response.Write "<option value='" & y & "'"
    IsCurr y
    Response.Write ">" & y & "</option>"
    rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close

Sub IsCurr(yr)
    if Cstr(yr) = Cstr(year(now)) then
        Response.Write " selected"
    end if
End Sub