Web サイトのページネーションを作成しようとしています。ただし、サーバーに次のページに移動するように通知するボタンをクリックするたびに、サーバーは同じページをロードしますが (たとえば、ページ 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"];
}