利用checkbox 删除gridview里的数据

<asp:GridView id="GridView1" runat="server" DataKeyNames="id" AllowSorting="True" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" SkinID="gvSkin" Width="100%" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" Width="3%" />
<ItemTemplate>
<asp:CheckBox ID="ckbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameNews" HeaderText="新闻标题" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="TypeNews" HeaderText="新闻类型">
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="PeopleNews" HeaderText="发布人">
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="DateNews" DataFormatString="{0:yyyy-MM-dd}" HeaderText="添加日期"
HtmlEncode="False">
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="ButtonNews" HeaderText="点击数">
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="News_Modfiy.aspx?id={0}"
HeaderText="操作" Text="修改" >
<ItemStyle HorizontalAlign="Center" Width="6%" />
</asp:HyperLinkField>
<asp:CommandField HeaderText="操作" ShowDeleteButton="True" DeleteText="<div id="de" οnclick="JavaScript:return confirm('确定删除吗?')">删除</div>" >
<ItemStyle HorizontalAlign="Center" Width="6%" />
</asp:CommandField>
</Columns>
</asp:GridView>
<asp:CheckBox id="CheckBox1" runat="server" Text=" 选中/取消所有" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged"></asp:CheckBox> <asp:Button id="Button1" οnclick="Button1_Click" runat="server" Text="删除选中项" OnClientClick='return confirm("你确定要删除?")'></asp:Button>

=================================.cs代码

protected void Button1_Click(object sender, EventArgs e)
{
for (int rowindex = 0; rowindex < this.GridView1.Rows.Count; rowindex++)
{
if (((CheckBox)this.GridView1.Rows[rowindex].Cells[0].FindControl("ckbDelete")).Checked == true)
{
int intID = Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value);
string strSql = "DELETE FROM OC_Product WHERE id=" + intID;
//对数据库执行删除操作省略
}
}
ShowNews();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("ckbDelete")).Checked = true;
}
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("ckbDelete")).Checked = false;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值