在asp.net中实现datagrid checkbox 全选的方法
更新时间:2006年12月29日 00:00:00 作者:
复制代码 代码如下:
<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>
<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>
您可能感兴趣的文章:
- asp.net GridView控件中模板列CheckBox全选、反选、取消
- asp.net Repeater取得CheckBox选中的某行某个值的c#写法
- asp.net gridview中用checkbox全选的几种实现的区别
- asp.net 获取Datalist中Checkbox的值的小结
- WPF的ListView控件自定义布局用法实例
- asp.net ListView 数据绑定
- ASP.NET中ListView(列表视图)的使用前台绑定附源码
- ASP.NET笔记之 ListView 与 DropDownList的使用
- asp.net ListView交替背景颜色实现代码
- WPF实现带全选复选框的列表控件
相关文章
C#,winform,ShowDialog,子窗体向父窗体传值
窗体的show方法,没有给调用代码任何通知,如果需要通知,使用showdialog是一种好的选择。 在调用show方法后,show方法后面的代码会立即执行,2008-08-08
最新评论