这几天看petshop,也做了一个repeator,可分页的,还没完善就急于放上来了,希望增加点人气

来源:互联网 发布:wap转盘源码 编辑:程序博客网 时间:2024/05/01 05:16

这几天看petshop,也做了一个repeator,可分页的,还没完善就急于放上来了,希望增加点人气

 /*********************** 数据库文件 ***********************/

create database dbpstest
go
use dbpstest
go

create table stu
(
 stucode varchar(20) primary key ,
 stuname varchar(20) ,
 stuage int
)
go

insert into stu values('stu001','zs',20)
insert into stu values('stu002','ls',21)
insert into stu values('stu003','ww',22)
insert into stu values('stu004','aa',23)
insert into stu values('stu005','bb',24)
insert into stu values('stu006','cc',25)
insert into stu values('stu007','dd',26)

/*********************** UserControlTest.Controls.MyRepeater3.cs文件 ***********************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace UserControlTest.Controls
{
 /// <summary>
 /// MyRepeater3 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:MyRepeater3 runat=server></{0}:MyRepeater3>")]
 public class MyRepeater3 : Repeater
 {
  private string text;
  private string html1 = "<table width=/"100%/" border=/"0/"><tr><td>" ;
  private string html2 = "</td></tr><tr><td align=/"left/" >" ;
  private string html3 = "<table ><tr>" ;
  private string html31 = "<td><a href=?page=1>首页</a></td>" ;
  private string html32 = "<td><a href=?page={0}>上页</a></td>" ;
  private string html33 = "<td><a href=?page={0}>{1}</a>   " ;
  private string html34 = "<td><a href=?page={0}>下页</a></td>" ;
  private string html35 = "<td><a href=?page={0}>末页</td>" ;
  private string html36 = "</tr></table>" ;
  private string html4 = "</td></tr></table>" ;
 

  private string sql ;
  private string orderFld ;
  private int recordCountPerPage ;
  private int currentPage ;
  private int lastPage ;
  private int barCount = 4 ;//显示页数的个数

  [Bindable(true),
  Category("Appearance"),
  DefaultValue("")]
  public string Text
  {
   get
   {
    return text;
   }

   set
   {
    text = value;
   }
  }

 


  public string SQL
  {
   get { return sql; }
   set { sql = value; }
  }

  public string OrderFld
  {
   get { return orderFld; }
   set { orderFld = value; }
  }

  public int RecordCountPerPage
  {
   get { return recordCountPerPage; }
   set { recordCountPerPage = value; }
  }

  public int CurrentPage
  {
   get { return currentPage; }
   set
   {
    int totalPages = 10 ;
    if(currentPage<1)
     currentPage = 1 ;
    if(currentPage>totalPages)
     currentPage = totalPages ;
    currentPage = value;
    this.OnDataBinding(null);
   }
  }

  public int LastPage
  {
   get { return lastPage; }
   set { lastPage = value; }
  }

  public int BarCount
  {
   get { return barCount; }
   set { barCount = value; }
  }


  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   output.Write(html1);
   base.Render(output);
   output.Write(html2);
   output.Write(html3);
   output.Write(html31);
   output.Write(string.Format(html32,this.CurrentPage-1));

   for( int i = 1 ;i<=this.BarCount;i++)
    output.Write(string.Format(html33 ,i , i));

   output.Write(string.Format(html34,this.CurrentPage+1));
   output.Write(string.Format(html35 , LastPage));
   output.Write(html36);
   output.Write(html4);

   //output.Write(this.SQL + this.RecordCountPerPage + this.OrderFld);
   output.Write(((DataView)this.DataSource).Count);
  }

  protected override void OnDataBinding(EventArgs e)
  {
   
   string strConn = "server=clx ;database=dbpstest;uid=sa;pwd='';" ;
   SqlConnection conn = new SqlConnection(strConn);
   conn.Open();

   SqlCommand cmd = new SqlCommand("select count(*) from (" + this.SQL + ") as x" ,conn);
   SqlDataReader sdr = cmd.ExecuteReader() ;
   sdr.Read() ;
   this.LastPage =( Int32.Parse(sdr[0].ToString())-1)/this.RecordCountPerPage + 1 ;
   sdr.Close();
   
   //SqlCommand cmd = new SqlCommand(this.SQL , conn);
   //string sqlExec = "select * from " + ;
   int begin = (this.currentPage-1) * this.RecordCountPerPage + 1 ;
   int end = this.currentPage * this.RecordCountPerPage  ;
   string sqlExec = @"select  *
        from
        (
         select top " + (end-begin+1) + @" * from
         (
          select top " + end + @" *
          from
          (
           select * from stu
          ) as alias1
          order by stucode asc
         ) as alias2
         order by stucode desc
        ) as stu
        order by stucode" ;
   SqlDataAdapter sda = new SqlDataAdapter(sqlExec , conn);
   DataSet ds = new DataSet();
   sda.Fill( ds ) ;
   this.DataSource = ds.Tables[0].DefaultView ;
   base.OnDataBinding (e);//这一句必须放在此方法的最后一句

 

  }

  protected override void OnLoad(EventArgs e)
  {
   base.OnLoad (e);
   string str = Context.Request["page"] ;
   int x = 1 ;
   if(str!=null)
    x = Int32.Parse(str) ;

   this.CurrentPage = x ;

   
  }
 }

}

/*********************** WebForm3.aspx文件 ***********************/

 

<%@ Register TagPrefix="mycontrol" Namespace="UserControlTest.Controls" Assembly="UserControlTest" %>
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="UserControlTest.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm3</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT>
   <br>
   <br>
   <br>
   <mycontrol:myrepeater3 runat="server" id="mr1" Text="myrepeat" SQL="select * from stu" OrderFld="stuage"
    RecordCountPerPage="3" CurrentPage="2">
    <ItemTemplate>
     <tr>
      <td><%# DataBinder.Eval(Container.DataItem ,"stucode") %></td>
      <td><%# DataBinder.Eval(Container.DataItem ,"stuname") %></td>
      <td><%# DataBinder.Eval(Container.DataItem ,"stuage") %></td>
     </tr>
    </ItemTemplate>
    <FooterTemplate>
     </table>
    
</FooterTemplate>
    <HeaderTemplate>
     <table border="1">
    </HeaderTemplate>
   </mycontrol:myrepeater3></form>
 </body>
</HTML>