<form runat="server">
<asp:datagrid autogeneratecolumns="false" onitemcreated="itemcreate" datakeyfield="link_id" id="mydg" runat="server" >
<columns>
<asp:templatecolumn>
<headertemplate>
<asp:checkbox id="checkall" oncheckedchanged="chkall_onchanged" autopostback="true" runat="server"></asp:checkbox>
</headertemplate>
<itemtemplate>
<asp:checkbox id="checkdel" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn datafield="link_id" headertext="link_id" ></asp:boundcolumn>
<asp:boundcolumn headertext="网站名称" datafield="sitename" />
<asp:boundcolumn headertext="网站地址" datafield="url" />
<asp:boundcolumn headertext="网站介绍" datafield="intro" />
<asp:boundcolumn headertext="网站评分" datafield="grade" />
<asp:boundcolumn headertext="提交时间" datafield="submit_date"></asp:boundcolumn>
</columns>
</asp:datagrid>
<asp:label id="lb1" runat="server"></asp:label>
<asp:button id="btn1" text="删 除" runat="server" onclick="btn1_onclick"></asp:button>
<asp:button id="ok" onclick="ok_click" text="保存修改" runat="server"></asp:button>
<asp:button id="cancel" onclick="cancel_click" text="取消修改" runat="server"></asp:button>
</form>
<%@ import namespace="system.data"%>
<%@ import namespace="system.data.oledb"%>
<script language="vb" runat="server">
dim conn as oledbconnection
dim ds as dataset
sub page_load(sender as object,e as eventargs)
if not ispostback then
calldb()
else
ds=session("ds")
end if
end sub
sub calldb()
conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source="& server.mappath("wwwlink.mdb"))
dim sqlstr="select * from link"
conn.open()
dim adp as new oledbdataadapter(sqlstr,conn)
ds =new dataset()
adp.fill(ds,"link")
mydg.datasource=ds.tables("link").defaultview
mydg.databind()
conn.close()
session("ds")=ds
end sub
sub btn1_onclick(sender as object, e as eventargs)
dim chk as checkbox
dim id as string
dim datag as datagriditem
conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source="& server.mappath("wwwlink.mdb"))
dim sqlstr as string
dim cmd as oledbcommand
dim i as integer
'for each datag in mydg.items
for i=0 to mydg.items.count-1
'chk=ctype(datag.findcontrol("checkdel"),checkbox)
'chk=datag.findcontrol("checkdel")
chk=mydg.items(i).cells(0).findcontrol("checkdel")
if chk.checked then
id=cstr(mydg.items(i).cells(1).text)
'id=cstr(mydg.datakeys(datag.itemindex))
conn.open()
sqlstr="delete from link where link_id=" & id
cmd=new oledbcommand(sqlstr,conn)
cmd.executenonquery
'ds.tables("link").rows(i).delete()
conn.close()
end if
next
'session("ds")=ds
'mydg.edititemindex=-1
'mydg.datasource=ds.tables("link").defaultview
'mydg.databind()
calldb()
end sub
sub cancel_click(sender as object, e as eventargs)
calldb()
end sub
sub ok_click(sender as object, e as eventargs)
conn=new oledbconnection("provider=microsoft.jet.oledb.4.0;data source="&server.mappath("wwwlink.mdb"))
dim adp as new oledbdataadapter("select * from link",conn)
dim ocb as new oledbcommandbuilder(adp)
adp.updatecommand=ocb.getupdatecommand()
adp.deletecommand=ocb.getdeletecommand()
adp.update(ds,"link")
calldb()
end sub
dim checka as checkbox
sub itemcreate(sender as object, e as datagriditemeventargs)
if (e.item.itemtype=listitemtype.header) then
checka=e.item.findcontrol("checkall")
end if
end sub
sub chkall_onchanged(sender as object,e as eventargs)
dim i as integer
if checka.checked=true then
for i=0 to mydg.items.count-1
dim chk as checkbox=mydg.items(i).cells(0).findcontrol("checkdel")
chk.checked=true
next
else
for i=0 to mydg.items.count-1
dim chk as checkbox=mydg.items(i).cells(0).findcontrol("checkdel")
chk.checked=false
next
end if
end sub
</script>