1

特定のページ (例: ページ番号 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"];
    }

サンプルソースコードはこちら。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadPageSizer();
        LeadStatusDDL();
        Session["curr_page"] = 1;
        BindAndPage();
    }
    OnPageLoad();
}

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);
    }
private void OnPageLoad()
    {
        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 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

ありがとうございました。

4

0 に答える 0