读书人

asp怎么根据选中的CheckBox批量插入更

发布时间: 2013-01-07 10:02:25 作者: rapoo

asp如何根据选中的CheckBox批量插入更新了的记录
前台代码如下:


<form>
<%
set rs=conn.execute("select id,price from tbname")
while not rs.eof
%>
<input type="checkbox" name="id" value="<%=rs("id")%>">
<input type="text" name="price" value="">
<%
rs.movenext
wend
set rs=nothing
%>
<input type="submit" value="批量修改">
</form>


后台处理提交的代码如下:

selectnum=request.form("id").count
for i=1 to selectnum
id=request.Form("id")(i)
price=request.Form("price")(i)
conn.execute("insert into tbname(id,price) values("&id&","&price&")")
next


后台这样写,出现的问题是:id是选中的id值,但request.Form("price")返回的是所有price控件的值,这样就无法插入对应id的准备数据。请问如何只对选中的行进行插入操作?

[解决办法]
用相同的name无法实现,因为checkbox只提交选择了的name,所以跟price无法对应。
你只能所它们取成不同名字,比如
<input type="checkbox" name="id1" value="<%=rs("id")%>">
<input type="text" name="price1" value="">
<input type="checkbox" name="id2" value="<%=rs("id")%>">
<input type="text" name="price2" value="">
<input type="checkbox" name="id3" value="<%=rs("id")%>">
<input type="text" name="price3" value="">
......
这样才能对应起来
[解决办法]

<form>
<%
i=1
set rs=conn.execute("select id,price from tbname")
while not rs.eof
%>
<input type="checkbox" name="id" value="<%=rs("id")%>">
<input type="text" name="price" value="">
<%i=i+1
rs.movenext
wend
set rs=nothing
%>
<input type="hidden" name="selectnum" value="<%=i-1%>">
<input type="submit" value="批量修改">
</form>

selectnum=request.form("id").count
for i=1 to request("selectnum")
id=request.Form("id"&i)
price=request.Form("price"&i)
conn.execute("insert into tbname(id,price) values("&id&","&price&")")
next

读书人网 >ASP

热点推荐