我正在尝试为网站创建分页。 但是,每当我单击指示服务器转到下一页的按钮时,它仍将加载相同的页面(例如,从第1页到第2页),但它从第2页开始正常工作,显示错误的页码页面标签(例如pagelabel = 3,currentpage = 2)。 还有一个问题是没有到达最后一页,因为页面似乎没有从第1页开始,但是当我继续点击page_back_button时,它将继续显示最后一页。 我在找到这个问题的根源时遇到了麻烦。这是我创建的源代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadPageSizer();
LeadStatusDDL();
Session["curr_page"] = 1;
}
rptLeadsPager.DataSource = ListLeads(ddlLStatus.Text.ToString(), Convert.ToInt16(Session["curr_page"]), Convert.ToInt16(ddlPageSize.Text));
rptLeadsPager.DataBind();
BuildPagination(Convert.ToInt16(Session["total_page"]), Convert.ToInt16(Session["curr_page"]), 10);
}
#region DDL List for Results/Page
private void LoadPageSizer()
{
ddlPageSize.Items.Add("10");
ddlPageSize.Items.Add("15");
ddlPageSize.Items.Add("20");
ddlPageSize.Items.Add("25");
ddlPageSize.Items.Add("30");
ddlPageSize.Items.Add("40");
ddlPageSize.Items.Add("50");
}
#endregion
#region DDL List for LeadsStatus
private void LeadStatusDDL()
{
ddlLStatus.Items.Add(new ListItem("Not Qualified"));
ddlLStatus.Items.Add(new ListItem("Qualified"));
}
#endregion
#region SQL Stored Procedure
private DataTable ListLeads(string leadStatus, int pageNumber, int pageSize)
{
int searchResultsCount;
DataSet dsSearchResults;
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["sqlConnection"]))
{
using (var sqlCommand = new SqlCommand("LeadsPager", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
//add parameters
sqlCommand.Parameters.AddWithValue("@LeadsStatus", leadStatus);
sqlCommand.Parameters.AddWithValue("@PageNumber", pageNumber);
sqlCommand.Parameters.AddWithValue("@ResultsPerPage", pageSize);
var resultsCountParam = new SqlParameter("@SearchResultsCount", SqlDbType.Int);
resultsCountParam.Direction = ParameterDirection.Output;
sqlCommand.Parameters.Add(resultsCountParam);
using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
dsSearchResults = new DataSet();
sqlDataAdapter.Fill(dsSearchResults);
searchResultsCount = int.Parse(resultsCountParam.Value.ToString());
}
}
}
if (searchResultsCount == 0)
{
ResultHeader.Visible = false;
ResultFooter.Visible = false;
}
else
{
Session["total_page"] = GetTotalPage(pageSize, searchResultsCount);
lblTotalPage.Text = Session["total_page"].ToString();
ResultHeader.Visible = true;
ResultFooter.Visible = true;
}
RenderToolbar();
if (dsSearchResults.Tables.Count > 0)
return dsSearchResults.Tables[0];
else
return null;
}
#endregion
#region Header Function
private void RenderToolbar()
{
int intCurPage = Session["curr_page"] == null ? 1 : Convert.ToInt16(Session["curr_page"]);
int intTotalPage = Session["total_page"] == null ? 1 : Convert.ToInt16(Session["total_page"]);
btnUpPageBack.Visible = intCurPage > 1;
btnUpPageNext.Visible = intTotalPage > intCurPage;
lblPage.Text = Session["curr_page"] == null ? "1" : Session["curr_page"].ToString();
}
#endregion
#region Pagination
protected void BuildPagination(int PageCount, int CurrentPageIndex, int ButtonsCount)
{
pnlPager.Controls.Clear(); //
if (PageCount <= 1) return; // at least two pages should be there to show the pagination
//finding the first linkbutton to be shown in the current display
int start = CurrentPageIndex - (CurrentPageIndex % ButtonsCount);
//finding the last linkbutton to be shown in the current display
int end = CurrentPageIndex + (ButtonsCount - (CurrentPageIndex % ButtonsCount));
//if the start button is more than the number of buttons. If the start button is 11 we have to show the <<First link
if (start > ButtonsCount - 1)
{
pnlPager.Controls.Add(createButton("<<", 0));
pnlPager.Controls.Add(createButton("..", start - 1));
}
int i = 0, j = 0;
for (i = start; i < end; i++)
{
//LinkButton lnk;
if (i < PageCount)
{
if (i + 1 == CurrentPageIndex) //if its the current page
{
Label lbl = new Label();
lbl.Text = (i + 1).ToString();
pnlPager.Controls.Add(lbl);
}
else
{
pnlPager.Controls.Add(createButton((i+1).ToString(), i));
}
}
j++;
}
//If the total number of pages are greaer than the end page we have to show Last>> link
if (PageCount > end)
{
pnlPager.Controls.Add(createButton("..", i));
pnlPager.Controls.Add(createButton(">>", PageCount - 1));
}
}
private LinkButton createButton(string title, int index)
{
LinkButton lnk = new LinkButton();
lnk.ID = "btnPage" + title + "_" + index.ToString();
lnk.Text = title;
lnk.CommandArgument = index.ToString();
lnk.Click += new EventHandler(lnkPager_Click);
return lnk;
}
#endregion Pagination
#region Get Total Page Function
private static string GetTotalPage(int vPageDisplay, int vTotalData)
{
decimal dlQuot = decimal.Divide(vTotalData, vPageDisplay);
int dlPresQuot = vTotalData / vPageDisplay;
return (dlQuot > dlPresQuot ? dlPresQuot + 1 : dlPresQuot).ToString();
}
#endregion
#region Navigating Page with Buttons Function
protected void NextPage_Click(object sender, EventArgs e)
{
Session["curr_page"] = Convert.ToInt16(Session["curr_page"]) + 1;
//Converts Results/Page to INT
ListLeads(ddlLStatus.Text.ToString(), Convert.ToInt16(Session["curr_page"]), Convert.ToInt16(ddlPageSize.Text));
}
protected void BackPage_Click(object sender, EventArgs e)
{
Session["curr_page"] = Convert.ToInt16(Session["curr_page"]) - 1;
ListLeads(ddlLStatus.Text.ToString(), Convert.ToInt16(Session["curr_page"]), Convert.ToInt16(ddlPageSize.Text));
}
protected void lnkPager_Click(object sender, EventArgs e) //Page index changed function
{
LinkButton lnk = (LinkButton)sender;
Session["curr_page"] = int.Parse(lnk.CommandArgument) + 1;
ListLeads(ddlLStatus.Text.ToString(), Convert.ToInt16(Session["curr_page"]), Convert.ToInt16(ddlPageSize.Text));
}
#endregion
更新<!/强> 我已经解决了链接按钮的问题。
但是,有一个新问题。每当我在特定页面(例如,页码6)中并且我从每页结果下拉列表中选择(例如从20到50)时,总页面将从(例如9到3)改变。
然而!我当前所在的特定页面(页面6)超出了总页数的范围(例如,总页数= 3,我的当前页面= 6)。然后它将不显示任何内容,我必须向后单击按钮才能到达新的每页结果列表的最后一页。
我已经创建了一个代码,用于指定发生这种情况时Session [“curr_page”]的位置。但是,我无法确定将此代码放在何处,因为每当我更改每页结果下拉列表时它都不会触发。这是我制作的代码。
if (Convert.ToInt16(Session["curr_page"]) > Convert.ToInt16(Session["total_page"]))
{
Session["curr_page"] = Session["total_page"];
}
答案 0 :(得分:2)
问题是您没有重新绑定数据。他们第一次点击您的页面Session["curr_page"] = 1;
时,您构建并设置了数据源,然后将其绑定。当用户单击“下一步”按钮,页面重新加载并且在Page_Load
上,Session["curr_page"]
仍然等于1时,您构建并设置数据源,然后将其绑定(因此,与初始页面加载相同的结果)
然后NextPage_Click
执行,你增加Session["curr_page"]
,并重建你的数据源......但是你永远不会设置和绑定它。
所以你可以这样做:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadPageSizer();
LeadStatusDDL();
Session["curr_page"] = 1;
BindAndPage();
}
}
private void BindAndPage()
{
rptLeadsPager.DataSource = ListLeads(ddlLStatus.Text.ToString(), Convert.ToInt16(Session["curr_page"]), Convert.ToInt16(ddlPageSize.Text));
rptLeadsPager.DataBind();
BuildPagination(Convert.ToInt16(Session["total_page"]), Convert.ToInt16(Session["curr_page"]), 10);
}
//other code
protected void NextPage_Click(object sender, EventArgs e)
{
Session["curr_page"] = Convert.ToInt16(Session["curr_page"]) + 1;
BindAndPage();
}
答案 1 :(得分:1)
您可以使用“DataTable”javascript功能来解决此问题。请参阅https://www.datatables.net/。
$(document).ready(function(){
$('#myTable').DataTable();
});
仅在此设置您的表格ID,它会自动设置您的表格的分页,排序和过滤。
由于