datalist的分页
来源:互联网 发布:梦与叶樱动作数据 编辑:程序博客网 时间:2024/04/30 00:35
后代代码
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;
using System.Data.SqlClient;
public partial class datalist : System.Web.UI.Page
{
SqlConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(Object src, EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString=ConfigurationManager.AppSettings["strConn"].ToString() ;
MyConn = new SqlConnection(MyConnString);
MyConn.Open();
//第一次请求执行
if (!Page.IsPostBack)
{
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
}
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from t";
SqlCommand MyComm = new SqlCommand(strCount, MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from t";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "t");
return ds.Tables["t"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
共有<asp:Label ID="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label
ID="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label ID="lblPageCount"
ForeColor="red" runat="server" />页
<asp:DataList ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow">
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<ItemTemplate>
序号:<%# DataBinder.Eval(Container.DataItem,"tid") %>
题号:<%# DataBinder.Eval(Container.DataItem,"id") %>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick"
runat="server" />
<asp:LinkButton ID="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick"
runat="server" />
</div>
</form>
</body>
</html>
终于搞定了 做个备忘。。。
- DataList的分页技术
- DataList的分页
- DataList的分页技术
- datalist的分页
- DataList的分页技术
- DataList的分页
- datalist的分页
- DataList 分页的实现
- DataList的分页方法
- datalist分页的方法
- DataList分页的实现
- DataList的分页
- DataList的分页
- datalist的分页
- DataList分页的实现
- DataList的使用+分页
- Datalist的分页
- datalist分页问题的解决办法
- 我的诺基亚 E61 登录 Lotus Mobile Connect (VPN 网关), 以及访问IBM内网门户
- MFC入门(四) 工具条和状态栏
- “项目产品平台”我的编程人生
- 第五章 面向切面编程(AOP) 之二
- 模仿 msn 写的一个界面程序(源码),希望大家能够喜欢
- datalist的分页
- Apple 广告曲
- 关于s60的MenuBar
- 《GhostXP_SP2电脑公司经典版v8.2》LONGWANG制作
- Hibernate3映射到PostgreSQL中遇到的小问题!
- IE图标颜色变深的还原方法
- boost编译
- Flex实践——Parallel Practice
- mo