using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;
/// <summary>
/// This custom control will create a gridview with a floating header and custom paging to only return the number of rows per page.
/// </summary>
namespace Controls
{
public class ScrollPageGridView : System.Web.UI.WebControls.GridView
{
#region Properties
private Int32 pagesPerGrouping = 10;
public String PagerBackColor { get; set; }
public Unit GridHeight { get; set; }
public Unit GridWidth { get; set; }
public Int32 TotalRows { get; set; }
public Int32 PagesPerGrouping
{
get
{
return pagesPerGrouping;
}
set
{
Int32 ret = 10;
Int32.TryParse(value.ToString(), out ret);
pagesPerGrouping = ret;
}
}
#endregion
public event EventHandler<EventArgs> GridViewPageChangedCustom;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
HtmlTableCell th = GeneratePaging("th");
}
#region ScrollPageGridView()
public ScrollPageGridView() { }
#endregion
#region CalculateWidth()
private String CalculateWidth()
{
string strWidth = "auto";
if (!this.GridWidth.IsEmpty)
{
strWidth = String.Format("{0}{1}", this.GridWidth.Value, ((this.GridWidth.Type == UnitType.Percentage) ? "%" : "px"));
}
return strWidth;
}
#endregion
#region CalculateHeight()
private String CalculateHeight()
{
string strHeight = "200px";
if (!this.GridHeight.IsEmpty)
{
strHeight = String.Format("{0}{1}", this.GridHeight.Value, ((this.GridHeight.Type == UnitType.Percentage) ? "%" : "px"));
}
return strHeight;
}
#endregion
#region Render
protected override void Render(HtmlTextWriter writer)
{
// Render header row.
if (this.HeaderRow != null)
{
GridViewRow customHeader = this.HeaderRow;
writer.Write("<table border=\"1\" rules=\"all\" cellspacing=\"" + this.CellSpacing.ToString() + "\" cellpadding=\"" + this.CellPadding.ToString() + "\" style=\"border-collapse:collapse;\">");
customHeader.ApplyStyle(this.HeaderStyle);
if (AutoGenerateColumns == false)
{
// Remove the existing cells from the Header.
for (int j = customHeader.Cells.Count - 1; j >= 0; j--)
{
if (this.Columns[j].Visible == false)
customHeader.Cells.RemoveAt(j);
}
Int32 i = 0;
foreach (DataControlField col in this.Columns)
{
if (col.Visible == true)
{
customHeader.Cells[i].ApplyStyle(col.HeaderStyle);
i++;
}
}
}
customHeader.RenderControl(writer);
writer.Write("</table>");
this.HeaderRow.Visible = false;
}
// Render data rows.
// Start the Div tag for the scrolling gridview.
writer.Write("<div id=\"div" + this.ID + "\" style=\"height:" + CalculateHeight() + ";overflow:auto;width:" + CalculateWidth() + ";\">");
// Get the pager row and make invisible.
GridViewRow customPager = this.BottomPagerRow;
if (this.BottomPagerRow != null)
{
this.BottomPagerRow.Visible = false;
}
base.Render(writer);
writer.Write("</div>");
// Render pager row.
if (customPager != null)
{
writer.Write("<table border='0' cellspacing='" + this.CellSpacing.ToString() + "' cellpadding='" + this.CellPadding.ToString() + "' style='width:" + customPager.Width.ToString() + "'>");
customPager.ApplyStyle(this.PagerStyle);
customPager.Visible = true;
customPager.RenderControl(writer);
writer.Write("</table>");
writer.Write("<table border=\"1\" rules=\"all\" cellspacing=\"" + this.CellSpacing.ToString() + "\" cellpadding=\"" + this.CellPadding.ToString() + "\" style=\"border-collapse:collapse;\">");
writer.Write("<tr style=\"background-color:" + this.PagerBackColor + "\">");
HtmlTableCell th = GeneratePaging("th");
th.RenderControl(writer);
writer.Write("</tr>");
writer.Write("</table>");
}
}
#endregion
#region GeneratePaging()
/// <summary>
/// Generate the page numbers on the grid.
/// </summary>
private HtmlTableCell GeneratePaging(String TableCellType)
{
try
{
HtmlTableCell th = new HtmlTableCell(TableCellType);
th.Attributes.Add("scope", "col");
th.Attributes.Add("style", "width:400px;text-align:left;");
//int pagesPerGrouping = PagesPerGrouping;
int fontSize = 10;
int totRows = 0;
int numPages = 1;
int gridActSelectedPage = 1;
int curPageGroup = 1;
int startPageForGroup;
int endPageForGroup;
string litSep = " ";
// Get the total number of rows that were returned from the DB.
totRows = this.TotalRows;
// Get the current page index.
if (this.ViewState["gv" + this.ID + "PageIndex"] != null)
gridActSelectedPage = Convert.ToInt32(this.ViewState["gv" + this.ID + "PageIndex"]) + 1; // Actual page index, page index is 0 based;
// Clear out any existing controls.
th.Controls.Clear();
// Based on the number of rows in the DataView and the number of items per page in the grid figure out the number of pages needed.
numPages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totRows) / Convert.ToDouble(this.PageSize)));
// Divide the current page by the number of pages per grouping to find out which grouping we're in.
// Subtract 1 from the current page grouping, multiply by the pages per grouping and then add 1 to get the starting page for the group.
curPageGroup = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(gridActSelectedPage) / Convert.ToDecimal(pagesPerGrouping)));
startPageForGroup = ((curPageGroup - 1) * pagesPerGrouping) + 1;
// Get the last page in the page group.
endPageForGroup = (startPageForGroup + pagesPerGrouping - 1 < numPages ? startPageForGroup + pagesPerGrouping - 1 : numPages);
// Add page nav buttons when there are more pages then what can show on the screen.
if (startPageForGroup != 1)
{
LinkButton lnk = new LinkButton();
lnk.ID = "lbl" + this.ID + "Pg" + (startPageForGroup - 1).ToString();
lnk.Text = "<<";
lnk.CommandArgument = (startPageForGroup - 1).ToString();
lnk.Font.Size = FontUnit.Point(fontSize);
lnk.Click += new EventHandler(gvPage_Click);
th.Controls.Add(lnk);
// Add a separator between page numbers.
Literal litSeparator = new Literal();
litSeparator.Text = litSep;
litSeparator.ID = "litSeparatorLT";
th.Controls.Add(litSeparator);
}
// Loop through the active page set and display the numbers across the footer.
for (int i = startPageForGroup; i <= endPageForGroup; i++)
{
// For the active page disable the link.
LinkButton lnk = new LinkButton();
lnk.ID = "lbl" + this.ID + "Pg" + i.ToString();
lnk.Text = i.ToString();
lnk.CommandArgument = i.ToString();
lnk.Font.Size = FontUnit.Point(fontSize);
if (i == gridActSelectedPage)
{
lnk.Enabled = false;
}
else
{
lnk.Click += new EventHandler(gvPage_Click);
lnk.Command += new CommandEventHandler(gvPage_Command);
}
th.Controls.Add(lnk);
// Add a separator between page numbers.
Literal litSeparator = new Literal();
litSeparator.Text = litSep;
litSeparator.ID = "litSeparator" + i.ToString();
th.Controls.Add(litSeparator);
}
// If the number of total pages is more than the grouping then put in >> for navigation.
if (numPages > endPageForGroup)
{
LinkButton lnk = new LinkButton();
lnk.ID = "lbl" + this.ID + "Pg" + (endPageForGroup + 1).ToString();
lnk.Text = ">>";
lnk.CommandArgument = (endPageForGroup + 1).ToString();
lnk.Font.Size = FontUnit.Point(fontSize);
lnk.Click += new EventHandler(gvPage_Click);
th.Controls.Add(lnk);
}
return th;
}
catch (Exception)
{
throw;
}
}
#endregion
#region gvPage_Click
private void gvPage_Click(object sender, EventArgs e)
{
if (GridViewPageChangedCustom != null)
{
GridViewPageChangedCustom(this, new EventArgs());
}
}
#endregion
}
}
|