使用Repeater模板 二

ASP.NET提供的Repeater模板并不带有分页功能,如果是少量数据的话利用Repeater模板来实现分页还是不错的,毕竟Repeater模板较为灵活。

<% @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > WebForm1 </ title >
        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" >
                
< asp:Repeater  id ="Repeater1"  runat ="server" >
                    
< HeaderTemplate >
                        
< table  border ="0" >
                            
< tr >
                                
< td >< font  color ="Highlight" > 编号 </ font ></ td >
                                
< td >< font  color ="Highlight" > 名称 </ font ></ td >
                                
< td >< font  color ="Highlight" > 描述 </ font ></ td >
                            
</ tr >
                    
</ HeaderTemplate >
                    
< ItemTemplate >
                        
< tr  bgcolor ="LightYellow" >
                            
< td >
                                
<% # DataBinder.Eval(Container.DataItem,"CategoryID" %>
                            
</ td >
                            
< td >
                                
<% # DataBinder.Eval(Container.DataItem,"CategoryName" %>
                            
</ td >
                            
< td >
                                
<% # DataBinder.Eval(Container.DataItem,"Description" %>
                            
</ td >
                        
</ tr >
                    
</ ItemTemplate >
                    
< AlternatingItemTemplate >
                        
< tr >
                            
< td > <% # DataBinder.Eval(Container.DataItem,"CategoryID" %> </ td >
                            
< td > <% # DataBinder.Eval(Container.DataItem,"CategoryName" %> </ td >
                            
< td > <% # DataBinder.Eval(Container.DataItem,"Description" %> </ td >
                        
</ tr >
                    
</ AlternatingItemTemplate >
                    
< FooterTemplate >
                        
</ table >
                        
< asp:ImageButton  CommandName ="previous"  ID ="previous"  ImageUrl ="./images/previous.gif"  Runat ="server"   />
                        
< asp:ImageButton  CommandName ="next"  ID ="next"  ImageUrl ="./images/next.gif"  Runat ="server"   />
                    
</ FooterTemplate >
                
</ asp:Repeater ></ FONT >
        
</ form >
    
</ body >
</ HTML >

 

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Data.SqlClient;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  WebApplication1
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Repeater Repeater1;

        
protected SqlConnection con = null;
        
protected string CON_STR="server=192.168.0.XX;database=Northwind;user id=sa;password=XXX";
        
protected string SQL_QUERY="select * from Categories";
        
protected SqlDataAdapter da = null;
        
protected DataSet ds = null;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                BindData(
0,4);
            }

        }


        
Web 窗体设计器生成的代码
        
private void BindData(int start,int end)
        
{
            
try
            
{
                con 
= new SqlConnection(CON_STR);

            }

            
catch(SqlException sqlexp)
            
{
                Response.Write(
"Connect to database Error! "+sqlexp.Message);
            }


            da 
= new SqlDataAdapter(SQL_QUERY,con);
            ds 
= new DataSet();
            
try
            
{
                da.Fill(ds,start,end,
"Categories");
                
this.Repeater1.DataSource = ds;
                
this.Repeater1.DataBind();
            }

            
catch(Exception exp)
            
{
                Response.Write(exp.Message);
            }

            
finally
            
{
                con.Close();
            }

        }

        
private void Repeater1_ItemCommand(object sender,System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        
{
            
if(e.CommandName.Equals("next"))
            
{
                BindData(
4,7);
            }

            
else if(e.CommandName.Equals("previous"))
            
{
                BindData(
0,4);
            }

        }

    }

}

    此分页利用的是DataAdapter的Fill功能,但它提供的这种方式效率较低,因此只适用于少量数据,在大量数据情况下效率明显降低。
 截图:
编号名称描述
1 Beverages Soft drinks, coffees, teas, beers, and ales
2CondimentsSweet and savory sauces, relishes, spreads, and seasonings
3 Confections Desserts, candies, and sweet breads
4Dairy ProductsCheeses
 
编号名称描述
5 Grains/Cereals Breads, crackers, pasta, and cereal
6Meat/PoultryPrepared meats
7 Produce Dried fruit and bean curd
8SeafoodSeaweed and fish
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值