Hi guys .. I am trying to do the following I have a dynamic table in my asp page and I want to show the table in multi pages like google results any useful suggestion please note: I don't to use gridview so any another way ??
You can use the jquery tablesorter plugin and the jquery.tablesorter.pager plugin to accomplish this. It will work on a standard html table, and has a lot of options such as alternate row hilighting, sorting, etc.
It depends on how many records at max will be returned. The most efficient way would be
- Implement Pagination numbers as links which differ in query parameter.
Based on the query parameter, in the server side, fetch the records corresponding to that page alone. eg. if your page size is 10 and the current page is 2 , fetch only 11,20 records from the server and bind it to html table.
If you are using LINQ to SQL , use Take and SKIP methods as in http://blog.ofiryaron.com/blog/post/Pagination-on-Linq-to-SQL-childs-play!.aspx
If you are using direct SQL queries or Stored procs , make use of RowID to fetch that pages records.
If you want more flexibility in the output than GridView provides, take a look at Repeater.
Since the Repeater doesn't directly implement paging, you'll have to supply your own Next and Previous buttons. As noted by Sundararajan S if you have many records you'll want to use the current page number and page size to return only the current page's records to the browser rather than all of them.
Below is an example (I didn't know what your data source would be like, so I just used a list as an example. Substitute with something more appropriate.)
Hope that helps.
Default.aspx:
<asp:Button ID="PrevPageButton" runat="server" Text="Prev"
onclick="PrevPageButton_Click" />
<asp:Label ID="CurrentPageLabel" runat="server" />
<asp:Button ID="NextPageButton" runat="server" Text="Next"
onclick="NextPageButton_Click" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<h2> <%# Eval("Name") %> </h2>
<p>
<%# Eval("Description") %>
</p>
</ItemTemplate>
</asp:Repeater>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RepeaterPaging
{
public partial class _Default : System.Web.UI.Page
{
private const int PageSize = 10;
private const int MaxPage = 4;
public int CurrPage
{
get
{
if (this.ViewState["CurrPage"] == null )
this.ViewState["CurrPage"] = 0;
return (int) this.ViewState["CurrPage"];
}
set { this.ViewState["CurrPage"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindItems();
}
}
protected void BindItems()
{
int currPage = CurrPage;
List<Book> books = new List<Book>();
int startItem = (currPage * PageSize) + 1;
for (int i = startItem; i < startItem+PageSize; i++)
{
books.Add(new Book("Title " + i, "Description " + i + " ..................."));
}
Repeater1.DataSource = books;
Repeater1.DataBind();
CurrentPageLabel.Text =
string.Format(" Page {0} of {1} ", CurrPage + 1, MaxPage + 1);
}
protected void NextPageButton_Click(object sender, EventArgs e)
{
if (CurrPage < MaxPage)
{
CurrPage++;
BindItems();
}
}
protected void PrevPageButton_Click(object sender, EventArgs e)
{
if (CurrPage > 0)
{
CurrPage--;
BindItems();
}
}
}
public class Book
{
public string Name { get; set; }
public string Description { get; set; }
public Book(string name, string desc)
{
Name = name;
Description = desc;
}
}
}