Gridview Peging Example
public void ApplyPagingStyle(GridView GridView1)
{
GridViewRow row = GridView1.BottomPagerRow;
if (row != null)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
ul.Attributes.Add("class", "pagination clearfix");
PlaceHolder ph;
LinkButton lnkPaging;
LinkButton btnFirstPage;
LinkButton btnPrevPage;
LinkButton btnNextPage;
LinkButton btnLastPage;
Label lblpagesize;
DropDownList ddlPageSize;
Label lblsizedetails;
HtmlGenericControl li1 = new HtmlGenericControl("li");
btnFirstPage = new LinkButton();
btnFirstPage.CssClass = "first";
btnFirstPage.CommandName = "Page";
btnFirstPage.CommandArgument = "first";
li1.Controls.Add(btnFirstPage);
HtmlGenericControl li2 = new HtmlGenericControl("li");
btnPrevPage = new LinkButton();
btnPrevPage.CssClass = "previous";
btnPrevPage.CommandName = "Page";
btnPrevPage.CommandArgument = "prev";
li2.Controls.Add(btnPrevPage);
ul.Controls.Add(li1);
ul.Controls.Add(li2);
if (GridView1.PageIndex == 0)
{
btnFirstPage.Enabled = false;
btnPrevPage.Enabled = false;
}
HtmlGenericControl li;
for (int i = 1; i <= GridView1.PageCount; i++)
{
li = new HtmlGenericControl("li");
lnkPaging = new LinkButton();
lnkPaging.Text = i.ToString();
lnkPaging.CommandName = "Page";
lnkPaging.Height = Unit.Pixel(15);
lnkPaging.CommandArgument = i.ToString();
if (i == GridView1.PageIndex + 1)
lnkPaging.CssClass = "active";
li.Controls.Add(lnkPaging);
ul.Controls.Add(li);
}
HtmlGenericControl li3 = new HtmlGenericControl("li");
btnNextPage = new LinkButton();
btnNextPage.CssClass = "next";
btnNextPage.CommandName = "Page";
btnNextPage.CommandArgument = "next";
li3.Controls.Add(btnNextPage);
ul.Controls.Add(li3);
HtmlGenericControl li4 = new HtmlGenericControl("li");
btnLastPage = new LinkButton();
btnLastPage.CssClass = "last";
btnLastPage.CommandName = "Page";
btnLastPage.CommandArgument = "last";
li4.Controls.Add(btnLastPage);
ul.Controls.Add(li4);
ph = (PlaceHolder)row.FindControl("ph");
ph.Controls.Add(ul);
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
btnNextPage.Enabled = false;
btnLastPage.Enabled = false;
}
//ph.Controls.Add(new LiteralControl("<br/>"));
lblpagesize = new Label();
lblpagesize.Text = "Page size:";
//lblpagesize.Style["text-align"] = "right";
ph.Controls.Add(lblpagesize);
ddlPageSize = new DropDownList();
ddlPageSize.Items.Add(new ListItem("1", "1"));
ddlPageSize.Items.Add(new ListItem("5", "5"));
ddlPageSize.Items.Add(new ListItem("10", "10"));
ddlPageSize.AutoPostBack = true;
ddlPageSize.Style["text-align"] = "right";
ddlPageSize.SelectedIndexChanged += new EventHandler(PageSize_SelectedIndexChanged);
ph.Controls.Add(ddlPageSize);
lblsizedetails = new Label();
int rcount = ((DataTable)GridView1.DataSource).Rows.Count;
lblsizedetails.Text = rcount.ToString() + " items in " + GridView1.PageCount + " pages";
//lblsizedetails.Style["text-align"] = "left";
ph.Controls.Add(lblsizedetails);
ddlPageSize.SelectedValue = GridView1.PageSize.ToString();
}
else
{
}
}
Preview
{
GridViewRow row = GridView1.BottomPagerRow;
if (row != null)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
ul.Attributes.Add("class", "pagination clearfix");
PlaceHolder ph;
LinkButton lnkPaging;
LinkButton btnFirstPage;
LinkButton btnPrevPage;
LinkButton btnNextPage;
LinkButton btnLastPage;
Label lblpagesize;
DropDownList ddlPageSize;
Label lblsizedetails;
HtmlGenericControl li1 = new HtmlGenericControl("li");
btnFirstPage = new LinkButton();
btnFirstPage.CssClass = "first";
btnFirstPage.CommandName = "Page";
btnFirstPage.CommandArgument = "first";
li1.Controls.Add(btnFirstPage);
HtmlGenericControl li2 = new HtmlGenericControl("li");
btnPrevPage = new LinkButton();
btnPrevPage.CssClass = "previous";
btnPrevPage.CommandName = "Page";
btnPrevPage.CommandArgument = "prev";
li2.Controls.Add(btnPrevPage);
ul.Controls.Add(li1);
ul.Controls.Add(li2);
if (GridView1.PageIndex == 0)
{
btnFirstPage.Enabled = false;
btnPrevPage.Enabled = false;
}
HtmlGenericControl li;
for (int i = 1; i <= GridView1.PageCount; i++)
{
li = new HtmlGenericControl("li");
lnkPaging = new LinkButton();
lnkPaging.Text = i.ToString();
lnkPaging.CommandName = "Page";
lnkPaging.Height = Unit.Pixel(15);
lnkPaging.CommandArgument = i.ToString();
if (i == GridView1.PageIndex + 1)
lnkPaging.CssClass = "active";
li.Controls.Add(lnkPaging);
ul.Controls.Add(li);
}
HtmlGenericControl li3 = new HtmlGenericControl("li");
btnNextPage = new LinkButton();
btnNextPage.CssClass = "next";
btnNextPage.CommandName = "Page";
btnNextPage.CommandArgument = "next";
li3.Controls.Add(btnNextPage);
ul.Controls.Add(li3);
HtmlGenericControl li4 = new HtmlGenericControl("li");
btnLastPage = new LinkButton();
btnLastPage.CssClass = "last";
btnLastPage.CommandName = "Page";
btnLastPage.CommandArgument = "last";
li4.Controls.Add(btnLastPage);
ul.Controls.Add(li4);
ph = (PlaceHolder)row.FindControl("ph");
ph.Controls.Add(ul);
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
btnNextPage.Enabled = false;
btnLastPage.Enabled = false;
}
//ph.Controls.Add(new LiteralControl("<br/>"));
lblpagesize = new Label();
lblpagesize.Text = "Page size:";
//lblpagesize.Style["text-align"] = "right";
ph.Controls.Add(lblpagesize);
ddlPageSize = new DropDownList();
ddlPageSize.Items.Add(new ListItem("1", "1"));
ddlPageSize.Items.Add(new ListItem("5", "5"));
ddlPageSize.Items.Add(new ListItem("10", "10"));
ddlPageSize.AutoPostBack = true;
ddlPageSize.Style["text-align"] = "right";
ddlPageSize.SelectedIndexChanged += new EventHandler(PageSize_SelectedIndexChanged);
ph.Controls.Add(ddlPageSize);
lblsizedetails = new Label();
int rcount = ((DataTable)GridView1.DataSource).Rows.Count;
lblsizedetails.Text = rcount.ToString() + " items in " + GridView1.PageCount + " pages";
//lblsizedetails.Style["text-align"] = "left";
ph.Controls.Add(lblsizedetails);
ddlPageSize.SelectedValue = GridView1.PageSize.ToString();
}
else
{
}
}
Preview
Comments