注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我只是一个人

我爱一个人

 
 
 

日志

 
 

分页  

2011-02-28 23:07:47|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

aspnetpager.dll

 

 

    /// <summary>
    /// 为控件绑定数据
    /// </summary>
    protected void getShu()
    {

            //Response.Write("dddddd");
            string connstring = ConfigurationManager.ConnectionStrings["ASPNET3WEBDBCONNECTIONSTRING"].ConnectionString;
            string sqlstring = "select * from [User]";
            SqlConnection conn = new SqlConnection(connstring);
            SqlDataAdapter da = new SqlDataAdapter(sqlstring, conn);
            DataSet ds = new DataSet();
            //查询总数
            AspNetPager2.RecordCount = 100; //ds.Tables["tab"].Rows.Count;
            AspNetPager2.PageSize = 10;//每页显示数目
            AspNetPager2.AlwaysShow = true;//是否一直显示分页空间
            //AspNetPager2.CurrentPageIndex = page + 1;
            AspNetPager2.EnableUrlRewriting = true;
            AspNetPager2.UrlPaging = true;
            AspNetPager2.UrlRewritePattern = "http://localhost:3184/Sample_10/CommandArgument.aspx?page={0}";
            da.Fill(ds, AspNetPager2.PageSize * (AspNetPager2.CurrentPageIndex - 1), AspNetPager2.PageSize, "tab");//数据源,起始记录,显示记录数,表名
            gvData.DataSource = ds.Tables["tab"];
            gvData.DataBind();
  
     

       
    }

    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        getShu();
    }
    protected void AspNetPager2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager2.CurrentPageIndex = e.NewPageIndex;
        getShu();
    }

 

 

自己写的

 

 

<body>
    <form id="form1" runat="server">
    <div>
  <asp:DataList ID="dlData" runat="server" CellPadding="4" DataKeyField="ID" RepeatColumns="5"
   RepeatDirection="Horizontal" BackColor="White" BorderColor="#336666"
   BorderStyle="Double" BorderWidth="3px"
   GridLines="Horizontal">
   <footerstyle backcolor="White" forecolor="#333333" />
   <itemstyle backcolor="White" forecolor="#333333" />
   <selecteditemstyle backcolor="#339966" font-bold="True" forecolor="White" />
   <headerstyle backcolor="#336666" font-bold="True" forecolor="White" />
   <ItemTemplate>
    ID:
    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
    <br />
    Username:
    <asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
    <br />
    Email:
    <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
    <br />
    TelePhone:
    <asp:Label ID="TelePhoneLabel" runat="server" Text='<%# Eval("TelePhone") %>' />
    <br />
    Address:
    <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
    <br />
    Postcode:
    <asp:Label ID="PostcodeLabel" runat="server" Text='<%# Eval("Postcode") %>' />
    <br />
    CreateDate:
    <asp:Label ID="CreateDateLabel" runat="server"
     Text='<%# Eval("CreateDate") %>' />
    <br />
    State:
    <asp:Label ID="StateLabel" runat="server" Text='<%# Eval("State") %>' />
    <br />
    Remark:
    <asp:Label ID="RemarkLabel" runat="server" Text='<%# Eval("Remark") %>' />
    <br />
    <br />
   </ItemTemplate>
  </asp:DataList><br />
  <table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#009DE9">
   <tr>
    <td height="40" align="center" valign="middle" bgcolor="#FFFFFF" class="Text">
     <asp:Label ID="lbCurrentIndex" runat="server" CssClass="Text"></asp:Label>&nbsp;
     <asp:ImageButton ID="ibtFirst" runat="server" ImageUrl="~/App_Themes/Aspnet3DBWeb/Images/blfirst.gif" CommandName="first" OnCommand="Page_Command" CausesValidation="False" />&nbsp;
     <asp:ImageButton ID="ibtPrev" runat="server" ImageUrl="~/App_Themes/Aspnet3DBWeb/Images/blprev.gif" CommandName="prev" OnCommand="Page_Command" CausesValidation="False" />&nbsp;
     <asp:ImageButton ID="ibtNext" runat="server" ImageUrl='~/App_Themes/Aspnet3DBWeb/Images/blnext.gif' CommandName="next" OnCommand="Page_Command" CausesValidation="False" />&nbsp;
     <asp:ImageButton ID="ibtLast" runat="server" ImageUrl="~/App_Themes/Aspnet3DBWeb/Images/bltail.gif" CommandName="last" OnCommand="Page_Command" CausesValidation="False" />
     &nbsp;转移到<asp:TextBox ID="tbMove" runat="server" Width="30px" CssClass="TextBox" SkinID="tbSkin">1</asp:TextBox>页<asp:RangeValidator ID="rvPage" runat="server" ControlToValidate="tbMove"
      Display="Dynamic" ErrorMessage="页码输入错误" MinimumValue="1" Type="Integer" MaximumValue="10000"></asp:RangeValidator><asp:RequiredFieldValidator ID="rfPage" runat="server" ControlToValidate="tbMove"
      Display="Dynamic" ErrorMessage="请输入页码"></asp:RequiredFieldValidator>
     <asp:ImageButton ID="ibtMove" runat="server" CommandName="move" ImageUrl='~/App_Themes/Aspnet3DBWeb/Images/bldo.gif' OnCommand="Page_Command" />   
    </td>
   </tr>
  </table>
    </div>
    </form>

 

