查看: 6234|回复: 0
打印 上一主题 下一主题

使用DataList实现分页的例子

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-19 23:47:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
数据表名称为:guest
包括字段:id(int),name(varchar),face(varchar),sex(varchar),caption(varchar),postdate(datetime),content(text),homepage(varchar),ip(varchar),qq(varchar),email(varchar)




<body MS_POSITIONING="GridLayout">
       <form method="post" action="default.aspx" runat="server" ID="Form1">
           <table width="100%" border="0" cellpadding="2" cellspacing="0" align="center">
               <tr>
                   <td align="center">
                       <asp:DataList ID="Myguest" RepeatColumns="1" Runat="server">
                           <ItemTemplate>
                               <table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center"
                                   width="610">
                                   <tr>
                                       <td>
                                           <table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center"
                                               width="610">
                                               <tr>
                                                   <td>
                                                       <table width="100%" border="0" cellpadding="6" cellspacing="0" class="gueststylebg" align="center">
                                                           <tr>
                                                               <td width="112" align="center" height="153" valign="top">
                                                                   <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" class="gueststylebg">
                                                                       <tr>
                                                                           <td>
                                                                               <table border="0" cellspacing="0" cellpadding="0" align="center" height="25">
                                                                                   <tr>
                                                                                       <td class="FILTER">
                                                                                           <div align="center"><font style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><%# DataBinder.Eval(Container.DataItem,"name") %></font></div>
                                                                                       </td>
                                                                                   </tr>
                                                                               </table>
                                                                               <div align="center"><font style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><%#"<img src=\""+ DataBinder.Eval(Container.DataItem,"face")+ "\" width=\"80\" height=\"90\" align=\"middle\">"%></font><br>
                                                                                   <br>
                                                                                   ★
                                                                                   <%# DataBinder.Eval(Container.DataItem,"sex") %>
                                                                                   ★</div>
                                                                           </td>
                                                                       </tr>
                                                                   </table>
                                                               </td>
                                                               <td align="center" height="153" width="2">
                                                                   <table class="gueststyleline" width="1" height="100%" border="0" cellpadding="0" cellspacing="0">
                                                                       <tr>
                                                                           <td width="1"></td>
                                                                       </tr>
                                                                   </table>
                                                               </td>
                                                               <td height="33" valign="top" width="463">
                                                                   <table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" style="TABLE-LAYOUT: fixed"
                                                                       height="20">
                                                                       <tr>
                                                                           <td height="14" width="310"><img src="webimages/icon/subject.gif" width="15" height="15" alt="主题">:<%# DataBinder.Eval(Container.DataItem,"caption") %><br>
                                                                           </td>
                                                                           <td width="151" height="14"><font style="0pt"> <img src="webimages/icon/posttime.gif" width="16" height="15" alt="发表时间[北京时间]">:<%# DataBinder.Eval(Container.DataItem,"postdate") %></font></td>
                                                                       </tr>
                                                                   </table>
                                                                   <hr class="gueststylehr" size="0.1" width="100%">
                                                                   <table width="100%" border="0" cellpadding="5" cellspacing="0" class="gueststylebg" height="45%"
                                                                       style="TABLE-LAYOUT: fixed">
                                                                       <tr>
                                                                           <td valign="top" width="28" align="center"><img src="webimages/emot/1.gif" width="16" height="16">
                                                                           </td>
                                                                           <td valign="top" width="94%" style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt">
                                                                               <%# DataBinder.Eval(Container.DataItem,"content") %>
                                                                           </td>
                                                                       </tr>
                                                                   </table>
                                                                   <table width="100%" border="0" cellpadding="4" cellspacing="0">
                                                                       <tr>
                                                                           <td valign="top">
                                                                               <hr class="gueststylehr" size="0.1" width="100%">
                                                                               <%#"<a href=\""+ DataBinder.Eval(Container.DataItem,"homepage")+" \" target=\"_blank\"><img src=\"webimages/icon/home.gif\" width=\"44\" height=\"16\" border=\"0\"></a>"%>
                                                                                
                                                                               <%#"<a target=\"_blank\" href=\"http://www.chinagz.net/ip/ipsearch.cgi?ip="+DataBinder.Eval(Container.DataItem,"ip")+"\"><img src=\"webimages/icon/ip.gif\" align=\"absmiddle\" height=\"16\" border=\"0\"></a>"%>
                                                                                
                                                                               <%#"<a href=\"http://search.tencent.com/cgi-bin/friend/user_show_info?ln="+DataBinder.Eval(Container.DataItem,"qq") +"\" target=\"_blank\"><img src=\"webimages/icon/qq.gif\" border=\"0\" width=\"35\" height=\"16\"></a>"%>
                                                                                
                                                                               <%# "<a href=\"mailto:"+DataBinder.Eval(Container.DataItem,"email")+"\"><img src=\"webimages/icon/email.gif\" width=\"42\" height=\"16\" border=\"0\"></a>"%>
                                                                                <a href="#"><img src="webimages/icon/postguest.gif" width="45" height="16" border="0"></a>
                                                                           </td>
                                                                       </tr>
                                                                   </table>
                                                               </td>
                                                           </tr>
                                                       </table>
                                                   </td>
                                               </tr>
                                           </table>
                                       </td>
                                   </tr>
                               </table>
                           </ItemTemplate>
                       </asp:DataList>
                       <BR>
                   </td>
               </tr>
           </table>
           <table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" height="9">
               <TBODY>
                   <tr>
                       <td align="center">
                           <table class="gueststyleline" width="610" border="0" cellpadding="1" cellspacing="0" height="18">
                               <tr>
                                   <td height="24">
                                       <table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="18">
                                           <tr>
                                               <td width="35%" align="center" height="36">
                                                   共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
                                               </td>
                                               <td align="center" height="36">
                                                   <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" runat="server" />
                                                   <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" runat="server" />
                                               </td>
                                           </tr>
                                       </table>
                                   </td>
                               </tr>
                           </table>
                       </td>
                   </tr>
               </TBODY>
           </table>
       </form>
   </body>








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.SqlClient;

namespace CrystalReport
{
   /// <summary>
   /// _default 的摘要说明。
   /// </summary>
   public class _default : System.Web.UI.Page
   {
       protected System.Web.UI.WebControls.DataList Myguest;
       protected System.Web.UI.WebControls.Label lblRecordCount;
       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.LinkButton lbnNextPage;
       protected int PageSize;
       protected int CurrentPage;
       protected int RecordCount;
       protected int PageCount;

   
       private void Page_Load(object sender, System.EventArgs e)
       {
           // 在此处放置用户代码以初始化页面
           PageSize=5;
           if(!Page.IsPostBack)
           {
               ListBind();
               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;
           }

       }

       #region Web 窗体设计器生成的代码
       override protected void OnInit(EventArgs e)
       {
           //
           // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
           //
           InitializeComponent();
           base.OnInit(e);
       }
      
       /// <summary>
       /// 设计器支持所需的方法 - 不要使用代码编辑器修改
       /// 此方法的内容。
       /// </summary>
       private void InitializeComponent()
       {   
           this.lbnPrevPage.Click += new System.EventHandler(this.Page_OnClick);
           this.lbnNextPage.Click += new System.EventHandler(this.Page_OnClick);
           this.Load += new System.EventHandler(this.Page_Load);

       }
       #endregion


       //计算总共有多少条记录
       public int CalculateRecord()
       {
           SqlConnection Conn= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
           Conn.Open();
           int intCount;
           string strCount = "select count(*) as co from guest";
           SqlCommand MyComm = new SqlCommand (strCount,Conn);
           SqlDataReader dr = MyComm.ExecuteReader();
           if(dr.Read())
           {
               intCount = Int32.Parse(dr["co"].ToString());
           }
           else
           {
               intCount = 0;
           }
           dr.Close();
           return intCount;
       }

       ICollection CreateSource()
       {
           SqlConnection Conn= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
           Conn.Open();
           int StartIndex;
           //设定导入的起终地址
           StartIndex = CurrentPage*PageSize;
           string strSel = "Select * from guest order by postdate desc";
           DataSet ds = new DataSet();
           SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Conn);
           MyAdapter.Fill(ds,StartIndex,PageSize,"guest");
           return ds.Tables["guest"].DefaultView;
       }


       public void ListBind()
       {
           Myguest.DataSource = CreateSource();
           Myguest.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();
       }




       private void lbnPrevPage_Click(object sender, System.EventArgs e)
       {
      
       }

       private void lbnNextPage_Click(object sender, System.EventArgs e)
       {
      
       }

       private void Page_OnClick(object sender, System.EventArgs e)
       {
           
           CurrentPage = (int)ViewState["PageIndex"];
           PageCount = (int)ViewState["PageCount"];
           string cmd =((LinkButton)sender).CommandName;

           //判断cmd,以判定翻页方向
           switch(cmd)
           {
               case "next":
                   if(CurrentPage<(PageCount-1))
                       CurrentPage++;
                   break;
               case "prev":
                   if(CurrentPage>0)
                       CurrentPage--;
                   break;
           }
           ViewState["PageIndex"] = CurrentPage;
           ListBind();
       }

   }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表