`
LiuXiaoYong
  • 浏览: 31146 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

(转)网上找的一个使用aspnetpager控件的使用方法

    博客分类:
  • .NET
阅读更多
前台页面代码:Page.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits="page" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Datalist分页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList ID="list" runat="server">
     <ItemTemplate>
     <%# DataBinder.Eval(Container.DataItem,"About_Title") %>
     </ItemTemplate>
    </asp:DataList>
   <webdiyer:AspNetPager ID="pagelist" runat="server" OnPageChanging="PageChanging" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" CustomInfoHTML="第%CurrentPageIndex%页,共%PageCount%页,每页%PageSize%条" ShowFirstLast="False" PageIndexBoxType="TextBox" ShowPageIndexBox="Never"> </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

注:不要忘记红色部分

后台页面:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using Wuqi.Webdiyer;//别忘记命名空间

public partial class page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTION"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT COUNT(*) FROM About";
            pagelist.AlwaysShow = true;
            pagelist.PageSize = 10; //显示页数
            pagelist.RecordCount = (int)cmd.ExecuteScalar(); //读出数据库里一共有多少条数据
            
            con.Close();
            DataBindlist();
        }
    }

    protected void DataBindlist()
    {
       
        SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTION"].ToString());
        SqlDataAdapter ad = new SqlDataAdapter("select * from About", con);
        DataSet ds = new DataSet();
        ad.Fill(ds, pagelist.PageSize * (pagelist.CurrentPageIndex - 1), pagelist.PageSize, "About");

        list.DataSource = ds.Tables["About"];
        list.DataBind();
      }

    protected void PageChanging(object src,PageChangingEventArgs e)
    {
        pagelist.CurrentPageIndex = e.NewPageIndex;
        DataBindlist();
    }
}





其实说实话,这个控件不好用,如果我们要完成分页功能,可以用.net提供的pagedateSource这个分页类来完成! 下面是用这个控件的部分代码,不全,建议还是用pagedateSource来分页,具体怎么实现你百度一下就ok了!

//页面加载事件
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
//将总条数放到分页控件中
DataTable dt1 = null;
dt1 = DataCtrl.selecttable("select count(classname) from mess_class","tabnl");
// this.pager.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]); 
this.AspNetPager1.RecordCount = System.Convert.ToInt32(dt1.Rows[0][0]);
Bind1();

}
private void Bind1()
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=xuedao;database=zhaoxuedaonet");
SqlCommand comm = new SqlCommand("select classname from mess_class",conn);
conn.Open();
comm.ExecuteNonQuery();

DataSet ds = new DataSet();
SqlDataAdapter dapter = new SqlDataAdapter(comm);
dapter.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"table");
this.DataList1.DataSource = ds.Tables["table"].DefaultView;
this.DataList1.DataBind();
//AspNetPager1.CustomInfoText = "记录总数:" + AspNetPager1.RecordCount.ToString() + "";
//AspNetPager1.CustomInfoText = "总页数:" + AspNetPager1.PageCount.ToString() + "";
//AspNetPager1.CustomInfoText = " 当前页:" + AspNetPager1.CurrentPageIndex.ToString() + ""; 

//myda.Fill(ds, pager.PageSize * (pager.CurrentPageIndex - 1), pager.PageSize, "article");
//this.dgList.DataSource = ds.Tables["article"];
//this.dgList.DataBind();
////动态设置用户自定义文本内容 
//pager.CustomInfoText = "记录总数:" + pager.RecordCount.ToString() + "";
//pager.CustomInfoText += " 总页数:" + pager.PageCount.ToString() + "";
//pager.CustomInfoText += " 当前页:" + pager.CurrentPageIndex.ToString() + ""; 


}
//分页事件
protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
Bind1();
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics