如何使用AspNetPager给DataList分页

2024-11-30 00:01:18
推荐回答(2个)
回答1:

建三个Lable控件,ID分别为lb_page,lb_count,lb_currentpage,分别表示共有多少页,共有多少条记录,当前页是多少页
建四个LinkButton控件,ID分别为lbtn_frist,lbtn_up,lbtn_down,lbtn_last,分别表示首页,上一页,下一页,尾页
建一个DropDownList控件,表于选择要跳转到的页
当然肯定要建一个datalist控件,不用设置数据源,因为在.cs代码里设置,唯一要做的就是编辑一下模板
.ASPX代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





无标题页






'>
    
'>
   
'>
    
'>
  
'>
    
'>




记录    
   共

页     当前第
页    
首页
  
上一页
  
下一页
   尾页
  






.CS代码如下:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
dlbind();

}

public void dlbind()
{
int curpage = Convert.ToInt32(lb_currentpage.Text);
SqlConnection conn = new SqlConnection("server=.;database=student;uid=sa;pwd=");
SqlDataAdapter da = new SqlDataAdapter("select top 50 * from addr", conn);
DataSet ds = new DataSet();
da.Fill(ds, "addr");
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["addr"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 5;
ps.CurrentPageIndex = curpage - 1;
lb_page.Text = Convert .ToString ( ps.PageCount);
lb_count.Text = ps.DataSourceCount.ToString();
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.SelectedItem.Text = curpage.ToString();
}
lbtn_frist.Enabled = true;
lbtn_up.Enabled = true;
lbtn_down.Enabled = true;
lbtn_last.Enabled = true;
if (curpage == 1)
{
lbtn_frist.Enabled = false;
lbtn_up.Enabled = false;
}
if (curpage == ps.PageCount)
{
lbtn_down.Enabled = false;
lbtn_last.Enabled = false;
}
DataList1.DataSource = ps;
DataList1.DataKeyField = "id";
DataList1.DataBind();
}

protected void lbtn_frist_Click(object sender, EventArgs e)
{
lb_currentpage.Text = "1";
dlbind();
}

protected void lbtn_up_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) - 1);
dlbind();
}
protected void lbtn_down_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) + 1);
dlbind();
}

protected void lbtn_last_Click(object sender, EventArgs e)
{
lb_currentpage.Text = lb_page.Text;
dlbind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lb_currentpage.Text = page.ToString();
dlbind();
}
}

回答2:

第一步,拷贝AspNetPager.dll文件在网站bin目录下
第二步,在要引用的页面上,注册该程序集
EG;<%@Register TagPrefix="Webdiyer" Namespace="Wuqi.Webdiyer" Assembly="aspnetpager"%>

第二步:在你想设置页码的位置,放置控件;
eg;HorizontalAlign="right"
ShowCustomInfoSection="left"
style="margin-bottom:5px;"
ShowInputBox="always"
SubmitButtonStyle="border:1px solid #000066;height:18px;width:35px;margin-right:5px;"
InputBoxStyle="border:1px solid #000066;height:14px;width:35px;margin-right:5px;"
SubmitButtonText="转到"
NumericButtonTextFormatString="[{0}]"
OnPageChanged="Pager_OnChangePage"
Width="100%"
Height="22px"
ImagePath="./images"
MoreButtonType="Text" />

-----
第四步,设置事件处理:Pager_OnChangePage
完成...