C#全选 后 进行 批量循环删除

C#全选_后_进行_批量循环删除


    <div class="wrap" style="width: 579px; height: 191px">
    <table width="1159">
        <thead>
            <tr>
                <th width="53">
                    <input type="checkbox" id="j_cbAll" />
                </th>
                <th>ID</th>
                <th>标题</th>
                <th width="171">时间</th>
                <th width="147">作者</th>
                <th>点击量</th>  
                <th>图片</th>              
                <th width="81">文件大小</th>  
                <th>编辑</th>   
                <th>预览</th>
                <th>删除</th>                    
                
            </tr>
        </thead>
        <tbody id="j_tb">
        
        


        <asp:Repeater ID="Repeater1" runat="server" >  
        <ItemTemplate>
        
        
            <tr>
                <td width="53">

                    <asp:CheckBox ID="CheckBox1" runat="server" />

                </td>
                <td >  <asp:Label ID="lieID" runat="server" Text= <%#Eval("ID") %>> </asp:Label> </td>
                <td width="185"><%#Eval("标题") %></td>
                <td width="171"><%#Eval("时间") %></td>
                <td width="147"><%#Eval("作者") %></td>  
                <td width="77"><%#Eval("浏览量") %></td>  
                <td width="139"> <img src="<%#Eval("缩略图1") %>" width="135" height="99" style="margin:0 auto;"></td>     
			  <td width="81"><img src="../zip.jpg" width="50" height="50" style="margin:0 auto;"><%#Eval("文件大小") %></td> 
			  <td width="121"><input type="button" name="a1" class="button" value="编辑"></td> 			  
			  <td width="120"><a href="../news.aspx?=<%#Eval("ID") %>" target="_blank"><input type="button" name="a2" class="button2" value="预览"></td> 	
			  <td width="149"><input type="button" name="a3" class="button3" value="删除"></td>         
         
            </tr>
            
            
            
              </ItemTemplate>   
    </asp:Repeater>  







    <div>
    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true"
     PageSize="6" CssClass="paginator"  CurrentPageButtonClass="cpb"
    LastPageText="尾页" FirstPageText="首页" PrevPageText="上页" NextPageText="下页"
     UrlPaging="false" NumericButtonTextFormatString="{0}"
      onpagechanged="AspNetPager1_PageChanged" 
            CustomInfoTextAlign="Left" LayoutType="Table" 
            onpagechanging="AspNetPager1_PageChanging" 
            CustomInfoHTML="页数 %CurrentPageIndex% of %PageCount%" ShowBoxThreshold="3" 
            UrlPageIndexName="页数" NumericButtonCount="3" CustomInfoSectionWidth="20%" SubmitButtonStyle="button3" >
</webdiyer:AspNetPager>




    <input id="Button1" type="button" value="全选/取消" onclick="javascript:document.getElementById('j_cbAll').click();" class="button"/>

    <input id="Button2" type="button" value="批量删除" onclick="querenshanchu()" class="button3" />

    <asp:Button ID="del" runat="server" Text="Button" onclick="del_Click" style="display:none" />



        </tbody>
    </table>
</div>



<script>
    var all = document.getElementById("j_cbAll");
    var tbody = document.getElementById("j_tb");
    var checkboxs = tbody.getElementsByTagName("input");
    all.onclick = function () {
        for (var i = 0; i < checkboxs.length; i++) {
            var checkbox = checkboxs[i];
            checkbox.checked = this.checked;

            


        }
    };
    for (var i = 0; i < checkboxs.length; i++) {
        checkboxs[i].onclick = function () {
            var isCheckedAll = true;
            for (var i = 0; i < checkboxs.length; i++) {
                if (!checkboxs[i].checked) {
                    isCheckedAll = false;
                    break;
                }
            }
            all.checked = isCheckedAll;
        };
    }
    
 
    
    </script>






<script>


 function querenshanchu()
{
    var r = confirm("您确认要删除吗?")
    if (r == true)
    {
       // alert("确认删除"); /////  触发点击按钮

        document.getElementById('del').click();


    }
    else
    {
       alert("取消了");
    }
}

</script>


    
后台代码:


  protected void del_Click(object sender, EventArgs e)
    {

          CheckBox CheckBox2;

           string idList = "";//用来存放选中的多条记录的ID


            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                CheckBox2 = (CheckBox)Repeater1.Items[i].FindControl("CheckBox1");
                if (CheckBox2.Checked == true)
                {
                  //如果选中 就取得该行的Label2标签的值,也就是该记录的ID
                    Label lbId = (Label)Repeater1.Items[i].FindControl("lieID");
                 

                    idList = idList.Trim() + lbId.Text.ToString() + ",";

                }          


            }
            if (idList.Substring(idList.Length - 1) == ",")
            {

                idList = idList.Substring(0, idList.Length - 1);
            }
            string[] num = idList.Split(',');
            for (int j = 0; j < num.Length; j++)
            {

                long id = Convert.ToInt64(num[j]);



                ///执行删除SQL
                string MyConn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
                SqlConnection MyConnection = new SqlConnection(MyConn);
                string MyDelete = "Delete  from 文章列表 where ID='" + id + "' ";
                SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
                MyConnection.Open();
                MyCommand.ExecuteNonQuery();
                MyConnection.Close();



                Response.Write("<script>alert('删除文章ID为:"+id+"成功!');location.href='list.aspx';</script>");
            }
            


        }


webform
桂ICP备16004754号