注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

K8拉登哥哥's Blog

K8搞基大队[K8team] 信息安全 网络安全 0day漏洞 渗透测试 黑客

 
 
 

日志

 
 

ASP.NET分页之自定义分页(假分页)-DataList  

2011-12-27 05:30:02|  分类: asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
ASP.NET分页之自定义分页(假分页)

本文来自 CSDN 博客 作者:Cici Cheng

 

自定义分页中主要是有比较复杂的逻辑内容,其实思想还是比较简单。具体示例如下:

页面源码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

 

<!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>ASP.NET分页之自定义分页</title>

</head>

<body>

<form id="form1" runat="server">

<asp:DataList ID="DataList1" runat="server" DataKeyField="o_id" RepeatColumns="4" RepeatDirection="Horizontal">

<ItemTemplate>

<table>

<tr>

<td align="right">编号:</td>

<td><%# Eval("o_id") %></td>

</tr>

<tr>

<td align="right">县市名称:</td>

<td><%# Eval("o_name") %></td>

</tr>

<tr>

<td align="right">市编号:</td>

<td><%# Eval("c_id") %></td>

</tr>

</table>

</ItemTemplate>

</asp:DataList>

<br />

<table border="0" cellpadding="0" cellspacing="0">

<tr>

<td>

共<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>页, 当前第<asp:Label

ID="lblCurrPage" runat="server" ForeColor="Red"></asp:Label>页&nbsp;&nbsp;

共<asp:Label ID="lblTotalCount" runat="server" ForeColor="Red"></asp:Label>条记录, 每页显示<asp:Label

ID="lblPageSize" runat="server" Text="16" ForeColor="Red"></asp:Label>条&nbsp;&nbsp;

</td>

<td>

<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">首页</asp:LinkButton>&nbsp;

<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click">上一页</asp:LinkButton>&nbsp;

<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click">下一页</asp:LinkButton>&nbsp;

<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click">尾页</asp:LinkButton>&nbsp;

跳到<asp:DropDownList ID="ddlPager" runat="server" Width="50" OnSelectedIndexChanged="ddlPager_SelectedIndexChanged"

AutoPostBack="true" />



</td>

</tr>

</table>

</form>

</body>

</html>

 

后台代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data;

 

public partial class Default2 : System.Web.UI.Page

{

//页面加载时默认显示第一页

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindPaging("1");

}

}

 

//首页按钮被点击

protected void btnFirst_Click(object sender, EventArgs e)

{

BindPaging("1");

}

 

//上一页页按钮被点击

protected void btnPrev_Click(object sender, EventArgs e)

{

BindPaging("-1");

}

 

//下一页页按钮被点击

protected void btnNext_Click(object sender, EventArgs e)

{

BindPaging("+1");

}

 

//尾页按钮被点击

protected void btnLast_Click(object sender, EventArgs e)

{

BindPaging("0");

}

 

//下拉列表框选项发生改变

protected void ddlPager_SelectedIndexChanged(object sender, EventArgs e)

{

BindPaging(ddlPager.Text);

}

 

private void BindPaging(string page)

{

SqlDataSource sqlds = new SqlDataSource();

sqlds.ConnectionString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

sqlds.SelectCommand = "SELECT * FROM [County]";

sqlds.SelectCommandType = SqlDataSourceCommandType.Text;

DataView dv = sqlds.Select(DataSourceSelectArguments.Empty) as DataView;

 

int totalCount = dv.Count;//总条数

int pageSize = 16;//每页显示条数

int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);//总页数

int currPage; //当前页数

//设置当前页

switch (page)

{

//首页

case "1":

currPage = 1;

break;

//下一页

case "+1":

currPage = int.Parse(lblCurrPage.Text) + 1;

break;

//上一页

case "-1":

currPage = int.Parse(lblCurrPage.Text) - 1;

break;

//尾页

case "0":

currPage = pageCount;

break;

//下拉列表框直接传值时

default:

currPage = int.Parse(page);

break;

}

 

//加载DropDownList项

if (!IsPostBack)

{

for (int i = 1; i <= pageCount; i++)

{

ddlPager.Items.Add(i.ToString());

}

}

 

//设置分页信息的显示

lblCurrPage.Text = currPage.ToString();

lblPageCount.Text = pageCount.ToString();

lblPageSize.Text = pageSize.ToString();

lblTotalCount.Text = totalCount.ToString();

 

//设置翻页按钮的可用性

btnFirst.Enabled = btnPrev.Enabled = btnNext.Enabled = btnLast.Enabled = true;

if (currPage == 1)

{

btnFirst.Enabled = btnPrev.Enabled = false;

}

if (currPage == pageCount)

{

btnNext.Enabled = btnLast.Enabled = false;

}

ddlPager.Text = currPage.ToString();

 

//从查询出的所有记录中筛选出即将呈现的当前页的数据集合

int loopCount = (currPage == pageCount) ? totalCount - (currPage - 1) * pageSize : pageSize; //显示的记录条数

DataRowView[] drwsToBind = new DataRowView[loopCount];

for (int i = 0; i < loopCount; i++)

{

drwsToBind[i] = dv[(currPage - 1) * pageSize + i];

}

DataList1.DataSource = drwsToBind;

DataList1.DataBind();

}

}
  评论这张
 
阅读(740)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016