书剑先生 发表于 2008-5-31 19:17:20

求教---DATALIST分页问题

DATALIST分页

Page.aspx

代码:
<%@ Page language="c#" Codebehind="Page.cs" AutoEventWireup="false" Inherits="MikeCat_app2.MikeCat_DataListPage" %>
<HTML>
<HEAD>
<title>欢迎光临</title>
<meta name="GENERATOR" C>
<meta name="CODE_LANGUAGE" C>
<meta name="vs_defaultClientScript" c>
<meta name="vs_targetSchema" chttp://schemas.microsoft.com/intellisense/ie5" target=_blank>http://schemas.microsoft.com/intellisense/ie5">
<STYLE>
TD { FONT-SIZE: 12px }
.font { FONT-SIZE: 12px }
</STYLE>
</HEAD>
<body MS_POSITI>
<form id="form1" runat="server">
<asp:Label id="Label5" runat="server" Font-Bold="True" style="LEFT: 184px; POSITION: absolute; TOP: 24px">DATALIST分页</asp:Label>
<asp:datalist id="dList" Runat="server" HeaderStyle-BackColor="SteelBlue" Width="100%" EnableViewState="False"
    style="LEFT: 8px; POSITION: absolute; TOP: 64px">
    <HeaderTemplate>
    <TABLE cellSpacing="0" cellPadding="0" width="100%">
      <TR>
      <TD align="left">
      <FONT color="#ffffff">BLOGID</FONT></TD>
      <TD align="left">
      <FONT color="#ffffff">日志标题</FONT></TD>
      <TD align="left">
      <FONT color="#ffffff">日志内容</FONT></TD>
      </TR>
    </HeaderTemplate>
    <FooterTemplate>
    </TABLE>
    </FooterTemplate>
    <ItemTemplate>
    <tr>
      <td><%# DataBinder.Eval(Container.DataItem, "blogid") %></td>
      <td><%# DataBinder.Eval(Container.DataItem, "日志标题") %></td>
      <td><%# DataBinder.Eval(Container.DataItem, "日志内容") %></td>
    </tr>
    </ItemTemplate>
</asp:datalist><font>
    <asp:Panel id="Panel1" runat="server" Width="552px" Height="48px" style="LEFT: 160px; POSITION: absolute; TOP: 288px"
    CssClass="font">
    <asp:linkbutton id="fp1" runat="server" CommandName="first">首页</asp:linkbutton>
    <asp:linkbutton id="pp1" runat="server" CommandName="prev">上一页</asp:linkbutton>
    <asp:linkbutton id="np1" runat="server" CommandName="next">下一页</asp:linkbutton>
    <asp:linkbutton id="lp1" runat="server" CommandName="last">尾页</asp:linkbutton>
    <asp:label id="Label1" runat="server">页次</asp:label>
    <asp:Label id="cp1" runat="server"></asp:Label>
    <asp:label id="Label2" runat="server">/</asp:label>
    <asp:Label id="pc1" runat="server"></asp:Label>
    <asp:label id="Label3" runat="server">每页记录数</asp:label>
    <asp:Label id="rc1" runat="server"></asp:Label>
    <asp:Label id="Label4" runat="server">选择跳转页码</asp:Label>
    <asp:DropDownList id="jp1" runat="server" AutoPostBack="True"></asp:DropDownList>
    </asp:Panel></font></form>
</body>
</HTML>

page.cs
代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace MikeCat_app2
{
/// <summary>
/// MikeCat_DataListPage 的摘要说明。
/// *****************************************
/// 功能:DataList数据分页
/// *****************************************
/// </summary>
public class MikeCat_DataListPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dList;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.LinkButton pp1;
protected System.Web.UI.WebControls.LinkButton lp1;
protected System.Web.UI.WebControls.LinkButton np1;
protected System.Web.UI.WebControls.LinkButton fp1;
int PageSize,RecordCount,PageCount,CurrentPage;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label pc1;
protected System.Web.UI.WebControls.Label rc1;
protected System.Web.UI.WebControls.Label cp1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.DropDownList jp1;
protected System.Web.UI.WebControls.Label Label5;
OleDbConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
PageSize = 10;
string c+Server.MapPath(".")+"\\northwind.mdb;";
rc1.Text=PageSize.ToString();
CurrentPage=0;
RecordCount=this.GetRecordCount();
PageCount = (RecordCount%PageSize)>0?(RecordCount/PageSize)+1:(RecordCount/PageSize);
pc1.Text=PageCount.ToString();
ArrayList al=new ArrayList();
for(int i=1;i<PageCount+1;i++)
{
    al.Add(i);
}

if(!Page.IsPostBack)
{
    jp1.DataSource=al;
    jp1.DataBind();
    mfbind();
}
}
public int GetRecordCount()
{
conn.Open();
OleDbCommand cmd=new OleDbCommand("select count(*) as cnt from mfblog",conn);
int intCount=int.Parse(cmd.ExecuteScalar().ToString());
cmd.Dispose();
conn.Close();
return intCount;
}
private IList DataSource()
{
int StartIndex;
StartIndex = CurrentPage*PageSize;
string strSql = "select * from mfblog";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(strSql,conn);
da.Fill(ds,StartIndex,PageSize,"mfblog");
return ds.Tables["mfblog"].DefaultView;
}
private void mfbind()
{
dList.DataSource=(DataView)DataSource();
dList.DataBind();
np1.Enabled = true;
pp1.Enabled = true;
if(CurrentPage==(PageCount-1)) np1.Enabled = false;
if(CurrentPage==0) pp1.Enabled = false;
cp1.Text = (CurrentPage+1).ToString();
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage =int.Parse(cp1.Text.Trim())-1;
PageCount =int.Parse(pc1.Text.Trim());

string cnstr = e.CommandName;
switch(cnstr)
{
    case "first":
    CurrentPage=0;
    break;
    case "next":
    if(CurrentPage<(PageCount-1)) CurrentPage++;
    break;
    case "prev":
    if(CurrentPage>0) CurrentPage--;
    break;
    case "last":
    CurrentPage=PageCount-1;
    break;
}
mfbind();

}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{   
this.fp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.pp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.np1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.lp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.jp1.SelectedIndexChanged += new System.EventHandler(this.jp1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void jp1_SelectedIndexChanged(object sender, System.EventArgs e)
{
CurrentPage=int.Parse(jp1.SelectedItem.Text.Trim())-1;
mfbind();
}
}
}

[ 本帖最后由 书剑先生 于 2008-5-31 17:50 编辑 ]

书剑先生 发表于 2008-5-31 19:18:41

你的分页看起来很清楚,但是我在应用时,提示下面错误

编译器错误信息: ASPNET: 请确保此代码文件中定义的类与“inherits”属性匹配,并且该类扩展的基类(例如 Page 或 UserControl)是正确的。

行 1: using System;
原文
http://bbs.inlazy.com/printthread.php?t=11

主要是,Inherits和Codebehind要对应。
可是想了好久也不懂得如何解决,有哪位同学帮能帮帮呢?

[ 本帖最后由 书剑先生 于 2008-5-31 17:51 编辑 ]

南方以南 发表于 2008-5-31 19:23:50

额..........全都是代码,看不懂!

书剑先生 发表于 2008-5-31 19:43:47

就是因为是代码,才看不懂嘛呵呵。当然也是有同学懂得的
页: [1]
查看完整版本: 求教---DATALIST分页问题