repeater 用控件分页,checkbox 全选 ,全部删除 ,单行删除 完整功能 !

时间:2021-07-19 10:55:06

 

前台 aspx 页

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " AdminProject.aspx.cs "  Inherits = " Admin_AdminProject "   %>

<% @ Register assembly = " AspNetPager "  namespace = " Wuqi.Webdiyer "  tagprefix = " webdiyer "   %>

<! 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 >
     < link  href ="../css/css.css"  rel ="stylesheet"  type ="text/css"   />
     < script  type ="text/javascript"  language ="JavaScript" >
               
<!--

        
function  CheckAll() {
            
var  dom  =  document.all;
            
var  el  =  event.srcElement;
            
if  (el.id.indexOf( " Ckb_All " >=   0   &&  el.tagName  ==   " INPUT "   &&  el.type.toLowerCase()  ==   " checkbox " ) {
                
var  ischecked  =   false ;
                
if  (el.checked)
                    ischecked 
=   true ;
                
for  (i  =   0 ; i  <  dom.length; i ++ ) {
                    
if  (dom[i].id.indexOf( " Ckb_Sel " >=   0   &&  dom[i].tagName  ==   " INPUT "   &&  dom[i].type.toLowerCase()  ==   " checkbox " )
                        dom[i].checked 
=  ischecked;
                }
            }

            
if  (el.id.indexOf( " Ckb_All1 " >=   0   &&  el.tagName  ==   " INPUT "   &&  el.type.toLowerCase()  ==   " checkbox " ) {
                
var  ischecked  =   false ;
                
if  (el.checked)
                    ischecked 
=   true ;
                
for  (i  =   0 ; i  <  dom.length; i ++ ) {
                    
if  (dom[i].id.indexOf( " Ckb_Sel " >=   0   &&  dom[i].tagName  ==   " INPUT "   &&  dom[i].type.toLowerCase()  ==   " checkbox " )
                        dom[i].checked 
=  ischecked;
                }
            }
        }

             
// -->
         </ script >
</ head >
< body  onclick ="CheckAll()" >
     < form  id ="form1"  runat ="server" >
    < table  width ="100%"  align ="center"  border ="0"  cellpadding ="0"  cellspacing ="0" >
     < tr >
     < td >
         < asp:Button  ID ="Button1"  runat ="server"  onclick ="Button1_Click"  Text ="删除选中项"   />
         </ td >
      < td  align ="center" >
         < webdiyer:AspNetPager  id ="pager1"  runat ="server"  ShowPageIndexBox ="Always"  
                                            PageSize
="30"  OnPageChanged ="AspNetPager1_PageChanged"  
                                            TextBeforeInputBox
="转到第  "  TextAfterInputBox ="  页  "  ShowPageIndex ="False"  
                                            ShowInputBox
="Always"  ShowCustomInfoSection ="Left"  PrevPageText ="上一页"  
                                            NextPageText
="下一页"  LastPageText ="尾页"  FirstPageText ="首页"  AlwaysShow ="True"  
                                            CustomInfoSectionWidth
="50%"  CustomInfoTextAlign ="NotSet" >
       </ webdiyer:AspNetPager >
         </ td >
     </ tr >
    </ table >
     < table  width ="100%"  align ="center"  border ="0"  cellpadding ="0"  cellspacing ="0" >
     < tr >
         < td  align ="center"  height ="10" ></ td >
     </ tr >
     </ table >
     < table   width ="100%"  align ="center"  border ="0"  cellpadding ="0"  cellspacing ="0"  bgcolor ="#000000" >
     < tr >
      < td  align ="center" > 

      < table  width ="100%"  align ="center"  cellpadding ="0"  cellspacing ="1"  border ="0"   >
          < asp:Repeater  ID ="Repeater1"  runat ="server"  
             onitemcommand
="Repeater1_ItemCommand"  onitemdatabound ="Repeater1_ItemDataBound" >
          < HeaderTemplate >
             < tr  >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >< asp:checkbox  id ="Ckb_All"  runat ="server" ></ asp:checkbox >全选 </ td >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >序号 </ td >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >项目名称 </ td >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >建设单位 </ td >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >修改 </ td >
              < td  height ="23"  background ="../images/tabletopcenter.gif" >删除 </ td >
             </ tr >
          </ HeaderTemplate >
          < ItemTemplate >
           < tr >
             < td  height ="25"  bgcolor ="#FFFFFF" >
             < INPUT  id ="txtID"  type ="hidden"  name ="txtID"  value ='<%#Eval("ID")% >' runat="server">
             < asp:CheckBox  id ="Ckb_Sel"  runat ="server"  Checked ="False" ></ asp:CheckBox >
             </ td >
             < td  height ="25"  bgcolor ="#FFFFFF" > <% #Container.ItemIndex + 1   %> </ td >
             < td  height ="25"  align ="left"  bgcolor ="#FFFFFF" >< href ="../info/ProjectDetail.aspx?id=<%#Eval(" ID")% >" target="_blank"> <% # Eval ( " p101 " ) %> </ a ></ td >
             < td  height ="25"  align ="left"  bgcolor ="#FFFFFF" >< href ="../info/CreditDetail.aspx?sname=<%#Eval(" p104")% >" target="_blank"> <% # Eval ( " p104 " ) %> </ a ></ td >
             < td  height ="25"  bgcolor ="#FFFFFF" >< href ="PubProject.aspx?id=<%#Eval(" ID")% >" target="_blank">修改 </ a ></ td >
             < td  height ="25"  bgcolor ="#FFFFFF" >
             < asp:ImageButton  CausesValidation ="False"  id ="ImageButton1"  runat ="server"  ImageUrl ="../images/delete.gif"  CommandArgument ='<%#Eval("ID")% > ' AlternateText="删除此项">
                                             </ asp:ImageButton >

             </ td >
          </ tr >
          </ ItemTemplate >
          < AlternatingItemTemplate >
           < tr >
             < td  height ="25"  bgcolor ="#FFFFFF" >
             < INPUT  id ="txtID1"  type ="hidden"  name ="txtID1"  value ='<%#Eval("ID")% >' runat="server">
             < asp:CheckBox  id ="Ckb_Sel1"  runat ="server"  Checked ="False" ></ asp:CheckBox >
             </ td >
             < td  height ="25"  bgcolor ="#FFFFFF" > <% #Container.ItemIndex + 1   %> </ td >
             < td  height ="25"  align ="left"  bgcolor ="#FFFFFF" >< href ="../info/ProjectDetail.aspx?id=<%#Eval(" ID")% >" target="_blank"> <% # Eval ( " p101 " ) %> </ a ></ td >
             < td  height ="25"  align ="left"  bgcolor ="#FFFFFF" >< href ="../info/CreditDetail.aspx?sname=<%#Eval(" p104")% >" target="_blank"> <% # Eval ( " p104 " ) %> </ a ></ td >
             < td  height ="25"  bgcolor ="#FFFFFF" >< href ="PubProject.aspx?id=<%#Eval(" ID")% >" target="_blank">修改 </ a ></ td >
             < td  height ="25"  bgcolor ="#FFFFFF" >
             < asp:ImageButton  CausesValidation ="False"  id ="ImageButton2"  runat ="server"  ImageUrl ="../images/delete.gif"  CommandArgument ='<%#Eval("ID")% > ' AlternateText="删除此项">
                                             </ asp:ImageButton >

             </ td >
          </ tr >
          </ AlternatingItemTemplate >
          </ asp:Repeater >
      </ table >


         </ td >
     </ tr >
    </ table >
     < table  width ="100%"  align ="center"  border ="0"  cellpadding ="0"  cellspacing ="0" >
     < tr >
         < td  align ="center"  height ="10" ></ td >
     </ tr >
     </ table >
     < table   width ="100%"  align ="center"  border ="0"  cellpadding ="0"  cellspacing ="0" >
     < tr >
      < td  align ="center" >
          < webdiyer:AspNetPager  ID ="pager2"  runat ="server"  CloneFrom ="pager1" >
                                         </ webdiyer:AspNetPager >
         </ td >
     </ tr >
    </ table >
     </ form >
</ body >
</ html >

 

后台 aspx.CS

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using DAL;

public  partial  class Admin_AdminProject : System.Web.UI.Page
{
    Db newdb =  new Db();
     protected  void Page_Load( object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
             this.Button1.Attributes.Add( " onclick "" return confirm('你确定删除所有选中信息吗?'); ");
            BindInfo();
        }

    }

     public  void BindInfo()
    {
         string sql =  " select * from PublicProject order by id desc ";

        DataSet dsss = newdb.CommonDataSet(sql);
         this.pager1.RecordCount = dsss.Tables[ 0].Rows.Count;

        DataSet ds = newdb.CommonWebDiyerDataSet(sql,  this.pager1.CurrentPageIndex,  this.pager1.PageSize);

         this.Repeater1.DataSource = ds.Tables[ 0].DefaultView;
         this.Repeater1.DataBind();

        pager1.CustomInfoHTML =  " 共:<font color=\"red\"><b> " +  this.pager1.RecordCount.ToString() +  " </b></font>&nbsp;记录 ";
        pager1.CustomInfoHTML +=  "  共:<font color=\"red\"><b> " + pager1.PageCount.ToString() +  " </b></font>&nbsp;页 ";
        pager1.CustomInfoHTML +=  "  每页:<font color=\"red\"><b> " + pager1.PageSize.ToString() +  " </b></font>&nbsp;条 ";
        pager1.CustomInfoHTML +=  "  当前:<font color=\"red\"><b> " + pager1.CurrentPageIndex.ToString() +  " </b></font> ";


    }
     protected  void Repeater1_ItemDataBound( object sender, RepeaterItemEventArgs e)
    {
         if (e.Item.ItemType == ListItemType.Item)
        {

            ImageButton button1 = (ImageButton)e.Item.FindControl( " ImageButton1 ");
            button1.Attributes.Add( " onclick ",
                 " return confirm (\"确定要删除此项记录吗?\"); ");

        }
         else  if (e.Item.ItemType == ListItemType.AlternatingItem)
        {

            ImageButton button2 = (ImageButton)e.Item.FindControl( " ImageButton2 ");
            button2.Attributes.Add( " onclick ",
                 " return confirm (\"确定要删除此项记录吗?\"); ");
        }

    }
     protected  void Repeater1_ItemCommand( object source, RepeaterCommandEventArgs e)
    {

         int nid =  int.Parse(e.CommandArgument.ToString());
         string sql_Del =  " delete PublicProject  where ID = ' " + nid +  " ' ";
        newdb.CommonExecuteNonQuery(sql_Del);
        BindInfo();
       
    }
     protected  void AspNetPager1_PageChanged( object sender, EventArgs e)
    {
        BindInfo();
    }

     #region 删除多选项事件  public void DelCheckBoxItem()

     public  void DelCheckBoxItem()
    {
        CheckBox cbox;
        HtmlInputHidden HtmlInputID;


         for ( int i =  0; i <= Repeater1.Items.Count -  1; i++)
        {
             if (Repeater1.Items[i].ItemType == ListItemType.Item)
            {
                cbox = (CheckBox)Repeater1.Items[i].FindControl( " Ckb_Sel ");
                HtmlInputID = (HtmlInputHidden)Repeater1.Items[i].FindControl( " txtID ");

                 if (cbox.Checked ==  true)
                {
                     string strID = HtmlInputID.Value.ToString();
                     int nid =  int.Parse(strID);

                     string sql_Del =  " delete PublicProject  where ID = ' " + nid +  " ' ";
                    newdb.CommonExecuteNonQuery(sql_Del);

                }
            }
             else  if (Repeater1.Items[i].ItemType == ListItemType.AlternatingItem)
            {
                cbox = (CheckBox)Repeater1.Items[i].FindControl( " Ckb_Sel1 ");
                HtmlInputID = (HtmlInputHidden)Repeater1.Items[i].FindControl( " txtID1 ");


                 if (cbox.Checked ==  true)
                {
                     string strID = HtmlInputID.Value.ToString();
                     int nid =  int.Parse(strID);

                     string sql_Del =  " delete PublicProject  where ID = ' " + nid +  " ' ";
                    newdb.CommonExecuteNonQuery(sql_Del);
                   
                }

            }
        }

        BindInfo();

    }

     #endregion
   

     protected  void Button1_Click( object sender, EventArgs e)
    {
        DelCheckBoxItem();
    }
}