利用Repeater控件实现数据分页显示

来源:互联网 发布:天界幻宠物进化数据 编辑:程序博客网 时间:2024/05/10 11:08

 

1<%@ Page Language="C#" %>
  2<%@ Import Namespace="System.Data.SqlClient" %>
  3<%@ Import Namespace="System.Data" %>
  5<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7<script runat="server">
   public int pageSize = 12;//每一页显示多少页数据
   public int rowSize = 10;//每一页显示多少数据
   protected void Page_Load(object sender, EventArgs e)
   {
       int index = 1;
       if (!string.IsNullOrEmpty(Request.QueryString["Index"]))
       {
           index = Convert.ToInt32(Request.QueryString["Index"]);
       }
      
       this.Repeater1.DataSource = this.DataSourceForRepeater1(index);
       this.Repeater1.DataBind();
   }
   //返回当前页码对应的数据
   public DataTable DataSourceForRepeater1(int index)
   {
       using (SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString))
       {
           scon.Open();
           SqlCommand scom = scon.CreateCommand();
           scom.CommandText = "SELECT top(" + rowSize.ToString() +
               ") * FROM Images where Id not in  (select top " + ((index - 1) * rowSize).ToString() + " Id from Images order by Id desc) order by Id Desc";
          
           DataTable dt = new DataTable();
           SqlDataAdapter sda = new SqlDataAdapter(scom);
           sda.Fill(dt);
           scon.Close();
           return dt;
       }
   }
   
   //返回总共多少页  
   public int Total
   {
       get
       {
           if (ViewState["Total"] == null)
           {
              
 48                using (SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString))
               {
                   scon.Open();
                   SqlCommand scom = scon.CreateCommand();
                   scom.CommandText = "SELECT COUNT(*) AS Total FROM Images";
                   DataTable dt = new DataTable();
                   SqlDataAdapter sda = new SqlDataAdapter(scom);
                   sda.Fill(dt);
                   scon.Close();
                   if (dt.Rows.Count != 0)
                   {
                       int quotient = Convert.ToInt32(dt.Rows[0][0])/rowSize;
                       int remainder = Convert.ToInt32(dt.Rows[0][0]) % rowSize;
                       quotient=remainder!=0?quotient+1:quotient;
                       ViewState["Total"] = quotient;
                       return quotient;
 65                    }
                   else return 0;
               }
           }
           else
           { return Convert.ToInt32(ViewState["Total"]); }
        }
   }
 73</script>
 75<html xmlns="http://www.w3.org/1999/xhtml">
 76<head runat="server">
   <title></title>
 78</head>
 79<body>
   <form id="form1" runat="server">   
 
 
  <div>
       
      <asp:Repeater ID="Repeater1" runat="server"  >
      <HeaderTemplate>
       <table>
           <thead>
               <tr>
                   <td>Id</td>
                   <td>ImageName</td>
                   <td>Des</td>
                   <td>Path</td>
                   <td>CategoryId</td>
               </tr>
           </thead>
           <tbody>
      
      </HeaderTemplate>
      <ItemTemplate>
       <tr>
       <td><%# Eval("Id") %></td>
       <td><%# Eval("ImageName") %></td>
      
       <td><%# Eval("Des")%></td>
       <td><%# Eval("Path")%></td>
       <td><%# Eval("CategoryId")%></td>       
       </tr>      
      </ItemTemplate>
      <FooterTemplate>
      </tbody>
      <tfoot>
       <tr>
           <td colspan="5">
           <%--初始页面为1--%>//5列数据,上一页,12345,下一页,
        <% int index=1;
          if(!string.IsNullOrEmpty(Request.Form["Index"]))
          {
              index=Convert.ToInt32(Request.Form["Index"]);
          }
           if(index>pageSize)  //小于页数都有上一页
           {
               Response.Write("<a href=\"GridviewPaging.aspx?Index=" + (index-1).ToString() + "')\">" + "上一页" + "</a>&nbsp");
           }
         
    %>
   <%-- 显示页码--%>
   <% for (int i = pageSize * (index - 1) + 1; (i <= this.Total) && (i <= index * pageSize); i++) %>//for循环实现页码显示,
   <%{%>
      
      
       <% Response.Write("<a href=\"GridviewPaging.aspx?Index="+i.ToString()+"\">" +i.ToString() + "</a>&nbsp");%>    
         
      
   <%} %>
   <%
       if (index*pageSize<this.Total)//条数少于查询的数据数都有下一页
       {
            Response.Write("<a href=\"GridviewPaging.aspx?Index=" + (index+1).ToString() + "')\">" + "下一页" + "</a>&nbsp");
       }
   %>
         </td>
       </tr>
      </tfoot>
      </table>
      </FooterTemplate>
      </asp:Repeater>
 
  
       
  </div>
 
   </form>
154</body>
155</html>
156

http://www.w3school.com.cn/aspnet/aspnet_repeater.asp  repeater控件

 

原创粉丝点击