• GridView中实现全选与取消全选,以便同时删除多条数据


    我们项目后台操作中不可避免的会有同时删除多项的操作,本文实现的就是当点击全选时,选定当前页中所有项,当取消了某一项的选定,则“全选”CheckBox的checked也为false;然后在后台中取到所选定的项的ID,实现同时删除多项的功能。

    前台代码

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewsForm.aspx.cs" Inherits="Admin_NewsForm" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" language="javascript">
    function isOrNotCheckAll(checkValue)
    {
    var arrAllCheckBoxes = document.getElementsByTagName("input");
    //check all
    if(checkValue==true)
    {
    for(i=0;i<arrAllCheckBoxes.length;i++)
    {
    if(document.getElementById(arrAllCheckBoxes[i].id).type=="checkbox")
    {
    document.getElementById(arrAllCheckBoxes[i].id).checked=true;
    }
    }
    }
    //do not check any one
    if(checkValue==false)
    {
    for(i=0;i<arrAllCheckBoxes.length;i++)
    {
    if(document.getElementById(arrAllCheckBoxes[i].id).type=="checkbox")
    {
    document.getElementById(arrAllCheckBoxes[i].id).checked=false;
    }
    }
    }
    }
    function CBDeleteClick(clientID,checkAllClientID)
    {
    var arrAllCheckBoxes = document.getElementsByTagName("input");
    //there has some one not be checked
    if(document.getElementById(clientID).checked==false)
    {
    for(i=0;i<arrAllCheckBoxes.length;i++)
    {
    if(arrAllCheckBoxes[i].id.indexOf("CheckAll")!=-1)
    {
    arrAllCheckBoxes[i].checked=false;
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:Image ID="Image1" runat="server" ImageUrl="HXImages/button/btn_add.gif" />
    <asp:ImageButton ID="BtnDelete" runat="server"
    ImageUrl="~/Admin/HXImages/button/btn_del.gif" onclick="BtnDelete_Click" />
    <asp:GridView ID="GVNews" runat="server" AutoGenerateColumns="False" DataKeyNames="newsID"
    Width="100%" onrowdatabound="GVNews_RowDataBound"
    onrowdeleting="GVNews_RowDeleting" AllowPaging="True"
    onpageindexchanging="GVNews_PageIndexChanging" PageSize="3" >
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    全选
    <asp:CheckBox ID="CheckAll" runat="server" OnClick="javascript: return isOrNotCheckAll (this.checked);" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="CBDelete" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>

    <asp:BoundField DataField="newsID" HeaderText="序号" >
    <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:BoundField DataField="newsTitle" HeaderText="标题">
    <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="添加日期">
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
    <%# Convert.ToDateTime(Eval("newsDate")).ToString("yyyy-MM-dd")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="newsDegree" HeaderText="点击次数(次)">
    <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="操作">
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
    <asp:ImageButton ID="ImgDel" title="删除" CommandName="delete" ImageUrl="HXImages/del.gif" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </form>
    </body>
    </html>

    后台代码

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class Admin_NewsForm : System.Web.UI.Page
    {
    string sql = null;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGVNews();
    BtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗?');");
    }
    }
    void BindGVNews()
    {
    sql = string.Format("select * from newsTable order by newsID desc");
    DataTable dt = DataBase.ExecuteDataSet(sql).Tables[0];
    GVNews.DataSource = dt.DefaultView;
    GVNews.DataBind();
    }
    protected void GVNews_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    string newsID = GVNews.DataKeys[e.RowIndex].Value.ToString();
    sql = string.Format("delete from newsTable where newsID={0}", Convert.ToInt32(newsID));
    int i = DataBase.ExecuteNonQuery(sql);
    if (i > 0)
    {
    BindGVNews();
    }
    }
    protected void GVNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    GVNews.PageIndex = e.NewPageIndex;
    BindGVNews();
    }
    protected void GVNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    ((ImageButton)e.Row.FindControl("ImgDel")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
    ((CheckBox)e.Row.FindControl("CBDelete")).Attributes.Add("onclick", "CBDeleteClick('"+((CheckBox)e.Row.FindControl("CBDelete")).ClientID+"')");
    }
    }
    string GetDeleteIDs()
    {
    string IDs = null;
    foreach (GridViewRow gvr in GVNews.Rows)
    {
    CheckBox cb = (CheckBox)gvr.FindControl("CBDelete");
    if (cb.Checked)
    {
    if (IDs == null)
    {
    IDs = GVNews.DataKeys[gvr.RowIndex].Value.ToString();
    }
    else
    {
    IDs = IDs+"," + GVNews.DataKeys[gvr.RowIndex].Value.ToString();
    }
    }
    }
    return IDs;
    }
    protected void BtnDelete_Click(object sender, ImageClickEventArgs e)
    {
    string newsIDs = this.GetDeleteIDs();
    if (newsIDs != null)
    {
    sql = string.Format("delete from newsTable where newsID in ({0})", newsIDs);
    int i = DataBase.ExecuteNonQuery(sql);
    if (i > 0)
    {
    BindGVNews();
    }
    }
    else
    {
    Response.Write("<script>alert('请指定要删除的项!');</script>");
    }
    }
    }

  • 相关阅读:
    Oracle9使用oradata恢复数据库
    我该怎么安排下属的工作项目经理如何分配任务
    如果说中国的程序员技术偏低,原因可能在这里
    项目经理问:为什么总是只有我在加班 – 挂包袱现象
    【转】面试真经
    [JAVA]PING和TELNET用法介绍
    Hello World 你懂的
    线程间操作控件
    获取客户端相关信息
    winfrom 特效 [转载]
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/3854965.html
Copyright © 2020-2023  润新知