DataList自定义分页

 private void DataListBind()
  {
   SqlConnection con = new SqlConnection("server=wfserver;database=test;uid =sa;pwd=123;");
   SqlDataAdapter sda = new SqlDataAdapter("select * from Employees",con);
   DataSet ds= new DataSet();

   try
   {
    sda.Fill(ds,"testTable");
    this.lblall.Text = "共 "+Convert.ToString(ds.Tables["testTable"].Rows.Count)+" 条数据";
    PagedDataSource objpage = new PagedDataSource();
    objpage.DataSource =ds.Tables["testTable"].DefaultView;
    objpage.AllowPaging=true;
    objpage.PageSize=5;
    int CurPage;
    if(Request.QueryString["Page"]!=null)
    {
     CurPage = Convert.ToInt32(Request.QueryString["Page"]);
    }
    else
    {
     CurPage = 1;
    }
    this.lblcurpage.Text = "当前第 "+Convert.ToString(CurPage)+" 页";
    objpage.CurrentPageIndex = CurPage -1;
    if(!objpage.IsFirstPage)
    {
     //到上一页
     this.Hyperprev.NavigateUrl = Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(CurPage-1);
    }
    if(!objpage.IsLastPage)
    {
     //到下一页
     this.Hypernext.NavigateUrl = Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(CurPage+1);
    }
    this.DataList1.DataSource = objpage;
    this.DataList1.DataBind();
   }
   catch(Exception error)
   {
    Response.Write(error.ToString());
   }
  }

aspx中的代码:
<asp:datalist id="mydatalist" Runat="server">
     <ItemTemplate>
         <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="400" border="0">
              <TR>
                   <TD width="15"> </TD>
                   <TD width="85" height="22"> </TD>
                   <TD width="211"> </TD>
                   <TD width="80"> </TD>
              </TR>
              <TR>
                   <TD width="15"> </TD>
                   <TD class="ziti" bgColor="#f7eeef" colSpan="3" height="22">回  复:</TD>
              </TR>
              <TR>
                   <TD width="15"></TD>
                   <TD bgColor="#666666" colSpan="3" height="1"></TD>
              </TR>
              <TR>
                   <TD width="15" rowSpan="3"> </TD>
                   <TD class="ziti" vAlign="top" width="85" rowSpan="3">发贴人姓名<BR>
                                               级别:<BR>
                   </TD>
                   <TD class="ziti" vAlign="top" height="22"><IMG height="20" src="images.files/face0.gif" width="20">发表于:<%# DataBinder.Eval(Container.DataItem,"remessage_time") %></TD>
                   <TD class="brown" width="80">回复</TD>
              </TR>
              <TR>
                   <TD colSpan="2" height="5"></TD>
              </TR>
              <TR>
                   <TD><%# DataBinder.Eval(Container.DataItem,"remessage_context") %></TD>
                   <TD width="80"> </TD>
                   <TD width="4"> </TD>
              </TR>
              <TR>
                   <TD width="15"></TD>
                   <TD bgColor="#666666" colSpan="3" height="1"></TD>
              </TR>
         </TABLE>
     </ItemTemplate>
</asp:datalist>
<TABLE id="Table3" cellSpacing="0" cellPadding="0" width="400" border="0">
         <TR>
                   <TD width="15"> </TD>
                   <TD class="ziti" colSpan="3" height="22">共有<asp:label id="lblRecordCount" runat="server" ForeColor="red"></asp:label>条记录  
                                     当前为<asp:label id="lblCurrentPage" runat="server" ForeColor="red"></asp:label>/<asp:label id="lblPageCount" runat="server" ForeColor="red"></asp:label>页
                                     <asp:linkbutton id="lbnPrevPage"runat="server" Text="上一页" CommandName="prev" ></asp:linkbutton><asp:linkbutton id="lbnNextPage" runat="server" Text="下一页" CommandName="next" >下一页</asp:linkbutton></TD>
              <TR>
</table>

CS代码:
using System.Data.SqlClient;
using System.Configuration;

protected System.Web.UI.WebControls.Label lblmcontent;
         protected System.Web.UI.WebControls.Label lblmessagetime;
         protected System.Web.UI.WebControls.Label lblmkind;
         protected System.Web.UI.WebControls.Label lblmauthor;
         protected System.Web.UI.WebControls.Label lblmessagetitle;
         protected System.Web.UI.WebControls.DataList mydatalist;
         protected System.Web.UI.HtmlControls.HtmlForm Form1;
        
         int PageSize,RecordCount,PageCount,CurrentPage;
         protected System.Web.UI.WebControls.Label lblCurrentPage;
         protected System.Web.UI.WebControls.Label lblPageCount;
         protected System.Web.UI.WebControls.LinkButton lbnPrevPage;
         protected System.Web.UI.WebControls.Label lblRecordCount;
         protected System.Web.UI.WebControls.LinkButton lbnNextPage;

         private void Page_Load(object sender, System.EventArgs e)
         {
              // 在此处放置用户代码以初始化页面
              PageSize = 2;
              SqlConnection con=new SqlConnection();
              con.C.AppSettings["ConnectionString"];
              SqlCommand cmd=new SqlCommand();
              cmd.CommandText="select message_title,message_content,message_content,message_time,message_author from message where message_id= '"+Request.QueryString["id"]+"'";
              cmd.C;
              con.Open();
              
              if(!Page.IsPostBack)
              {

                   //设定PageSize

                   SqlDataReader reader=cmd.ExecuteReader();
                   if(reader.Read())
                   {
                       this.lblmauthor.Text=Convert.ToString(reader["message_author"]);
                       this.lblmcontent.Text=Convert.ToString(reader["message_content"]);
                       this.lblmessagetime.Text=Convert.ToString(reader["message_time"]);
                       this.lblmessagetitle.Text=Convert.ToString(reader["message_title"]);
                      
                   }

                   reader.Close();
                  

                   ListBind();
                   CurrentPage = 0;
                   ViewState["PageIndex"] = 0;

                   //计算总共有多少记录
                   RecordCount = CalculateRecord();
                   lblRecordCount.Text = RecordCount.ToString();

                   //计算总共有多少页
                //float pagetemp=0;
                   if((RecordCount%PageSize)==0)
                   PageCount = (RecordCount/PageSize);
                   else
                   PageCount = (RecordCount/PageSize)+1;
                   lblPageCount.Text = PageCount.ToString();
                   ViewState["PageCount"] = PageCount;

         }
         }
       //****************************
         public int CalculateRecord()
         {
              int intCount;
              SqlConnection recon=new SqlConnection();
              recon.C.AppSettings["ConnectionString"];
              SqlCommand recmd=new SqlCommand();
              recmd.CommandText="select count(*) as co from remessage where remessage_fatherid = '"+Request.QueryString["id"]+"'";
              recmd.C;
              recon.Open();
              SqlDataReader reader1=recmd.ExecuteReader();
              if(reader1.Read())
              {
                   intCount = Int32.Parse(reader1["co"].ToString());
              }
              else
              {
                   intCount = 0;
              }
              reader1.Close();
              return intCount;
         }

         ICollection CreateSource()
         {

              int StartIndex;
              SqlConnection recon=new SqlConnection();
              recon.C.AppSettings["ConnectionString"];

              //设定导入的起终地址
              StartIndex = CurrentPage*PageSize;
              string strSel = "select * from remessage where remessage_fatherid = '"+Request.QueryString["id"]+"'";
              DataSet ds = new DataSet();
            
              SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, recon);
              MyAdapter.Fill(ds,StartIndex,PageSize,"remessage");
              return ds.Tables["remessage"].DefaultView;
         }
          public void ListBind()
         {
              this.mydatalist.DataSource = CreateSource();
              this.mydatalist.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#" 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>


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 connstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"&#92;&#92;northwind.mdb;";
   conn = new OleDbConnection(connstr);
   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();
}
}
}

posted @ 2008-11-28 17:17  googlegis  阅读(149)  评论(0编辑  收藏  举报

坐标合肥,非典型GIS开发人员 GitHub