Torna al Thread
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session.Remove("ElencoRicette");
CaricaRicette();
}
Pagine(ElencoRicette.Count);
}
List<tipoRicetta> ElencoRicette
{
set
{
Session["ElencoRicette"] = value;
}
get
{
return List<tipoRicetta>)Session["ElencoRicette"];
}
}
void CaricaRicette()
{
rptRicette.DataSource = null;
rptRicette.DataBind();
PagedDataSource objPage = new PagedDataSource();
List<tipoRicetta> elenco = ElencoRicette;
lbIndietro.Visible = false;
lbAvanti.Visible = false;
if (elenco.Count > 0)
{
objPage.AllowPaging = true;
objPage.DataSource = elenco;
//Setting the Pagesize
objPage.PageSize = elem_pagina;
//"CurrentPage" is public static variable to hold the current page index value declared in the global section.
objPage.CurrentPageIndex = CurrentPage;
lbIndietro.Visible = !objPage.IsFirstPage;
lbAvanti.Visible = !objPage.IsLastPage;
rptRicette.DataSource = objPage;
rptRicette.DataBind();
}
}
protected void Cerca(object sender, EventArgs e)
{
string cerca = txtCerca.Text;
if (!string.IsNullOrEmpty(cerca))
{
CurrentPage = 0;
ElencoRicette = Ricette.Cerca(cerca);
CaricaRicette();
Pagine(ElencoRicette.Count);
}
}
int CurrentPage
{
get
{
return Convert.ToInt32(hdCurrentPage.Value);
}
set
{
hdCurrentPage.Value = value.ToString();
}
}
void Pagine(int n_elem)
{
phPagine.Controls.Clear();
int current = Convert.ToInt32(hdCurrentPage.Value);
int tot_pag = n_elem / elem_pagina;
tot_pag = (n_elem % elem_pagina == 0 ? tot_pag : tot_pag + 1);
int fine = (current + n_pag <= tot_pag ? current + n_pag : tot_pag); //current + n_pag - 1 se la prima pagina è 1
int inizio = (fine - (current + 1) < n_pag - 1 && fine > n_pag ? fine - n_pag + 1 : current + 1);
if (fine < n_pag) inizio = 1;
for (int i = inizio; i <= fine; i++)
{
Button lbPagina = new Button();
lbPagina.Text = i.ToString();
lbPagina.CommandArgument = (i - 1).ToString();
lbPagina.Click += new EventHandler(lbPagina_Click);
lbPagina.Style["text-align"] = "center";
lbPagina.OnClientClick = "setPage('" + (i - 1).ToString() + "');";
lbPagina.CssClass = "n_pag";
if (i - 1 == CurrentPage) lbPagina.CssClass = "n_pag_sel";
//divPagina.Controls.Add(lbPagina);
phPagine.Controls.Add(lbPagina);
}
lbPageBack.Visible = (current - n_pag + 1 > 0 && tot_pag > 0 ? true : false); //current - n_pag se la prima pagina è 1
lbPageForward.Visible = (current + n_pag < tot_pag && tot_pag > 0 ? true : false); //current + n_pag - 1 se la prima pagina è 1
}
//Evento richiamato al click su una pagina
protected void lbPagina_Click(object sender, EventArgs e)
{
Button lbPagina = (Button)sender;
CurrentPage = Convert.ToInt32(lbPagina.CommandArgument);
CaricaRicette();
}