从表格中的按钮调用ASP功能

时间:2014-03-30 14:56:53

标签: html asp-classic

我在asp经典中创建一个动态表,每行有一个按钮,我需要这个按钮才能激活一些功能..

在某种原因,按钮剂量反应..

源代码:

 <%
set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rs = con.Execute("Select ProdID,ProductName,BrandName,CategoryName,Description,Price FROM Products P,Brands B,Categories C WHERE P.mode = true AND InStock=true AND P.CatID =C.CatID AND SupID = BrandID")
Dim filePath  
Dim Id
Dim nNumOfItems 
Dim NumStr
 nNumOfItems = 0
Do Until rs.EOF
nNumOfItems = nNumOfItems + 1
Id = rs.Fields("ProdID").Value
Response.Write "<tr>"
Response.Write "<td>"
Response.Write rs.Fields("ProductName").Value
Response.Write "</td>"
Response.Write "<td>"
Response.Write "<button  name = ' DeleteProduct '  value ='DeleteProduct' class = 'button' onClick='DeleteProduct ('Id')'>"
Response.Write "Delete Product"
Response.Write "</button>"
Response.Write "</td>"
Response.Write "</tr>"
rs.MoveNext
     Loop

    rs.Close
  Set rs = Nothing  
   con.Close
       Set con = Nothing
   %>

   <%
  Function DeleteProduct(Id)
  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  set rs= "Update products SET mode = false WHERE ProdId= "&cint(Id)&""
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 

  End Function
  %>

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

经典ASP是服务器端代码,您的按钮需要是一个提交输入,将表单发布到服务器

Do Until rs.EOF
nNumOfItems = nNumOfItems + 1 %>
<form method="post">
<input type="hidden" name="id" value="<%=rs.Fields("ProdID").Value %>">
<tr>
<td>
<%= rs.Fields("ProductName").Value %>
</td>
<td>
<input type="submit" name="DeleteProduct" Value="Delete Product">
</td>
</tr>
</form>
<% rs.MoveNext
     Loop

编辑 - 这是如何进行数据库查询。在任何HTML之前,此代码应位于页面的开头。条件(if)语句检查是否有表单提交。如果有,则执行代码

<%
If Request.Form("DeleteProduct") <>"" then
  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  con.Execute ("Update products SET mode = false WHERE ProdId="& cint(Request.Form("Id"))
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 
End If
%>

答案 1 :(得分:2)

如果你想要一个循环产品并一次删除一个项目的方法,我会使用每个项目的链接。它更轻,更简单的代码,你可以设置链接的样式。

您的代码使用链接而不是表单按钮:

<%
set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rs = con.Execute("Select ProdID,ProductName,BrandName,CategoryName,Description,Price FROM Products P,Brands B,Categories C WHERE P.mode = true AND InStock=true AND P.CatID =C.CatID AND SupID = BrandID")
Dim filePath  
Dim Id
Dim nNumOfItems 
Dim NumStr
 nNumOfItems = 0
Do Until rs.EOF
nNumOfItems = nNumOfItems + 1
Id = rs.Fields("ProdID").Value
Response.Write "<tr>"
Response.Write "<td>"
Response.Write rs.Fields("ProductName").Value
Response.Write "</td>"
Response.Write "<td>"

Response.Write "<a href='DeleteProduct.asp?ID='" & ID & "'>"
Response.Write "Delete Product"
Response.Write "</a>"

Response.Write "</td>"
Response.Write "</tr>"
rs.MoveNext
     Loop

    rs.Close
  Set rs = Nothing  
   con.Close
       Set con = Nothing
   %>

DeleteProduct.asp:

<%
ID = Request("ID") 

&#39;如果它是一个数字

,请确认其格式与您希望的格式相同,如isNumeric(ID)
  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  con.Execute ("Update products SET mode = false WHERE ProdId=" & ID)
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 
%>

附加说明:

  1. 如果您希望允许用户一次删除多个产品,请使用您已开始使用的表单代码,但是在每个订单项旁边放置复选框,只需一个按钮即可#&quot;全部删除检查&#34;
  2. 当您开始使用SQL Server时,您应该在存储过程中执行此操作以控制权限并避免SQL注入攻击。