UserDataPager.ascx用户控件代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserDataPager.ascx.cs" Inherits="UserDataPager" %>
<style type="text/css">
.style8
{
}
.style11
{
width: 249px;
font-size: small;
}
</style>
<table cellpadding="0" cellspacing="0" style="border: 1px solid #999999; font-size: small; height: 25px; width: 403px;"
bgcolor="#F2F2F2">
<tr>
<td style="text-align: center" class="style11">
共有数据<asp:Label ID="lbldatanum" runat="server" ForeColor="Red"></asp:Label>
条&nbsp; 每页<asp:Label ID="lblnum" runat="server" ForeColor="Red"></asp:Label>
条&nbsp; 第<asp:Label ID="lblCurrentPage" runat="server" ForeColor="Red"></asp:Label>
页/共<asp:Label ID="lblcount" runat="server" Font-Size="9pt" ForeColor="Red"></asp:Label>
页</td>
<td style="text-align: center" class="style8">
<asp:LinkButton ID="lbtnFirst" runat="server" Font-Size="9pt"
onclick="lbtnFirst_Click" Enabled="False">|&lt;</asp:LinkButton>
&nbsp;<asp:LinkButton ID="lbtnForward" runat="server" Font-Size="9pt"
onclick="lbtnForward_Click" Enabled="False">&lt;</asp:LinkButton>
&nbsp;<asp:LinkButton ID="lbtnBackwards" runat="server" Font-Size="9pt"
onclick="lbtnBackwards_Click">&gt;</asp:LinkButton>
&nbsp;<asp:LinkButton ID="lbtnLast" runat="server" Font-Size="9pt"
onclick="lbtnLast_Click">&gt;|</asp:LinkButton>
</td>
</tr>
</table>

UserDataPager.ascx.cs代码:

public partial class UserDataPager : System.Web.UI.UserControl
{
protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
private SqlConnection conn; private object operateID;
public object OperateID
{
get { return operateID; }
set { operateID = value; }
}
private int pageNum=;
public int PageNum
{
get { return pageNum; }
set { pageNum = value; }
}
private string strSQL = "";
public string StrSQL
{
get { return strSQL; }
set { strSQL = value; }
}
private string sql = "";
public string DataSQL
{
get { return sql; }
set { sql = value; }
} private void BindDataList(int currentpage)
{
string mytype = operateID.GetType().ToString();
mytype = mytype.Substring(mytype.LastIndexOf(".") + , mytype.Length - - mytype.LastIndexOf("."));
conn = new SqlConnection(strSQL);
pds.AllowPaging = true;//允许分页
pds.PageSize = pageNum;//每页显示3条数据
pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值
conn.Open();//打开数据库连接
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);//把执行得到的数据放在数据集中
pds.DataSource = ds.Tables[].DefaultView;//把数据集中的数据放入分页数据源中
lblcount.Text = pds.PageCount.ToString();
lblCurrentPage.Text =(pds.CurrentPageIndex + ).ToString();
lblnum.Text = pageNum.ToString();
lbldatanum.Text = ds.Tables[].Rows.Count.ToString();
if (pds.PageCount > )
{
lbtnBackwards.Enabled = true;
lbtnLast.Enabled = true;
}
else
{
lbtnBackwards.Enabled = false;
lbtnLast.Enabled = false;
}
if (mytype == "GridView")
{
((GridView)(operateID)).DataSource = pds;
((GridView)(operateID)).DataBind();
}
else if (mytype == "DataList")
{
((DataList)(operateID)).DataSource = pds;
((DataList)(operateID)).DataBind();
}
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void lbtnFirst_Click(object sender, EventArgs e)//首页
{
pds.CurrentPageIndex = ;
BindDataList(pds.CurrentPageIndex);
lbtnFirst.Enabled = false;
lbtnForward.Enabled = false;
lbtnBackwards.Enabled = true;
lbtnLast.Enabled = true; }
protected void lbtnForward_Click(object sender, EventArgs e)//上一页
{
if (pds.CurrentPageIndex >= )
{
pds.CurrentPageIndex = pds.CurrentPageIndex - ;
BindDataList(pds.CurrentPageIndex);
lbtnBackwards.Enabled = true;
lbtnLast.Enabled = true;
if (pds.CurrentPageIndex == )
{
lbtnForward.Enabled = false;
lbtnFirst.Enabled = false;
}
}
}
protected void lbtnBackwards_Click(object sender, EventArgs e)//下一页
{
if (pds.CurrentPageIndex <= pds.PageCount - )
{
lbtnFirst.Enabled = true;
lbtnForward.Enabled = true;
pds.CurrentPageIndex = pds.CurrentPageIndex + ;
BindDataList(pds.CurrentPageIndex);
if (pds.CurrentPageIndex == pds.PageCount - )
{
lbtnBackwards.Enabled = false;
lbtnLast.Enabled = false;
}
}
}
protected void lbtnLast_Click(object sender, EventArgs e)//尾页
{
pds.CurrentPageIndex = pds.PageCount - ;
BindDataList(pds.CurrentPageIndex);
lbtnLast.Enabled = false;
lbtnBackwards.Enabled = false; lbtnFirst.Enabled = true;
lbtnForward.Enabled = true;
}
}

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="UserDataPager.ascx" tagname="UserDataPager" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style7
{
width: 100%;
height: 42px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 1019px; text-align: center; height: 54px;"> <table align="center" cellpadding="0" cellspacing="0" class="style7">
<tr>
<td style="text-align: left">
<uc1:UserDataPager ID="UserDataPager1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table> </div>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="Ridge" BorderWidth="1px"
CellPadding="3" Width="403px">
<RowStyle ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>

Default.aspx.cs代码:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserDataPager1.OperateID = GridView1; //设置针对哪个控件进行分页描述
UserDataPager1.StrSQL = "server=.;uid=sa;pwd=123.456;database=TYW;";//设置链接字符串
UserDataPager1.DataSQL = "select * from card";
UserDataPager1.PageNum = 6;
}
}

