Il problema si presenta quando apri una pagina con un solo record. Riempie il NavMenu con tre collegamenti; "Primo", "1" e "Ultimo".Paging in ASP.NET; il numero di pagine non cambia mai dopo il filtro
Per qualche motivo, quando si esegue una query di ricerca che restituisce più di una pagina, visualizza sempre solo "Primo", "1" e "Ultimo". Allo stesso modo, se inizi con quattro pagine e la query di ricerca successiva restituisce solo due record, mostra comunque "Primo", "1", "2", "3", "4" e "Ultimo". Quindi, per qualche motivo, comunque molte pagine con cui inizi, otterrai sempre. Come si può resettare il contatore/display della pagina?
Ecco la mia C# code-behind:
public void RunTheSearch()
{
//Run the Stored Procedure first
SqlConnection connection2 = new SqlConnection(strCon1);
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "sp_Search";
cmd2.Connection = connection2;
//--- A bunch of code that returns a dataset. Lengthy and unnecessary to my issue
connection2.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd2);
DataSet ds = new DataSet();
adp.Fill(ds, "OLDPages");
//Pagination code so only a set number of records loads at a time.
// Done to speed up the loading, since this list gets really long.
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["OLDPages"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
//NavMenu.Items.Clear();
int currentPage;
if (Request.QueryString["page"] != null)
{
currentPage = Int32.Parse(Request.QueryString["page"]);
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
//Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
if (!pds.IsFirstPage)
{
MenuItem itemMessage = NavMenu.FindItem("First");
itemMessage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
}
AcctRepeater.DataSource = pds;
AcctRepeater.DataBind();
CreatePagingControl(pds.PageCount, pds.CurrentPageIndex);
// End of Pagination code
connection2.Close();
}
private void CreatePagingControl(int PCount, int PIndex)
{
int PIndex2 = 0;
int SCounter = PIndex + 1;
int RowCount = PCount;
//Allow the pagination menu to always start 5 less than the current page you're on
if (PIndex < 5)
{
PIndex2 = 0;
}
else
{
PIndex2 = PIndex - 5;
}
// Show 10 total page numbers. You can increase or shrink that range by changing the 10 to whatever number you want
for (int i = PIndex2; i < PIndex2 + 10 && i < PCount; i++)
{
NavMenu.Items.Add(new MenuItem
{
Text = (i + 1).ToString(),
NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (i + 1).ToString()
});
// Now determine the selected item so the proper CSS can be applied
foreach (MenuItem item in NavMenu.Items)
{
item.Selected = item.Text.Equals(SCounter.ToString());
}
}
NavMenu.Items.Add(new MenuItem
{
Text = "Last",
NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (PCount)
});
}
E sulla pagina aspx:
<asp:Menu ID="NavMenu" runat="server" CssClass="menu"
IncludeStyleBlock="false" Orientation="Horizontal" width="703px"
BackColor="#CC3300" EnableViewState="true">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="First" Selectable="true" />
</Items>
</asp:Menu>
Ho provato NavMenu.Items.Clear(), ma non ha così perché ha anche eliminato l'elemento codificato sul lato dell'aspx.
Invece di 'Cancella()' puoi usare 'Rimuovi (MenuItem)' per rimuovere selettivamente 'MenuItems'. – ctumturk
Puoi pubblicare il tuo metodo di caricamento della pagina? –