Crew .NET |
|
cloudstrife schreef: Je gaat dus de beperking via de query doen. Bij een mysql database kan je limit gebruiken in je select. Het is niet nodig dat je gaat limiteren op je query zelf. Zelfs op een Repeater kan je Paging toepassen dankzij de PagedDataSource! Plaats volgende ASP.NET opmaak onder je Repeater in je aspx-pagina:
<asp:LinkButton ID="lnkPrev" runat="server" Text="<<" OnClick="lnkPrev_Click" />
<asp:Label ID="lblPages" runat="server" />
<asp:LinkButton ID="lnkNext" runat="server" Text=">>" OnClick="lnkNext_Click" />
<asp:LinkButton ID="lnkPrev" runat="server" Text="<<" OnClick="lnkPrev_Click" /> <asp:Label ID="lblPages" runat="server" /> <asp:LinkButton ID="lnkNext" runat="server" Text=">>" OnClick="lnkNext_Click" />
De C# die alles doet werken is dit:
public int Current
{
get
{
object o = this.ViewState["_Pagina"];
return o == null ? 0 : (int)o;
}
set { this.ViewState["_Pagina"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
protected void lnkPrev_Click(object sender, EventArgs e)
{
Current--;
Bind();
}
protected void lnkNext_Click(object sender, EventArgs e)
{
Current++;
Bind();
}
private void Bind()
{
PagedDataSource pds = new PagedDataSource();
String sqlString = "SELECT Naam, Bericht, Datum FROM TblGuestBook ORDER BY Id DESC";
DataTable tempTable = new DataTable();
try
{
SqlDataAdapter adaptData = new SqlDataAdapter(sqlString, sqlConn);
adaptData.Fill(tempTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
pds.DataSource = tempTable.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 5;
pds.CurrentPageIndex = Current;
lblPages.Text = (Current + 1) + "/" + pds.PageCount;
lnkPrev.Enabled = !pds.IsFirstPage;
lnkNext.Enabled = !pds.IsLastPage;
repeatItems.DataSource = pds;
repeatItems.DataBind();
}
public int Current { get { object o = this.ViewState["_Pagina"]; return o == null ? 0 : (int)o; } set { this.ViewState["_Pagina"] = value; } } protected void Page_Load(object sender, EventArgs e) { Bind(); } protected void lnkPrev_Click(object sender, EventArgs e) { Current--; Bind(); } protected void lnkNext_Click(object sender, EventArgs e) { Current++; Bind(); } private void Bind() { PagedDataSource pds = new PagedDataSource (); String sqlString = "SELECT Naam, Bericht, Datum FROM TblGuestBook ORDER BY Id DESC"; DataTable tempTable = new DataTable (); try { SqlDataAdapter adaptData = new SqlDataAdapter (sqlString, sqlConn ); adaptData.Fill(tempTable); } catch (Exception ex) { throw new Exception (ex. Message); } pds.DataSource = tempTable.DefaultView; pds.AllowPaging = true; pds.PageSize = 5; pds.CurrentPageIndex = Current; lblPages.Text = (Current + 1) + "/" + pds.PageCount; lnkPrev.Enabled = !pds.IsFirstPage; lnkNext.Enabled = !pds.IsLastPage; repeatItems.DataSource = pds; repeatItems.DataBind(); }
Voorzie wel volgende libraries in je code:
using System.Data;
using System.Data.SqlClient;
using System.Data; using System.Data.SqlClient;
|