• JS全选反选脚本结合GridView


    前台:(每个脚本函数其中有一个GridView1的ID需要更改)
    <body>
    <script type="text/javascript" language="javascript">
     function selectAll(object)
      { 
       
        table = document.all.GridView1;
        var chk = table.getElementsByTagName("input");
        for(var i=0;i<chk.length;i++)
        {
         chk[i].checked = object.checked;
        }
      }
      function UnselectAll()
            {
                table = document.all.GridView1;
          var chk = table.getElementsByTagName("input");
       for(var i=0;i<chk.length;i++)
       {
         if(chk[i].id!="chkAll")
         {
        if(chk[i].checked)
        {
           chk[i].checked=!chk[i].checked;
        }
        else
        {
             chk[i].checked=true;
        }
         }
       }
      }

            function  checkselectAll()
            {
               
                var num=0;//未选中
                table = document.all.GridView1;
          var chk = table.getElementsByTagName("input");
       for(var i=0;i<chk.length;i++)
       {
         if(!chk[i].checked)
         {
           num++;
         }
         if(num>0)
         {
            for(var j=0;j<chk.length;j++)
              {
                 if(chk[j].id=="chkAll"&&chk[j].checked==true)
                 {
                 chk[j].checked=false;
              }
           }
         }
       
       
       }
       
      }
      function checkselect()
      {
         var count=0;//选中
         table = document.all.GridView1;
          var chk = table.getElementsByTagName("input");
       for(var i=0;i<chk.length;i++)
       {
         if(chk[i].checked)
         {
           count++;
         }
         if(count==5)
         {
         for(var k=0;k<chk.length;k++)
         {
          if(chk[k].id=="chkAll"&&chk[k].checked==false)
          {
           chk[k].checked=true;
          }
          break;
         }
         }
        }
      }
    </script>

     

    <asp:TemplateField>
                        <HeaderTemplate>
                           <input id="chkAll" onclick="selectAll(this)" type="checkbox" name="chkAll">
               <asp:LinkButton id="btnDelete" runat="server" CommandName="delet" Height="6px" CausesValidation="False">删除</asp:LinkButton>

                        </HeaderTemplate>
                        <ItemTemplate>
                            <input id="chk" onclick="checkselectAll();checkselect()" type="checkbox" value='<%#DataBinder.Eval(Container.DataItem,"id").ToString()%>' name="chk" runat="server" />

                        </ItemTemplate>
    </asp:TemplateField>

     


    <input id="chkUnSelect" title="反选" onclick="UnselectAll(this)" type="checkbox" name="反选">反选(随便放在哪里)

    后台:


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           //if(e.CommandName=="select")
           //{
           //    Response.Redirect("index.aspx");
           //}
            #region 删除所有选中记录

            if (e.CommandName == "delet")
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox check;

                foreach (GridViewRow dgi in GridView1.Rows)
                {
                    check = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)dgi.Cells[12].FindControl("chk"));
                    if (check != null)
                    {
                        if (check.Checked)
                        {
                            Response.Write("选中的值主键值为:" + check.Value + "<br>");
                        }
                    }
                }

            }

            #endregion
           
        }

  • 相关阅读:
    LoadRunner调用java函数测试oracle
    pam_cracklib.so模块
    crontab定时任务安装、使用方法
    yum的repo文件详解、以及epel简介、yum源的更换
    ubuntu添加开机自启和sysv-rc-conf
    MySQL配置参数详解
    集群管理软件clustershell
    Mysql命令大全
    Nginx配置文件nginx.conf 详解
    linux下iptables配置详解
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1217431.html
Copyright © 2020-2023  润新知