• 在GridView中实现全选反选的例子


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView控件.aspx.cs" Inherits="WebApplication1.GridView控件" %>
    
    <!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 language="javascript" type="text/javascript" >
            //反选
            function ReverseSelect() {
                var checkbox = document.all.CheckboxGroup;
                if (checkbox == null) 
                {
                    return false;
                }
                if (checkbox.length + "" != "undefined")
                 {
                    for (var i = 0; i < checkbox.length; i++) {
                        checkbox[i].checked = !checkbox[i].checked;
                    }
                }
                else {
                    //修正当列表长度为1,不能反选
                    checkbox.checked = !checkbox[i].checked;
                }
                return false;
            }
            //全选
            function SelectAll() {
             var checkbox = document.all.CheckboxGroup;
                if (checkbox == null) 
                {
                    return false;
                }
                if (checkbox.length + "" != "undefined") {
                    for (var i = 0; i < checkbox.length; i++) {
                        checkbox[i].checked = !checkbox[i].checked;
                    }
                }
                else {
                    checkbox.checked = true;
                }
                return false;
            }
            //检查至少选一项
            function CheckHasSelectedItem() {
                var checkbox = document.all.CheckboxGroup;
                 if (checkbox == null) 
                {
                    return false;
                }
                if (checkbox.length + "" != "undefined") {
                    for (var i = 0; i < checkbox.length; i++)
                     {
                        if(checkbox[i].checked)
                        {
                        return true;
                        }
                    }
                }
                else {
                       return false;
                }
            }
            //删除用户前的确认
            function ConfirmDelete() {
                if (CheckHasSelectedItem())//如果少选择一项
                {
                    return confirm("确定删除该用户?");
                }
                else {
                    alter("至少选择一项");
                    return false;
    
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:GridView ID="gvUserList" runat="server" AutoGenerateColumns="false" Width="800px" AllowPaging="True"
                onpageindexchanging="gvUserList_PageIndexChanging" PageSize="5">
            <Columns>
            <asp:TemplateField>
             <ItemTemplate>
             <input name ='CheckboxGroup' type='checkbox' value='<%#Eval("ID")%>'/>
             </ItemTemplate>
            </asp:TemplateField>
             <asp:BoundField DataField="ID" HeaderText="编码" />
             <asp:HyperLinkField  DataNavigateUrlFields="ID" 
               DataNavigateUrlFormatString=" GridView控件.aspx?ID={0}"
                DataTextField="RealName" HeaderText="查看" />
             <asp:BoundField DataField="UserName" HeaderText="用户姓名" />
             <asp:BoundField DataField="RealName" HeaderText="真实姓名" />
             <asp:BoundField DataField="Age" HeaderText="年龄" />
             <asp:CheckBoxField DataField="Sex" HeaderText="" />
             <asp:BoundField DataField="Mobile" HeaderText="手机" />
             <asp:TemplateField HeaderText="电子邮件">
               <AlternatingItemTemplate>
                <a href='emailto:<%#Eval("Email")%>'>发邮件给<%#Eval("RealName")%></a>
               </AlternatingItemTemplate>
               <ItemTemplate>
                <%#Eval("Email")%>
               </ItemTemplate>
             </asp:TemplateField>
            </Columns>
          </asp:GridView>
          <table border="0" width="800" cellpadding="0" cellspacing="0">
           <tr><td><a style="text-decoration:underline" href="#" onclick="SelectAll();">全选</a></td>
           <td><a style="text-decoration:underline" href="#" onclick="ReverseSelect();">反选</a></td>
             <td>
             <asp:Button ID="btnDelete" runat="server" OnClientClick="javascript:return ConfirmDelete();"
             OnClick="btnDelete_Click" Text="删除" />
             </td>
           </tr>
          </table>
        </div>
        </form>
    </body>
    </html>

    CS代码:

    sing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace WebApplication1
    {
        public partial class GridView控件 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    //默认显示第一页 但索引为0
                 BindGridView(0);
                }
            }
            //指定绑定页面的数据
            private void BindGridView(int pageIndex)
            {
                SqlConnection conn = new SqlConnection(@"server=Rose-PCSQLEXPRESS;Database=User;User Id=sa;password=");
                SqlCommand command = new SqlCommand("Select * from UserInfo", conn);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable data = new DataTable();
                adapter.Fill(data);
    
                #region 注意这部分代码可以在设计视图中设置,不必卸载代码里
                gvUserList.AllowPaging = true;//设置允许自动分页
                //gvUserList.AutoGenerateColumns=false;//设置不允许自动绑定
                gvUserList.PageSize = 5;
                #endregion
    
                gvUserList.DataSource = data;
                gvUserList.PageIndex = pageIndex;//设置显示第几页
                gvUserList.DataBind();
            }
            //翻页事件
            
            protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                //指定新页面,重新绑定数据
                BindGridView(e.NewPageIndex);
            }
    
           
            //删除选中用户
            protected void btnDelete_Click(object sender, EventArgs e)
            {
                string Sql = "delete from UserInfo where ID in (" +Request["CheckboxGroup"] + ")";
                SqlConnection conn = new SqlConnection(@"server=Rose-PCSQLEXPRESS;Database=User;User Id=sa;password=");
                SqlCommand command = new SqlCommand(Sql, conn);
                conn.Open();
                int count = command.ExecuteNonQuery();
                conn.Close();
                //删除成功后给出提示
                if (count >0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success",
                        "<script language='javascript'>alert('删除成功!');" + "window.location='MultiSelectGridView.aspx';</script>");
    
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "fail",
                        "<script language='javascript'>alert('删除成功!');</script>");
                }
            } 
            
        }
    }
  • 相关阅读:
    我的第一个开源项目
    读headFirst设计模式
    读headFirst设计模式
    读headFirst设计模式
    eclipse中svn插件的安装和tortoiseSVN的安装
    读headFirst设计模式
    浅析博客园的保存密码并自动登录, 然后自己写一个demo
    java中易遗忘的知识,不定时更新……
    POI操作Excel
    java泛型基础
  • 原文地址:https://www.cnblogs.com/ai394495243/p/3356175.html
Copyright © 2020-2023  润新知