最终效果:

036. asp.netWeb用户控件之五使用用户控件实现分页数据导航的更多相关文章

  1. 037. asp.netWeb用户控件之五使用用户控件实现文件上传功能

    fileUpload.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile= ...

  2. 033. asp.netWeb用户控件之二将页面转换成web控件和使用Web控件显示热点新闻

    访问Web用户控件的属性 ASP.NET提供的各种服务器控件都有其自身的属性和方法,程序开发人员可以灵活地使用服务器控件中的属性和方法开发程序.在用户控件中,程序开发人员也可以自行定义各种属性和方法, ...

  3. ASP.NET MVC中加载WebForms用户控件(.ascx)

    原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...

  4. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  5. JS获取用户控件中的子控件Id

    用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...

  6. asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解

    http://book.51cto.com/art/200909/154039.htm http://book.51cto.com/art/200909/154041.htm ASP.NET提供了一套 ...

  7. ASP.NET MVC显示WebForm网页或UserControl控件

    ASP.NET MVC显示WebForm网页或UserControl控件 学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user ...

  8. 念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件

    学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user control呢?这个灵感(算不上灵感,只能算是想法)是来自前些天有写过一 ...

  9. 【Asp.net之旅】--因自己定义控件注冊而引发的思考

    前言 近期在开发远洋的SOA系统平台,开发使用的是.NET平台.对于Asp.net并不困难,但该系统的开发并非全然依靠Asp.net.而是自身封装好的框架.这套框架是远洋地产购买的微软的开发平台,项目 ...

随机推荐

  1. 火狐浏览器Firefox上DownThemAll插件

    DownThemAll插件支持断点续传.多线程下载,可以大幅度提高下载速度. 在Windows平台上,要下载大量的文件,迅雷自然是首选:但在非Windows平台上,只要安装一个火狐浏览器,再安装Dow ...

  2. ORACLE分页查询SQL语法——最高效的分页

    --1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT  ...

  3. C/C++ 如何来自动优雅的涮别银家的贴子

    被涮屏涮烦了,就分享一下如何用低调的c/c++来涮别人家的屏吧! 此处埋下三颗雷! 这不是啥新知识,也不是什么浅显的代码.下面,来淘淘这份经验,呼呼 我们要了解Web browser 这个控件,因为到 ...

  4. css实现一段不够一行时居中显示,多于一行时两端对齐

    今天有遇到这个问题,不够一行要居中才好看,多于一行居中又很难看: 居中 两端对齐 一开始想用text-align-last:center; 可是这样结果是这样的: 单行的居中了 可是多行的最后一行也居 ...

  5. H5页面左图右边文字如何布局

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. Linux gcc 编译日记

    gcc 编译器是众多编译器组合入口,例如在编译 .cpp 文件时,使用c++ 编译器,编译.c 文件时,使用c编译器. 在编译c++程序时, 库文件与头文件可通过 -L[dir] 指定库目录 , -l ...

  7. JDK1.5新特性

    静态导入 import static java.util.Collections.*; import static java.lang.System.out; 1.如果静态导入的成员与本类的成员存在同 ...

  8. QuartZ.net 常用配置说明

    配置文件说明 app.config中的quartz部分 <quartz> <!-- configure Thread Pool--> <addkey="quar ...

  9. powershell脚本闪电输入神器

    如图所示: 用autohotkey,把下列常用按键互唤.来达到快速输入目的.并且只对powershell ise,power gui,visual studio code,powershell stu ...

  10. PHP的GD 支持和加载MySQL功能

    本机安装dedecms时发现, GD 支持 On [×]Off (不支持将导致与图片相关的大多数功能无法使用或引发警告) MySQL 支持 On [×]Off (不支持无法使用本系统) 错误,原来是环 ...