【求助】如何在按下按钮以后执行一段ASP语句
小弟写了一段代码
希望能在单击某个按钮以后执行一段程序
目的是更新数据库
其中conn已经定义好
主要代码如下:
[opendb.asp]
connstr = "***"
set conn = server.createobject("ADODB.CONNECTION")
conn.open connstr
[main.asp]
<!--#include file="opendb.asp"-->
<html><body>
<table>
<tr>
<td>
<input type="button" name="btn1" value="按钮1" />
<input type="button" name="btn2" value="按钮2"/>
</td>
</tr>
</table>
</body></html>
<script language="vbscript" event="onclick" for="btn1">
sql= "UPDATE table SET col = abc WHERE id = 123"
conn.execute(sql)
</script>
<script language="vbscript" event="onclick" for="btn2">
'some code
</script>
但是按下按钮以后,提示SCRIPT错误:缺少对象 "CONN"
如果我将这段语句直接写成
<%
sql= "UPDATE table SET col = abc WHERE id = 123"
conn.execute(sql)
%>
是可以顺利运行成功的
但是写成响应event的vbscript以后就不能执行了
请问各位这是什么原因呢?请指教。
或者给出能正确执行的代码,谢谢了!
问题解决立即揭帖
问题点数:50、回复次数:3Top
1 楼wzhiyuan(我是谁)回复于 2005-08-01 22:11:05 得分 45
响应鼠标等是客户端事件,数据库操作是服务器端事件。
要想执行服务器端代码,必须由客启端提交请求后,在服务器端执行。
你这个例子,可以写成
<input type=button value='更新数据' onclick="window.location='xxx.asp'">
其中xxx.asp里写你的数据库操作语句。
当然,你可以在xxx.asp的后面加一句response.redirect aaa.asp 来返回到你提交时的页面,可以造成客户端操作数据库的假象。呵。
Top
2 楼chjpeng(鹏(招聘.net web开发程序员))回复于 2005-08-01 22:12:44 得分 5
<!--#include file="opendb.asp"-->
<%
id=request("id")
sql= "UPDATE table SET col = abc WHERE id = '" & id & "'"
if len(id)>0 and isnumeric(id) then
conn.execute(sql)
end if
%>
<html><body>
<table>
<tr>
<td>
<input type="button" name="btn1" value="按钮1" onclick="javascript:window.location='main.asp?id=123'">
<input type="button" name="btn2" value="按钮2" onclick="javascript:window.location='main.asp?id=124'">
</td>
</tr>
</table>
</body></html>Top
3 楼goodloop(小志)回复于 2005-08-01 22:56:08 得分 0
wzhiyuan(我是谁)
谢谢给我提示
揭帖Top




