• 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
           
        }

  • 相关阅读:
    挑战程序设计竞赛 dp
    算法导论 动态规划
    算法导论第二章
    divide conquer
    时间戳
    bootstrap 针对超小屏幕和中等屏幕设备定义的不同的类
    jQuery中的Ajax
    怎么判断一个变量是Null还是undefined
    认识Ajax
    关于apache
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1217431.html
Copyright © 2020-2023  润新知