asp.Netでdatagrid checkboxの全選択を実現する方法
4440 ワード
<br>dim conn as oledbconnection
<br>dim ds as dataset
<br>sub Page_load(sender as object,e as eventargs)
<br>if not ispostback then
<br>calldb()
<br>else
<br>ds=session("ds")
<br>end if
<br>
<br>end sub
<br>sub calldb()
<br>conn = new oledbconnection("provider=Microsoft.jet.oledb.4.0;data source="& server.MapPath("wwwlink.mdb"))
<br>dim sqlstr="select * from link"
<br>conn.open()
<br>dim adp as new oledbdataAdapter(sqlstr,conn)
<br> ds =new dataset()
<br>adp.fill(ds,"link")
<br>mydg.DataSource=ds.tables("link").defaultview
<br>mydg.databind()
<br>conn.close()
<br>session("ds")=ds
<br>end sub
<br>sub btn1_onclick(sender as object, e as eventargs)
<br>dim chk as checkbox
<br>dim id as string
<br>dim datag as datagriditem
<br>conn = new oledbconnection("provider=Microsoft.jet.oledb.4.0;data source="& server.MapPath("wwwlink.mdb"))
<br>dim sqlstr as string
<br>dim cmd as oledbcommand
<br>
<br>dim i as integer
<br>'for each datag in mydg.items
<br>
<br>for i=0 to mydg.items.count-1
<br>'chk=ctype(datag.findcontrol("checkdel"),checkbox)
<br>'chk=datag.findcontrol("checkdel")
<br>chk=mydg.items(i).cells(0).findcontrol("checkdel")
<br>if chk.checked then
<br>id=cstr(mydg.items(i).cells(1).text)
<br>'id=cstr(mydg.datakeys(datag.itemindex))
<br>conn.open()
<br>sqlstr="delete from link where link_id=" & id
<br>cmd=new oledbcommand(sqlstr,conn)
<br>cmd.executenonquery
<br>'ds.tables("link").rows(i).delete()
<br>conn.close()
<br>end if
<br>
<br>next
<br>'session("ds")=ds
<br>'mydg.edititemindex=-1
<br>'mydg.datasource=ds.tables("link").defaultview
<br>'mydg.databind()
<br>calldb()
<br>end sub
<br>sub cancel_click(sender as object, e as eventargs)
<br>calldb()
<br>end sub
<br>
<br>sub ok_click(sender as object, e as eventargs)
<br>conn=new oledbconnection("provider=Microsoft.jet.oledb.4.0;data source="&server.MapPath("wwwlink.mdb"))
<br>dim adp as new oledbdataadapter("select * from link",conn)
<br>dim ocb as new oledbcommandbuilder(adp)
<br>adp.updatecommand=ocb.getupdatecommand()
<br>adp.deletecommand=ocb.getdeletecommand()
<br>adp.update(ds,"link")
<br>calldb()
<br>end sub
<br>dim checka as checkbox
<br>sub itemcreate(sender as object, e as datagriditemeventargs)
<br>if (e.item.itemtype=Listitemtype.Header) then
<br>checka=e.item.findcontrol("checkall")
<br>end if
<br>end sub
<br>
<br>sub chkall_onchanged(sender as object,e as eventargs)
<br>
<br>dim i as integer
<br>if checka.checked=true then
<br>for i=0 to mydg.items.count-1
<br>
<br>dim chk as checkbox=mydg.items(i).cells(0).findcontrol("checkdel")
<br>chk.checked=true
<br>next
<br>else
<br>for i=0 to mydg.items.count-1
<br>dim chk as checkbox=mydg.items(i).cells(0).findcontrol("checkdel")
<br>chk.checked=false
<br>next
<br>
<br>end if
<br>end sub
<br>