这几天看petshop,也做了一个repeator,可分页的,还没完善就急于放上来了,希望增加点人气
2006-05-03 06:31
411 查看
这几天看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 ;
}
}
}
<%@ 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
4000
" 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>
/*********************** 数据库文件 ***********************/
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
4000
" 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>
相关文章推荐
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
- 自定义分页控件(四)完善设计时支持和增加分页模式
- 发布一个未完善的分页DataList控件 -- PageDataList
- 【开源】QuickPager ASP.NET2.0分页控件V2.0.0.7 增加了一个js函数的分页方式。
- 发布一个分页算法,希望大家一起讨论,彻底解决分页之苦
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
- 这几天新写了一个记事本的源码。很多地方没有注释,先放上来吧
- 运维角度浅谈MySQL数据库优化一个成熟的数据库架构并不是一开始设计就具备高可用、高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善。这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方
- 【开源】QuickPager ASP.NET2.0分页控件V2.0.0.7 增加了一个js函数的分页方式。
- 发一个python写的多线程 代理服务器 抓取,保存,验证程序,希望喜欢python的朋友和我一起完善它
- 今天带来的是一个对图书编号和价格设定程序 不是很完善希望大家给与建议进行修改
- 完善分页插件,增加内部成员,确定控件是否已经初始化过一次。
- 这是我在公司自己写的一个小项目模仿的一个互帮派APP后面那些我也没有时间完善希望希望的朋友帮我完善后面那些
- [Transfer][VB]DataGrid分页机制,通过编程做一个十分有个性、功能十分完善的分页效果
- 奋斗中。。。自己写的一个时钟程序,希望大家继续完善!
- 一个DataGrid分页控件,c#写的,再vb.net中调用,将会不断完善的
- 个人论坛,希望大家注册一下,增加人气
- PHP写的一个比较完善的分页.
- 4,在本章第2.6.2(题目错误)节中给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数
- 一个分页存储过程的使用