.cs文件:

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DataListPaging : System.Web.UI.Page
{
 private const string TOTALPAGEINDEX = "TOTALPAGEINDEX";
 private const string CURRENTPAGEINDEX = "CURRENTPAGEINDEX";

 /// <summary>
 /// 每一页显示的记录数量,默认值为10
 /// </summary>
 private int PageCount = 10;
 /// <summary>
 /// 当前页码
 /// </summary>
 private int CurrentPageIndex = -1;
 /// <summary>
 /// 总页码数量
 /// </summary>
 private int TotalPageIndex = 0;

 protected void Page_Load(object sender,EventArgs e)
 {  
  ///获取总页码数量
  if(ViewState[TOTALPAGEINDEX] != null)
  {
   TotalPageIndex = Int32.Parse(ViewState[TOTALPAGEINDEX].ToString());
  }
  ///获取当前页码
  if(ViewState[CURRENTPAGEINDEX] != null)
  {
   CurrentPageIndex = Int32.Parse(ViewState[CURRENTPAGEINDEX].ToString());
  }
  ///初始化页面的数据,并设置为第一页。
  if(!Page.IsPostBack)
  {
   PagingDataInit();
   BindCurrentPageData();
  }
 }

 private void PagingDataInit()
 {   ///获取数据
  ASPNET3DBBook.UserDB user = new ASPNET3DBBook.UserDB();
  DataSet ds = user.GetUsers();

  if(ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) return;
  if(PageCount == 0) return;
  ///计算总页码数量
  TotalPageIndex = ds.Tables[0].Rows.Count / PageCount;
  if(TotalPageIndex * PageCount < ds.Tables[0].Rows.Count)
  {
   TotalPageIndex++;
  }
  ///保存总页码数量
  ViewState[TOTALPAGEINDEX] = TotalPageIndex;
  ///如果总页码大于0,则设置当前页码为第一页
  if(TotalPageIndex > 0)
  {
   CurrentPageIndex = 0;
   ///保存当前页码
   ViewState[CURRENTPAGEINDEX] = CurrentPageIndex;
  }
 }
 private void BindCurrentPageData()
 {
  if(PageCount <= 0 && CurrentPageIndex < 0) return;
  ///获取数据
  ASPNET3DBBook.UserDB user = new ASPNET3DBBook.UserDB();
  DataSet dsCurrent = user.GetCurrentUsers(PageCount * CurrentPageIndex,PageCount);

  ///显示数据
  dlData.DataSource = dsCurrent;
  dlData.DataBind();
  ///控制分页按钮的可用性
  SetPageButtonEnable();
  ShowCurrentIndex();
 }

 /// <summary>
 /// 分页函数,执行【上一页】、【下一页】和直接跳转到指定页码的操作。
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Page_Command(object sender,CommandEventArgs e)
 {
  string commandName = e.CommandName.ToLower();
  if(string.IsNullOrEmpty(commandName) == true) return;

  ///判断总页码数量是否合法
  if(TotalPageIndex <= 0) return;

  switch(commandName)
  {
   case "first":   ///首页
    {
     CurrentPageIndex = 0;
     break;
    }
   case "prev":   ///上一页
    {
     CurrentPageIndex = Math.Max(0,CurrentPageIndex - 1);
     break;
    }
   case "next":   ///下一页
    {
     CurrentPageIndex = Math.Min(TotalPageIndex - 1,CurrentPageIndex + 1);
     break;
    }
   case "last":   ///末页
    {
     CurrentPageIndex = TotalPageIndex - 1;
     break;
    }
   case "move":   ///直接跳转到指定的页码
    {
     int page = Int32.Parse(tbMove.Text.Trim());
     if(page > 0 && page <= PageCount)
     {
      CurrentPageIndex = page - 1;
     }
     break;
    }
   default: break;
  }
  ///保存当前页码
  ViewState[CURRENTPAGEINDEX] = CurrentPageIndex;
  ///重新绑定控件的数据
  BindCurrentPageData();
 }

 /// <summary>
 /// 设置分页按钮(【首页】、【上一页】、【下一页】、【末页】、【执行】)的可用性
 /// </summary>
 private void SetPageButtonEnable()
 {
  ///判断总页码数量是否合法
  if(TotalPageIndex <= 0) return;

  ///如果页数量小于1,则所有分页按钮不可用
  if(TotalPageIndex <= 1)
  {
   ibtFirst.Enabled = ibtPrev.Enabled = ibtNext.Enabled = ibtLast.Enabled = ibtMove.Enabled = false;
   return;
  }
  ///如果是第一页,【上一页】按钮不可用
  ibtPrev.Enabled = CurrentPageIndex == 0 ? false : true;
  ///如果是最后一页,【下一页】按钮不可用
  ibtNext.Enabled = CurrentPageIndex == TotalPageIndex - 1 ? false : true;
  tbMove.Enabled = true;
 }

 /// <summary>
 /// 设置当前页码和所有页码
 /// </summary>
 private void ShowCurrentIndex()
 {
  ///判断总页码数量是否合法
  if(TotalPageIndex <= 0) return;
  ///显示页码信息
  lbCurrentIndex.Text = "当前第" + (CurrentPageIndex + 1).ToString()
   + "页,共" + TotalPageIndex.ToString() + "页";
 } 
}

  评论这张
 
阅读(98)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017