使用PagedDatasource在数据列表中分页
本文关键字:分页 列表 数据 PagedDatasource 使用 | 更新日期: 2023-09-27 18:13:58
我正在使用asp.net页面。我用的是DataList
控件。我看到这个控件没有分页模板,所以我使用PagedDataSource
控件(第一次)。我需要显示上一页,下一页和编号页,如:
previous 1 2 3 4 5 next View ALL
到目前为止我所做的是:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PagedDataSource pg = new PagedDataSource();
pg.DataSource = HRCompany.GetList().DefaultView;
pg.AllowPaging = true;
pg.PageSize = 8;
CompaniesDataList.DataSource = pg;
CompaniesDataList.DataBind();
}
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default page index of 0
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
private void cmdPrev_Click(object sender, System.EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;
// Reload control
PagedDataSource pg = new PagedDataSource();
pg.DataSource = HRCompany.GetList().DefaultView;
CompaniesDataList.DataSource = pg;
CompaniesDataList.DataBind();
}
private void cmdNext_Click(object sender, System.EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;
// Reload control
PagedDataSource pg = new PagedDataSource();
pg.DataSource = HRCompany.GetList().DefaultView;
CompaniesDataList.DataSource = pg;
CompaniesDataList.DataBind();
}
但我没有看到下一页上一个或下一个按钮点击。另外,如果用户单击"查看全部",如何显示基于记录的页码并显示所有记录。另外,是否需要更改存储过程以允许分页?
我的存储过程是这样的:
ALTER PROCEDURE [dbo].[hr_Companies_GetByIDAndName]
@CompanyID INT,
@CompanyName VARCHAR(100),
@Lang int = 1
AS
BEGIN
SET NOCOUNT ON;
SELECT C.*
FROM dbo.hr_Companies C
WHERE C.Deleted = 0
AND C.CompanyID = @CompanyID
AND @CompanyName = CASE WHEN @Lang = 1 then NameLang1 ELSE NameLang2 END
END
请建议。
兄弟,
你有趣的问题迫使我在工作时编译和解决你的代码问题:)解决方案如下:
PagedDataSource pg = null;
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
pg = new PagedDataSource();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pg.DataSource = dt.DefaultView;
pg.AllowPaging = true;
pg.PageSize = 2;
dlPagedControl.DataSource = pg;
Session["DataTable"] = dt; //dt is just a datatable replace it with whatever you need
Session["CurrentPage"] = pg.CurrentPageIndex;
dlPagedControl.DataBind();
}
}
protected void cmdPrev_Click(object sender, EventArgs e)
{
if(int.Parse(Session["CurrentPage"].ToString())!=0)
{
if (pg == null)
{
pg = new PagedDataSource();
pg.PageSize = 2;
pg.AllowPaging = true;
}
pg.DataSource = (Session["DataTable"] as DataTable).DefaultView;
pg.CurrentPageIndex = (int.Parse(Session["CurrentPage"].ToString())) - 1;
dlPagedControl.DataSource = pg;
dlPagedControl.DataBind();
Session["CurrentPage"] = pg.CurrentPageIndex;
lable1.Text=pg.CurrentPageIndex;
}
}
protected void cmdNext_Click_Click(object sender, EventArgs e)
{
if (pg == null)
{
pg = new PagedDataSource();
pg.PageSize = 2;
pg.AllowPaging = true;
}
pg.DataSource = (Session["DataTable"] as DataTable).DefaultView;
pg.CurrentPageIndex = (int.Parse(Session["CurrentPage"].ToString())) + 1;
dlPagedControl.DataSource = pg;
dlPagedControl.DataBind();
Session["CurrentPage"] = pg.CurrentPageIndex;
//show Current Page in Label
lable1.Text=pg.CurrentPageIndex;
}