• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net自定义分页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

       GridView也自带分页技术,但是这种方式值只用于小数量的数据,因为它是将整个查询内容一次性查出来加载在内存中,这样数据量大的话就会造成系统反应迟缓。但是对于Row_Nomber()来说数据量大的话确实是明智之举,但是小数量的话和一次性全部加载的效果差距不明显,所以在实际应用中还是根据实际情况来选择吧!

  下面来介绍用Row_Nomber()分页:

   代码很简单, 因为代码上已加了相应的注释,所以就再不多做代码解释,

  先编写分页用户控件:

  前台效果

  代码:

View Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="control_WebUserControl"%>
<asp:Table ID="Table1" runat="server" Width="100%">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell0" runat="server"></asp:TableCell>
<asp:TableCell ID="TableCell1" runat="server" Width="140px">
<span>页次:</span><asp:Label ID="LabelMessage" runat="server" Text="1/1"></asp:Label></asp:TableCell>
<asp:TableCell ID="TableCell2" runat="server" Width="30px">
<asp:LinkButton ID="LinkButtonFirst" runat="server" OnClick="LinkButton_Click" CommandArgument="1"
CommandName
="First" Enabled="False">首页</asp:LinkButton></asp:TableCell>
<asp:TableCell ID="TableCell3" runat="server" Width="30px">
<asp:LinkButton ID="LinkButtonPrevious" runat="server" OnClick="LinkButton_Click"
CommandArgument
="1" CommandName="Previous" Enabled="False">上页</asp:LinkButton></asp:TableCell>
<asp:TableCell ID="TableCell4" runat="server" Width="30px">
<asp:LinkButton ID="LinkButtonNext" runat="server" OnClick="LinkButton_Click" CommandArgument="1"
CommandName
="Next" Enabled="False">下页</asp:LinkButton></asp:TableCell>
<asp:TableCell ID="TableCell5" runat="server" Width="30px">
<asp:LinkButton ID="LinkButtonLast" runat="server" OnClick="LinkButton_Click" CommandArgument="1"
CommandName
="Last" Enabled="False">末页</asp:LinkButton></asp:TableCell>
<asp:TableCell ID="TableCell6" runat="server" Width="30px">
<asp:LinkButton ID="LinkButtonGoto" runat="server" OnClick="LinkButton_Click" CommandArgument="1"
CommandName
="Goto">转到</asp:LinkButton></asp:TableCell>
<asp:TableCell ID="TableCell7" runat="server" Width="40px">
<asp:TextBox ID="TextBoxPage" runat="server" Width="35px"></asp:TextBox></asp:TableCell>
<asp:TableCell ID="TableCell8" runat="server" Width="20px">&nbsp;页</asp:TableCell>
</asp:TableRow>
</asp:Table>

分页控件后台代码:

View Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

publicpartialclass control_WebUserControl : System.Web.UI.UserControl
{
//设置条数数
privateint pageCount =1;
publicint PageCount
{
get
{
//先从ViewState中读取总页数,如果有总会页数的值,则将值返回,否则认为是一页
if (ViewState["PageCount"] !=null)
{
return Convert.ToInt32(ViewState["PageCount"]);
}
else
{
return1;
}
}
set
{
if (pageCount != value)
{
pageCount
= value;
ViewState[
"PageCount"] = pageCount;
ChangePage(currentPage);
}
}
}
//当前页数
privateint currentPage =1;
publicint CurrentPage
{
get
{
if (ViewState["CurrentPage"] !=null)
{
return Convert.ToInt32(ViewState["CurrentPage"]);
}
else
{
return1;
}
}
set
{
if (currentPage != value)
{
currentPage
= value;
//将当前页数保存在 ViewState["CurrentPage"]
ViewState["CurrentPage"] = currentPage;
//设置按钮启用
ChangePage(currentPage);
}
}
}
//总页数
privateint recorderCount =1;
publicint RecorderCount
{
get { return recorderCount; }
set
{
recorderCount
= value;
PageCount
= (recorderCount + PageSize -1) / PageSize;
}
}
//每页数量
privateint pageSize =1;
publicint PageSize
{
get
{
if (ViewState["PageSize"] !=null)
{
return Convert.ToInt32(ViewState["PageSize"]);
}
else
{
return1;
}
}
set
{
if (pageSize != value)
{
pageSize
= value;
//将总页数保存在ViewState["PageSize"] 中
ViewState["PageSize"] = pageSize;
ChangePage(currentPage);
}
}
}

///<summary>
/// 添加图层之前
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
//用委托添加添加ChangePage委托
publicdelegatevoid PageChangedEventHandler(object sender, int e);
//用委托改ChangePage添加委托
publicevent PageChangedEventHandler PageChanged;
protectedvirtualvoid OnPageChanged(int e)
{
//如果存在事件响应,则将当前点击的事件和页数传递
if (PageChanged !=null)
{
PageChanged(
this, e);
}
}

protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//ViewState["CurrentPage"] = 1;
//ViewState["PageCount"] = 1;
}
else
{
currentPage
= Convert.ToInt32(ViewState["CurrentPage"]);
pageCount
= Convert.ToInt32(ViewState["PageCount"]);
pageSize
= Convert.ToInt32(ViewState["PageSize"]);
}
}
protectedvoid LinkButton_Click(object sender, EventArgs e)
{
int iTmpCurrent =1;
LinkButton myLinkButton
= (LinkButton)sender;

if (myLinkButton.CommandName =="First")
{
iTmpCurrent
=1;
}
elseif (myLinkButton.CommandName =="Previous")
{
iTmpCurrent
= currentPage -1;
}
elseif (myLinkButton.CommandName =="Next")
{
iTmpCurrent
= currentPage +1;
}
elseif (myLinkButton.CommandName =="Last")
{
iTmpCurrent
= pageCount;
}
elseif (myLinkButton.CommandName =="Goto")
{
int iGoto =1;
if (int.TryParse(this.TextBoxPage.Text, out iGoto))
{
if (iGoto <=1)
{
iGoto
=1;
}
if (iGoto > pageCount)
{
iGoto
= pageCount;
}
iTmpCurrent
= iGoto;
}
else
{
iTmpCurrent
= currentPage;
}

}
//iTmpCurrent要跳转的页数
ChangePage(iTmpCurrent);
//currentPage当前页数,点击事件触发
OnPageChanged(currentPage);
}
//改变页数方法
privatevoid ChangePage(int page)
{
currentPage
= page;
//加载热点商品推荐
this.LinkButtonGoto.Enabled =true;

if (page <=1)
{
this.LinkButtonFirst.Enabled =false;
this.LinkButtonPrevious.Enabled =false;
this.LinkButtonNext.Enabled =true;
this.LinkButtonLast.Enabled =true;
currentPage
=1;
}
elseif (page >= pageCount)
{
this.LinkButtonFirst.Enabled =true;
this.LinkButtonPrevious.Enabled =true;
this.LinkButtonNext.Enabled =false;
this.LinkButtonLast.Enabled =false;
currentPage
= pageCount;
}
else
{
this.LinkButtonFirst.Enabled =true;
this.LinkButtonPrevious.Enabled =true;
this.LinkButtonNext.Enabled =true;
this.LinkButtonLast.Enabled =true;

}
if (pageCount <=1)
{
this.LinkButtonFirst.Enabled =false;
this.LinkButtonPrevious.Enabled =false;
this.LinkButtonNext.Enabled =false;
this.LinkButtonLast.Enabled =false;
this.LinkButtonGoto.Enabled =false;
}
//currentPage.ToString()当前页数。pageCount.ToString();总页数
this.LabelMessage.Text = currentPage.ToString() +" / "+ pageCount.ToString();
this.TextBoxPage.Text = currentPage.ToString();
//将当前的页数保存在ViewState中
ViewState["CurrentPage"] = currentPage;
}
}

调用实现:

View Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

publicpartialclass learning : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
//设置控件点击事件
this.WebUserControl1.PageChanged +=new control_WebUserControl.PageChangedEventHandler(WebUserControl1_PageChanged);
if (!Page.IsPostBack)
{
DataInit();
DataLoad();
}
}

void WebUserControl1_PageChanged(object sender, int e)
{
//加载事件
DataLoad();
}
//根据查询数据,依据控件内的当前页数和每页数量和每页大小查询 并返回
publicstring DataLoad()
{
string sql1 ="select ROW_NUMBER() over (order by NewsTimes) as RowNum,NewsTitle,NewsTimes,NewsURL,NewsTypeId,NewsContent,NewsId from News where NewsTypeId=2";
string sql ="select * from ("+ sql1 +") As T where T.RowNum>"+ (this.WebUserControl1.CurrentPage -1) *this.WebUserControl1.PageSize +" And T.RowNum<="+this.WebUserControl1.CurrentPage *this.WebUserControl1.PageSize+"order by NewsTimes desc";
return DataLoadTitle1(sql);
}
///<summary>
/// 返回资讯标题
///</summary>
///<param name="Sql"></param>
///<returns></returns>
publicstaticstring DataLoadTitle1(string Sql)
{
Conndb.Conndb conn
=new Conndb.Conndb();
conn.Open();
DataSet ds
= conn.ExeSelectSql(Sql);
string sa ="";
string s, url, Time, s2, s1;

foreach (DataRow dr in ds.Tables[0].Rows)
{
s
= dr["NewsTitle

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET初识Cookie发布时间:2022-07-10
下一篇:
asp.net动态运用webservice并设置超时时间发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap