当前位置: 首页 > 文章分类 > 编程开发 > Asp.net > 文章页面

文章正文:

ASP.Net实例之百度分页程序

作者:佚名 时间:2006-9-27 21:15:45 来源:网络

后台代码:
namespace search.Modules
{
  using System;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;

  using System.Data.SqlClient;

  /// <summary>
  ///    ProductDetail 的摘要说明。
  /// </summary>
  public class SupplyPage : System.Web.UI.UserControl
  {

    protected System.Web.UI.WebControls.Repeater ProductList;
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.WebControls.Image productImage;
    protected System.Web.UI.WebControls.Label lblError;
    public int resultCount = 0;

    //定义存储过程分页(GetRecordFromPage)  
    private static string tblName = "infoTotal";
    private static string fldName = "add_time";
    private static int PageSize = 5;
    private static int PageIndex= 1;
    private static int OrderType = 1;//0表示升序,非0表示降序
    private static string strwhere =string.Empty;//"CategoryId="+int.Parse(Request.QueryString["categoryId"]).ToString(); 不能这样写,要在下面定义一个方法 
    //分页六个参数.end

    private string DivPageString;
    private int DivPageIndex;//DivPageIndex 值在分页显示页码的时候用到
    private string Keywords = string.Empty;
    private string KeywordsURL = string.Empty;
    private string URLpara = string.Empty;

    private void SetParameter()
    {
      strwhere = "type="+int.Parse(Request.QueryString["type"]).ToString();
      Keywords=Request.QueryString["PreKeyword"]; 
      //地址相同部分的处理,用于分页的地址链接
      URLpara = "&type="+
        Request.QueryString["type"];

      if ( Keywords != null )
      {
        strwhere = strwhere + " and ((title like '%"+Keywords+"%') or (keywords like '%"+Keywords+"%'))";
        KeywordsURL = HttpUtility.UrlEncode(Keywords);
        URLpara = URLpara + "&PreKeyword="+
          KeywordsURL.ToString();
      }

      if(Request.QueryString["page"]!=null)
      {
        PageIndex = Convert.ToInt16(Request.QueryString["page"]);
        
        if ( PageIndex > 1 ){DivPageIndex=1;}
        if ( PageIndex > 2 ){DivPageIndex=2;}
        if ( PageIndex > 3 ){DivPageIndex=3;}
        if ( PageIndex > 4 ){DivPageIndex=4;}
        if ( PageIndex > 5 ){DivPageIndex=5;}
        if ( PageIndex > 6 ){DivPageIndex=6;}
        if ( PageIndex > 7 ){DivPageIndex=7;}
      }
      else
      {
        PageIndex = 1;
      }


    }
    private void Page_Load(object sender, System.EventArgs e)
    {
      // 在此处放置用户代码以初始化页面
    
      SetParameter();

      if (!Page.IsPostBack)
      {
        //显示第一页的记录
        ShowResult(tblName , fldName ,PageSize , PageIndex , OrderType , strwhere );
      }
    }
    void ShowResult( string tbName ,string fldName , int PageSize , int PageIndex ,int OrderType , string strwhere)
    {
      //绑定Repeater控件
      ProductList.DataSource = BLL.Product.GetProduct(tblName , fldName ,PageSize , PageIndex , OrderType , strwhere );
      ProductList.DataBind();
      //调用Product类中的方法获得该类商品的总数
      resultCount = BLL.Product.GetProductCount(tblName , fldName , strwhere );

      Label lblRecord = (Label)this.Page.FindControl("RecordCount");

      lblRecord.Text = resultCount.ToString();

      int count;
    
      if(resultCount == 0)
      {
        this.lblError.Visible = true;
        this.lblError.Text = "没有查找到相关的数据!";
        this.lblError.ForeColor=Color.Red;
        this.lblError.Font.Bold = true;
      
      }
    
    
      //如果查询结果总数是页大小的整数倍
      if (resultCount%PageSize == 0)
      {
        count = resultCount/PageSize;
        //PageCount.Text = count.ToString();
      }
      else
      {
        count = resultCount/PageSize+1;
        //PageCount.Text = count.ToString();
      }

      //分页显示的控制

      DivPageString="共"+count.ToString()+"页&nbsp;&nbsp;&nbsp;&nbsp;";
      
      //显示<上一页>
      if ( PageIndex > 1 )
      {
        DivPageString=DivPageString+"&nbsp;<a href=\"search.aspx?page="+
          (PageIndex-1).ToString()
          +URLpara
          +"\">上一页</a>";
      }
      //显示当前页的面前几页
      //
      for(int i=PageIndex-DivPageIndex; i<=PageIndex-1; i++)
      {
        DivPageString=DivPageString+"&nbsp;<a href=\"search.aspx?page="+
          i.ToString()
          +URLpara
          +"\">"+
          i.ToString()
          +"</a>";
      }

      //显示当前页
      DivPageString=DivPageString+"&nbsp;<font color='#FF0000'>"+PageIndex.ToString()+"</font>";
      //显示当前页的后前几页

      for(int i=PageIndex+1; i<=PageIndex+7; i++)
      { 
        if(i>count){break;}
        DivPageString=DivPageString+"&nbsp;<a href=\"search.aspx?page="+
          i.ToString()
          +URLpara
          +"\">"+
          i.ToString()
          +"</a>";
        
      }
      //显示<下一页>
      if( PageIndex < count )
      {
        DivPageString=DivPageString+"&nbsp;<a href=\"search.aspx?page="+
          (PageIndex+1).ToString()
          +URLpara
          +"\">下一页</a>";
      }

      //分页显示完成
    }
    //返回值到容器中
    public string strPage //取值
    {
      get
      {
        return ReturnPageString();
      }
    }
    private string ReturnPageString() //返值
    {
      return DivPageString;//
      
    }

    

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

    }
    #endregion

    private void ProductList_ItemDataBound(object sender,System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
      if(e.Item.ItemType == ListItemType.alternatingItem  e.Item.ItemType == ListItemType.Item)
      {

      }
    }

    
  }
}

前台代码:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="SupplyPage.ascx.cs" Inherits="search.Modules.SupplyPage" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:repeater id="ProductList" runat="server">
            <ItemTemplate>
              <div class="list">
<!--产品图片插入-->
<!--<img src="temp/aa.gif" class="l-img" />-->

<asp:Image ID="productImage" Runat="server" ImageUrl=<%# DataBinder.Eval(Container.DataItem,"photo")%> CssClass="l-img" Visible=<%#(DataBinder.Eval(Container.DataItem,"photo")!=System.DBNull.Value?true:false)%>></asp:Image> 
<ul>
<li class="l-tit"><a href="#"><%# DataBinder.Eval(Container.DataItem,"title").ToString().Length>22?DataBinder.Eval(Container.DataItem,"title").ToString().Substring(0,22).Replace("\n\r","<br>"):DataBinder.Eval(Container.DataItem,"title").ToString().Replace("\n\r","<br>") %></a><span class="l-time"><%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"add_time")).ToShortDateString() %></span></li>
<li class="content"><%# DataBinder.Eval(Container.DataItem,"content").ToString().Length>82?DataBinder.Eval(Container.DataItem,"content").ToString().Substring(0,82).Replace("\n\r","<br>"):DataBinder.Eval(Container.DataItem,"content").ToString().Replace("\n\r","<br>") %></li>
<li class="contact"><img src="images/talk.jpg" width="97" height="21" border="0" alt="在线洽谈" />联系人:<%# DataBinder.Eval(Container.DataItem,"real_name").ToString() %> [<a href="#">全部供应信息</a>]</li>
</ul>
</div>
            </ItemTemplate>
</asp:repeater>
<asp:Label ID="lblError" Runat="server" Visible="False" Height="120px" Width="500px"></asp:Label>
<!--分页开始-->
<div id="page">
<asp:Panel id="Panel1" runat="server"><%= strPage%></asp:Panel>
</div>
<!--分页结束-->

[责任编辑:]
评论列表:
暂无评论
登录名: 密码:匿名发表(无需注册)