Repeater 结合PagedDataSource 实现分页

 

  
  
  
  
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml"> 
  6. <head runat="server"> 
  7.     <title></title>   
  8.     <script language="javascript" type="text/ecmascript"> 
  9.         function Check(ParentChk, ChildId) {  
  10.           
  11.             var oElements = document.getElementsByTagName("INPUT");//获取全部 input元素  
  12.             var bIsChecked = ParentChk.checked;  
  13.               
  14.             for(var i=0;i<oElements.length;i++)  
  15.             {  
  16.                 if (IsCheckBox(oElements[i]) && IsMatch(oElements[i].id, ChildId))  //是checkbox 而且是 repeater 内的符合条件的input 元素  
  17.                 {  
  18.                     oElements[i].checked=bIsChecked;  //将全部的 checkbox 状态置为选中  
  19.                 }  
  20.             }  
  21.         }  
  22.                   
  23.         function IsMatch(id,ChidId)  //是不是repeater 下的某个子元素  
  24.         {  
  25.             var sPattern = '^Rept1.*' + ChidId + '$';  
  26.             var oRegExp=new RegExp(sPattern);  
  27.             if(oRegExp.exec(id))  
  28.             return true;  
  29.             else   
  30.             return false;  
  31.         }  
  32.            
  33.         function IsCheckBox(chk)   // 判断是不是 checkbox  
  34.         {  
  35.             if(chk.type=='checkbox')return true;  
  36.             else return false;  
  37.         }  
  38.     </script> 
  39. </head> 
  40. <body> 
  41. <form id="form1" runat="server" title="自定义表结构"> 
  42.     <div> 
  43.     <asp:Repeater ID="Rept1" runat="server"> 
  44.           
  45.     <HeaderTemplate> <%-- 定义标题--%> 
  46.        <table border="0" cellpadding="0" cellspacing="0" style="width:1006px;border-collapse:collapse; text-align:center;"> 
  47.        <tr> 
  48.          <td style="background-color:#cccccc; font-weight:bold; height:25px;"> 
  49.          <input id="chkAll" name="chkAll" runat="server" type="checkbox" onclick="Check(this,'chkItem')" title="全选" /></td> 
  50.          <td style="background-color:#cccccc; font-weight:bold; height:25px;">View</td> 
  51.          <td style="background-color:#cccccc; font-weight:bold; height:25px;"  visible="false"></td>      
  52.          <td style="background-color:#cccccc; font-weight:bold;">名字</td> 
  53.          <td style="background-color:#cccccc; font-weight:bold;">班级</td>       
  54.        </tr> 
  55.      </HeaderTemplate> 
  56.       
  57.       
  58.     <ItemTemplate>   <%-- <ItemTemplate> 元素的内容会对应 DataSet 中的 "record" 重复出现--%> 
  59.     <tr> 
  60.  <%--  <td >张三</td> 能够直接定义表数据  
  61.     <td>07一班</td>--%> 
  62.     <td><asp:CheckBox  ID="chkItem" runat="server"/></td> 
  63.     <td><a href='<%#"ViewDetail.aspx?id="+DataBinder.Eval(Container.DataItem,"id") %>' target="_blank">View</a></td>  <%--链接 :将本记录的行 Id 传递到目标页面--%> 
  64.     <td><asp:Label ID ="lblID" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' runat="server" Visible="false"></asp:Label></td>  <%-- 存储本记录的 id ,做为删除等操做的依据 --%> 
  65.     <td><%#DataBinder.Eval(Container.DataItem,"name") %></td>  <%--从数据库中取出表数据--%> 
  66.     <td><%#DataBinder.Eval(Container.DataItem,"class") %></td> 
  67.     </tr>    
  68.     </ItemTemplate>      
  69.       
  70.     <AlternatingItemTemplate>  <%--记录显示方式的交替样式  , 基本与 ItemTemplate 样式一致--%> 
  71.     <tr bgcolor="#e8e8e8"> 
  72.      <td><asp:CheckBox  ID="chkItem" runat="server"/></td> 
  73.      <td><a href='<%#"Default.aspx?id="+DataBinder.Eval(Container.DataItem,"id") %>' target="_blank">View</a></td> 
  74.      <td><asp:Label ID ="lblID" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' runat="server" Visible="false"></asp:Label></td>    
  75.     <td><%#DataBinder.Eval(Container.DataItem,"name") %></td> 
  76.     <td><%#DataBinder.Eval(Container.DataItem,"class")%></td>    
  77.     </tr>      
  78.     </AlternatingItemTemplate> 
  79.       
  80.   <%-- <SeparatorTemplate>--%> <%--可以用于描述每一个记录之间的分隔符 --%> 
  81.  <%--   <tr> 
  82.     <td colspan="0"><hr /></td>--%> <%-- 在每一个表格行之间插入了一条水平线--%> 
  83. <%--   </tr> 
  84.     </SeparatorTemplate>--%> 
  85.       
  86.     <FooterTemplate> 
  87.     </table> 
  88.     </FooterTemplate> 
  89.       
  90.       
  91.     <FooterTemplate>  <%--<FooterTemplate> 的内容在输出中仅出现一次--%> 
  92.     </table> 
  93.     </FooterTemplate> 
  94.       
  95.     </asp:Repeater> 
  96.     <div style="background-color:#dedede; width:1006px;"> 
  97.         <asp:Button ID="btnDel" runat="server" Text="删除" onclick="btnDel_Click" /> 
  98.         <asp:label ID="lblCurrentPage" runat="server"></asp:label> 
  99.         <asp:HyperLink id="lnkFrist" runat="server">首页</asp:HyperLink> 
  100.         <asp:HyperLink id="lnkPrev" runat="server">上一页</asp:HyperLink> 
  101.         <asp:HyperLink id="lnkNext" runat="server">下一页</asp:HyperLink>   
  102.         <asp:HyperLink id="lnkEnd" runat="server">尾页</asp:HyperLink>        
  103.     </div> 
  104.     </div> 
  105.     </form> 
  106. </body> 
  107. </html> 

