首页  :: 新随笔  :: 联系 :: 管理

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

Posted on 2009-06-25 15:56  ^_^克方  阅读(1732)  评论(1编辑  收藏  举报
  1<%@ Page Language="C#" %>
  2<%@ Import Namespace="System.Data.SqlClient" %>
  3<%@ Import Namespace="System.Data" %>
  4
  5<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6
  7<script runat="server">
  8    public int pageSize = 12;//每一页显示多少页数据
  9    public int rowSize = 10;//每一页显示多少数据
 10    protected void Page_Load(object sender, EventArgs e)
 11    {
 12        int index = 1;
 13        if (!string.IsNullOrEmpty(Request.QueryString["Index"]))
 14        {
 15            index = Convert.ToInt32(Request.QueryString["Index"]);
 16        }

 17        
 18        this.Repeater1.DataSource = this.DataSourceForRepeater1(index);
 19        this.Repeater1.DataBind();
 20    }

 21    //返回当前页码对应的数据
 22    public DataTable DataSourceForRepeater1(int index)
 23    {
 24        using (SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString))
 25        {
 26            scon.Open();
 27            SqlCommand scom = scon.CreateCommand();
 28            scom.CommandText = "SELECT top(" + rowSize.ToString() +
 29                ") * FROM Images where Id not in  (select top " + ((index - 1* rowSize).ToString() + " Id from Images order by Id desc) order by Id Desc";
 30            
 31            DataTable dt = new DataTable();
 32            SqlDataAdapter sda = new SqlDataAdapter(scom);
 33            sda.Fill(dt);
 34            scon.Close();
 35            return dt;
 36        }

 37    }

 38     
 39    //返回总共多少页   
 40    public int Total
 41    {
 42        get
 43        {
 44            if (ViewState["Total"== null)
 45            {
 46                
 47
 48                using (SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString))
 49                {
 50                    scon.Open();
 51                    SqlCommand scom = scon.CreateCommand();
 52                    scom.CommandText = "SELECT COUNT(*) AS Total FROM Images";
 53                    DataTable dt = new DataTable();
 54                    SqlDataAdapter sda = new SqlDataAdapter(scom);
 55                    sda.Fill(dt);
 56                    scon.Close();
 57                    if (dt.Rows.Count != 0)
 58                    {
 59                        int quotient = Convert.ToInt32(dt.Rows[0][0])/rowSize;
 60                        int remainder = Convert.ToInt32(dt.Rows[0][0]) % rowSize;
 61                        quotient=remainder!=0?quotient+1:quotient;
 62                        ViewState["Total"= quotient;
 63                        return quotient;
 64
 65                    }

 66                    else return 0;
 67                }

 68            }

 69            else
 70            return Convert.ToInt32(ViewState["Total"]); }
 71         }

 72    }

 73</script>
 74
 75<html xmlns="http://www.w3.org/1999/xhtml">
 76<head runat="server">
 77    <title></title>
 78</head>
 79<body>
 80    <form id="form1" runat="server">    
 81   
 82   
 83   <div>
 84         
 85       <asp:Repeater ID="Repeater1" runat="server"  >
 86       <HeaderTemplate>
 87        <table>
 88            <thead>
 89                <tr>
 90                    <td>Id</td>
 91                    <td>ImageName</td>
 92                    <td>Des</td>
 93                    <td>Path</td>
 94                    <td>CategoryId</td>
 95                </tr>
 96            </thead>
 97            <tbody>
 98        
 99       </HeaderTemplate>
100       <ItemTemplate>
101        <tr>
102        <td><%# Eval("Id"%></td>
103        <td><%# Eval("ImageName"%></td>
104        
105        <td><%# Eval("Des")%></td>
106        <td><%# Eval("Path")%></td>
107        <td><%# Eval("CategoryId")%></td>        
108        </tr>       
109       </ItemTemplate>
110       <FooterTemplate>
111       </tbody>
112       <tfoot>
113        <tr>
114            <td colspan="5"> 
115            <%--初始页面为1--%>
116         <% int index=1;
117           if(!string.IsNullOrEmpty(Request.Form["Index"]))
118           {
119               index=Convert.ToInt32(Request.Form["Index"]);
120           }

121            if(index>pageSize)
122            {
123                Response.Write("<a href=\"GridviewPaging.aspx?Index=" + (index-1).ToString() + "')\">" + "上一页" + "</a>&nbsp");
124            }

125           
126     %>
127    <%-- 显示页码--%>
128    <% for (int i = pageSize * (index - 1+ 1; (i <= this.Total) && (i <= index * pageSize); i++%>
129    <%{%>
130        
131        
132        <% Response.Write("<a href=\"GridviewPaging.aspx?Index="+i.ToString()+"\">" +i.ToString() + "</a>&nbsp");%>     
133           
134        
135    <%}
 %>
136    <% 
137        if (index*pageSize<this.Total)
138        {
139             Response.Write("<a href=\"GridviewPaging.aspx?Index=" + (index+1).ToString() + "')\">" + "下一页" + "</a>&nbsp");
140        }

141    %>
142          </td>
143        </tr>
144       </tfoot>
145       </table>
146       </FooterTemplate>
147       </asp:Repeater>
148   
149    
150         
151   </div>
152   
153    </form>
154</body>
155</html>
156