C# 每页结果
每当我在特定页面(例如第6页)中,并从每页结果下拉列表(例如从20到50)中选择时,总页面将从第9页更改为第3页 然而!我当前在第6页上的特定页面超出了总页面的范围,例如,总页面=3,我的当前页面=6。然后它将不显示任何内容,我必须向后单击按钮才能到达每页新结果列表的最后一页 我创建了一个代码来指定每次发生这种情况时会话[curr_page]应该在哪里。但是,我无法确定将此代码放在何处,因为每当我更改每页结果下拉列表时,它不会触发。这是我编写的代码C# 每页结果,c#,pagination,webforms,C#,Pagination,Webforms,每当我在特定页面(例如第6页)中,并从每页结果下拉列表(例如从20到50)中选择时,总页面将从第9页更改为第3页 然而!我当前在第6页上的特定页面超出了总页面的范围,例如,总页面=3,我的当前页面=6。然后它将不显示任何内容,我必须向后单击按钮才能到达每页新结果列表的最后一页 我创建了一个代码来指定每次发生这种情况时会话[curr_page]应该在哪里。但是,我无法确定将此代码放在何处,因为每当我更改每页结果下拉列表时,它不会触发。这是我编写的代码 if (Convert.ToInt16(Ses
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
谢谢。尝试将其放在页面加载事件中的OnPageLoad之前。谢谢,但不幸的是,它无法正常工作。在设置会话[total_page]=GetTotalPagepageSize、searchResultsCount之后,将其放入ListLeads方法中;