经典的asp验证服务器的结果

时间:2014-01-03 15:58:51

标签: asp-classic

我正在尝试检查或取消选中复选框取决于来自服务器的数据结果。但是我不能正确地使用下面的代码,我做错了吗?

<%
Dim AFTER_SAVE, IN_VIEW, Y
Dim SQL, Data
SQL = " SELECT code, name, value FROM mytable WHERE code = '" & User & "'" 
Data = Data(SQL)
%>  
    <%If IsArray(Data) Then%>
         <%If ((Data(1,0) = "AFTER_SAVE") AND (Data(2,0) = "Y")) Then %>      
                document.getElementById("chkSave").checked == true;                      
         <%End If%>
         <% If ((Data(1,0) = "IN_VIEW") AND (Data(2,0) = "Y")) Then %>       
                document.getElementById("chkVIEW").checked == true;  
         <%End If%>
  <%End If%>

1 个答案:

答案 0 :(得分:1)

您正试图以非常奇怪的方式将服务器端代码与客户端代码结合起来。有时,有必要这样做(即使用服务器端VBScript编写客户端Javascript),但如果我正确理解你的意图,那么就不需要了。

基本上,如果这实际上是一个经典的ASP页面,那么在该页面的某个位置,您将生成相关的复选框。因此,您需要做的就是在此之前将数据库调用放在某处,然后在生成复选框时,您可以输出checked='checked'或不输出,具体取决于。

请注意,我不清楚Data = Data(SQL)应该是什么意思。它没有办法成为有效的VBScript代码 - 括号是针对数组的,但字符串不是有效的数组索引,然后将它自己分配给它自己?无论如何,我忽略了那一部分。

<html>
<head>
<%
Dim AFTER_SAVE, IN_VIEW
Dim SQL, RS, Conn
Dim User
'...blah blah blah, give a value to User, set up your DB connection, etc. etc....

SQL = "SELECT code, name, [value] FROM mytable WHERE code = '" & User & "'"
'- ("value" is a reserved keyword in SQL, hence the brackets)
Set RS = Server.Createobject("ADODB.Recordset")
RS.Open SQL, Conn, 1, 2 '- this is rather handwavy and unlikely to actually
'- work as-is; use the options and connection methods that make sense for you
Do Until RS.EOF
    '- I have no idea how your data is set up; this may make no sense.
    '- The idea is, read the checkbox states from your database, and 
    '- stick them in variables for later reference.
    Select Case RS("name")
        Case "AFTER_SAVE" AFTER_SAVE = RS("value")
        Case "IN_VIEW"   IN_VIEW = RS("value")
    End Select
    RS.Movenext
Loop
RS.Close
Set RS = Nothing
%>
</head>
<body>
<form method="post" action="myformhandler.asp">
<!-- form fields and stuff -->
<input type="checkbox" name="chkSave" id="chkSave" <%
If AFTER_SAVE = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkSave">After save</label>
<input type="checkbox" name="chkView" id="chkView" <%
If IN_VIEW = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkView">In view</label>
<!-- more form stuff -->
</form>
</body>
</html>