C#代码javascript

 

  
  
  
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10.  
  11. public partial class Default4 : System.Web.UI.Page  
  12. {  
  13.     PagedDataSource PDS = new PagedDataSource(); //PagedDataSource 实现分页  
  14.  
  15.  
  16.     private  string StrConn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString();  
  17.     private string strSql = "";  
  18.  
  19.     protected void Page_Load(object sender, EventArgs e)  
  20.     {  
  21.         if (!IsPostBack)  
  22.             BindRepeater();  
  23.     }  
  24.     private void BindRepeater()  
  25.     {  
  26.         int totalCount = 0;//总记录条数  
  27.         int totalPage = 1;//总页数  
  28.  
  29.         string sql = "select id ,name,class from Category";  
  30.         DataSet ds=new DataSet ();  
  31.         SqlConnection conn = new SqlConnection(StrConn);  
  32.  
  33.         conn.Open();  
  34.         SqlDataAdapter da = new SqlDataAdapter(sql, conn);  
  35.         da.Fill(ds);  
  36.         conn.Close();  
  37.  
  38.         totalCount = ds.Tables[0].DefaultView.Count;  
  39.  
  40.         PDS.DataSource = ds.Tables[0].DefaultView; //设置pagedatasource 的属性        
  41.  
  42.         PDS.AllowPaging = true; //是否容许分页  
  43.         PDS.PageSize = 5;//每页显示记录数量  
  44.  
  45.         int curPage;  
  46.  
  47.         if(Request.QueryString["page"]!=null)  
  48.              curPage=Convert.ToInt32(Request.QueryString["page"]);// 为四个连接准备  
  49.         else  
  50.              curPage=1;  
  51.  
  52.         if (totalCount ==0)//若是没有记录,就一页  
  53.             totalPage = 1;  
  54.         else  
  55.         {  
  56.             if (totalCount % PDS.PageSize == 0) //若是是整数页  
  57.                 totalPage = totalCount / PDS.PageSize;  
  58.             else  
  59.                 totalPage = totalCount / PDS.PageSize + 1; //若是非整数页  
  60.         }  
  61.         PDS.CurrentPageIndex = curPage - 1;  
  62.         lblCurrentPage.Text = "共" + totalCount.ToString() + "条记录,当前页" + curPage.ToString() + "/" + totalPage;//共30条记录,当前页1/2  
  63.         lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath+"?page=1";//获取当前请求的虚拟路径  
  64.         if (!PDS.IsFirstPage) //若是不是第一页  
  65.             lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage - 1);  
  66.         if (!PDS.IsLastPage) //若是不是最后一页  
  67.             lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage + 1);  
  68.         lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + totalPage;  
  69.  
  70.         Rept1.DataSource = PDS;  
  71.         Rept1.DataBind();  
  72.     }  
  73.  
  74.     protected void btnDel_Click(object sender, EventArgs e)  
  75.     {  
  76.         string ID = "";  
  77.         for (int i = 0; i < this.Rept1.Items.Count; i++)  
  78.         {  
  79.             CheckBox cbox = (CheckBox)this.Rept1.Items[i].FindControl("chkItem");  
  80.             if (cbox.Checked == true) //选中复选框  
  81.             {  
  82.                 if (ID == "")  
  83.                 {  
  84.                     ID = "'" + ((Label)this.Rept1.Items[i].FindControl("lblID")).Text + "'";//label中存储字段 id   
  85.                 }  
  86.                 else  
  87.                 {  
  88.                     ID += ","+"'" + ((Label)this.Rept1.Items[i].FindControl("lblID")).Text + "'";  
  89.                 }              
  90.             }  
  91.         }  
  92.         strSql = "delete from Category where id in(" + ID + ")"; //删除选中的记录行  
  93.         SqlConnection conn = new SqlConnection(StrConn);  
  94.         try  
  95.         {              
  96.             SqlCommand cmd = new SqlCommand(strSql, conn);  
  97.             cmd.Connection.Open();  
  98.             cmd.ExecuteNonQuery();  
  99.             cmd.Connection.Close();  
  100.             System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('删除成功!');</script>"); //删除成功后,提示对话框  
  101.  
  102.         }  
  103.         catch (System.Data.SqlClient.SqlException E)  
  104.         {  
  105.             throw new Exception(E.Message);  
  106.         }  
  107.         finally  
  108.         {  
  109.             if (conn.State == ConnectionState.Open)  
  110.                 conn.Close();  
  111.         }  
  112.         this.BindRepeater();  
  113.     }  
  114. }  
  115.    

运行结果html

 

当点击view 链接时,可在目标页面获取 id 值java

 

  
  
  
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8. using System.Data;  
  9. using System.Data.SqlClient;  
  10.  
  11. public partial class ViewDetail : System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         Response.Write("由页面Default传过来的记录 ID 是 "+Request.QueryString["id"]) ;  
  16.     }  
  17. }