ASP.NET2.0下含有CheckBox的GridView删除选定记录实例

1<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"PageSize="5"
2Width="726px"AllowPaging="True"AllowSorting="True"
3DataKeyNames="DB1_1,DB1_2"OnRowDeleting="GridView1_RowDeleting"OnPageIndexChanging="GridView1_PageIndexChanging"OnRowDataBound="GridView1_RowDataBound"OnSorting="GridView1_Sorting"Height="279px">
4<Columns>
5<asp:TemplateFieldHeaderText="序号">
6<ItemTemplate>
7<%#this.GridView1.PageIndex*this.GridView1.PageSize+this.GridView1.Rows.Count+1%>
8</ItemTemplate>
9</asp:TemplateField>
10<asp:TemplateFieldHeaderText="选择">
11<ItemTemplate>
12<asp:CheckBoxID="chkSelect"runat="server"></asp:CheckBox>
13</ItemTemplate>
14</asp:TemplateField>
15<asp:TemplateFieldHeaderText="操作"ShowHeader="False">
16<ItemTemplate>
17<asp:LinkButtonID="lbtnDelete"runat="server"CausesValidation="False"CommandName="Delete"
18Text="删除"OnClientClick="returnconfirm('确认要删除吗?');"></asp:LinkButton>
19</ItemTemplate>
20</asp:TemplateField>
21</Columns>
22<AlternatingRowStyleBackColor="Aquamarine"/>
23</asp:GridView>

1<asp:CheckBoxID="chkSelectAll"runat="server"Text="全部选中"OnCheckedChanged="chkSelectAll_CheckedChanged"AutoPostBack="True"></asp:CheckBox>
2<asp:ButtonID="btnDelete"runat="server"Text="删除"OnClick="btnDelete_Click"/>

1protectedvoidbtnDelete_Click(objectsender,EventArgse)
2{
3stringstrDelete="";
4for(inti=0;i<this.GridView1.Rows.Count;i++)
5{
6stringLabel;
7boolisChecked=((CheckBox)GridView1.Rows[i].FindControl("chkSelect")).Checked;
8Label=((Label)GridView1.Rows[i].FindControl("labXH")).Text;
9if(isChecked)
10{
11strDelete="DB1_1"+"="+Label;
12}
13}
14conn.RunSql("DeletefromDB1where"+strDelete
15this.chkSelectAll.Checked=false;
16GridViewBind();
17}
18
19protectedvoidchkSelectAll_CheckedChanged(objectsender,EventArgse)
20{
21//遍历GridView行获取CheckBox属性
22for(inti=0;i<this.GridView1.Rows.Count;i++)
23{
24((CheckBox)GridView1.Rows[i].FindControl("chkSelect")).Checked=this.chkSelectAll.Checked;
25}
26}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值