Sunday, November 8, 2009

Gridview Footer Page

#region Namespaces
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
#endregion

/// <summary&rt;
/// Summary description for GridViewPageFooter
/// </summary&rt;
public class GridViewPageFooter
{
public GridViewPageFooter()
{
//
// TODO: Add constructor logic here
//
}
public static void GetGridViewPager(GridView gv, GridViewRow gvr)
{
if ((PlaceHolder)gvr.FindControl("phPager") == null)
{
throw new ApplicationException("A placeholder with an ID=\"phPager\" is required for this to work");
}

PlaceHolder ph = (PlaceHolder)gvr.FindControl("phPager");

// html markup and style
StringBuilder sb = new StringBuilder();
sb.Append("<div style=\"float:left;\"&rt;");
sb.Append(string.Format("<span class=\"pagerCurrentPage\"&rt;Page: {0} of {1}</span&rt;", (gv.PageIndex + 1).ToString(), gv.PageCount));


Literal lit = new Literal();
lit.Text = sb.ToString();
ph.Controls.Add(lit);

//adding the table
//StringBuilder sbTable = new StringBuilder();
//sbTable.Append("width=\"100\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"left\"&rt;");
//sbTable.Append("


// Previous page button
ImageButton btnImage = new ImageButton();
btnImage.ID = "btnPrevPage";
btnImage.CommandName = "Page";
btnImage.CommandArgument = "Prev";
btnImage.ToolTip = "Previous Page";
//btnImage.ImageAlign = ImageAlign.Middle;

if (gv.PageIndex == 0)
{
btnImage.Enabled = false;
btnImage.ImageUrl = "~/Images/prev_icon.jpg";
}
else
{
btnImage.Enabled = true;
btnImage.ImageUrl = "~/Images/prev_icon.jpg";
//btnImage.OnClientClick = "GeneaSys.Common.addProgressText('Previous grid page...');";
btnImage.Click += new ImageClickEventHandler(btn_Click2);
}

ph.Controls.Add(btnImage);

// Pager page links
LinkButton lnb;
int numberOfPageLinks = gv.PagerSettings.PageButtonCount;
int startPage = (((gv.PageIndex) / numberOfPageLinks) * numberOfPageLinks) + 1;
if (startPage &rt; gv.PageCount - numberOfPageLinks && startPage &rt; numberOfPageLinks) startPage = (gv.PageCount - numberOfPageLinks) + 1;
for (int i = startPage; i < numberOfPageLinks + startPage && i <= gv.PageCount; i++)
{
if (i &rt; startPage)
{
lit = new Literal();
lit.Text = " <span class=\"pageSeperator\"&rt;|</span&rt; ";
ph.Controls.Add(lit);
}
else
{
lit = new Literal();
lit.Text = "<span class=\"pagerPageNumbers\"&rt;";
ph.Controls.Add(lit);
}
if (gv.PageIndex + 1 == i)
{
lit = new Literal();
lit.Text = i.ToString();
ph.Controls.Add(lit);
}
else
{
lnb = new LinkButton();
lnb.CommandName = "Page";
lnb.CommandArgument = i.ToString();
lnb.Text = i.ToString();
ph.Controls.Add(lnb);
}
}
lit = new Literal();
lit.Text = "</span&rt;";
ph.Controls.Add(lit);

// Next page button
btnImage = new ImageButton();
btnImage.ID = "btnNextPage";
btnImage.CommandName = "Page";
btnImage.CommandArgument = "Next";
btnImage.ToolTip = "Next Page";
//btnImage.ImageAlign = ImageAlign.Middle;
if (gv.PageIndex == gv.PageCount - 1)
{
btnImage.Enabled = false;
btnImage.ImageUrl = "~/images/next_icon.jpg";
}
else
{
btnImage.Enabled = true;
btnImage.ImageUrl = "~/Images/next_icon.jpg";
btnImage.Click += new ImageClickEventHandler(btn_Click1);
//btnImage.OnClientClick = "GeneaSys.Common.addProgressText('Next grid page...');";
}
ph.Controls.Add(btnImage);


lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

lit = new Literal();
lit.Text = "<span style=\"width:600px;\"&rt; Go to page: ";
ph.Controls.Add(lit);

TextBox tb = new TextBox();
tb.ID = "txtGoToPage";
tb.ToolTip = "Enter the Page Number";
tb.Width = Unit.Pixel(30);
tb.CausesValidation = true;
tb.ValidationGroup = "GoToPage";
ph.Controls.Add(tb);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

ImageButton btn = new ImageButton();
btn.ID = "btnGoToPage";
//btn.Text = "Go";
btn.ImageUrl = "~/Images/go_btn.gif";
btn.CausesValidation = true;
btn.ValidationGroup = "GoToPage";
btn.ToolTip = "Go To Page";
btn.Click += new ImageClickEventHandler(btn_Click);
//btn.OnClientClick = "GeneaSys.Common.addProgressText('Going to grid page...');";
ph.Controls.Add(btn);

RangeValidator rv = new RangeValidator();
rv.ID = "valPager";
rv.Type = ValidationDataType.Integer;
rv.MinimumValue = "1";
rv.MaximumValue = gv.PageCount.ToString();
rv.Text = "not a page";
rv.EnableClientScript = true;
rv.ValidationGroup = "GoToPage";
rv.ControlToValidate = "txtGoToPage";
rv.SetFocusOnError = true;
ph.Controls.Add(rv);
// html markup
lit = new Literal();
lit.Text = "</div&rt;<div style=\"float:right;\"&rt;";
ph.Controls.Add(lit);



lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

RangeValidator rv1 = new RangeValidator();
rv1.ID = "valPager1";
rv1.Type = ValidationDataType.Integer;
rv1.MinimumValue = "1";

rv1.MaximumValue = "100";

rv1.Text = "not a page";
rv1.EnableClientScript = true;
rv1.ValidationGroup = "PageSize";
rv1.ControlToValidate = "txtPageSize";
rv1.SetFocusOnError = true;

ph.Controls.Add(rv1);

lit = new Literal();
lit.Text = "<span style=\" margin: 0pt; width: 200px; padding-left: 35px;\"&rt;Results per page: ";
ph.Controls.Add(lit);


TextBox tb1 = new TextBox();
tb1.ID = "txtPageSize";
tb1.Text = gv.PageSize.ToString();
tb1.ToolTip = "Enter the Page Size";
tb1.Width = Unit.Pixel(30);
tb1.CausesValidation = true;
tb1.ValidationGroup = "PageSize";
ph.Controls.Add(tb1);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt;  </span&rt;";
ph.Controls.Add(lit);

ImageButton btn1 = new ImageButton();
btn1.ID = "btnGoToPageSize";
//btn.Text = "Go";
btn1.ImageUrl = "~/Images/go_btn.gif";
btn1.CausesValidation = true;
btn1.ValidationGroup = "PageSize";
btn1.ToolTip = " Page Size";
btn1.Click += new ImageClickEventHandler(btn2_Click);
//btn.OnClientClick = "GeneaSys.Common.addProgressText('Going to grid page...');";
ph.Controls.Add(btn1);


}

/// <summary&rt;
/// Developer: Mansoor: Purpose : Method used to set a gridview footer with Go to page and results per page feature
/// </summary&rt;
/// <param name="gv"&rt;</param&rt;
/// <param name="gvr"&rt;</param&rt;
/// <param name="GridRecordCount"&rt;</param&rt;
public static void GetGridViewPager(GridView gv, GridViewRow gvr,string GridRecordCount)
{
if ((PlaceHolder)gvr.FindControl("phPager") == null)
{
throw new ApplicationException("A placeholder with an ID=\"phPager\" is required for this to work");
}

PlaceHolder ph = (PlaceHolder)gvr.FindControl("phPager");

// html markup and style
StringBuilder sb = new StringBuilder();
sb.Append("<div style=\"float:left;\"&rt;");
sb.Append(string.Format("<span class=\"pagerCurrentPage\"&rt;Page: {0} of {1}</span&rt;", (gv.PageIndex + 1).ToString(), gv.PageCount));


Literal lit = new Literal();
lit.Text = sb.ToString();
ph.Controls.Add(lit);

//adding the table
//StringBuilder sbTable = new StringBuilder();
//sbTable.Append("width=\"100\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"left\"&rt;");
//sbTable.Append("


// Previous page button
ImageButton btnImage = new ImageButton();
btnImage.ID = "btnPrevPage";
btnImage.CommandName = "Page";
btnImage.CommandArgument = "Prev";
btnImage.ToolTip = "Previous Page";
//btnImage.ImageAlign = ImageAlign.Middle;

if (gv.PageIndex == 0)
{
btnImage.Enabled = false;
btnImage.ImageUrl = "~/Images/prev_icon.jpg";
}
else
{
btnImage.Enabled = true;
btnImage.ImageUrl = "~/Images/prev_icon.jpg";
//btnImage.OnClientClick = "GeneaSys.Common.addProgressText('Previous grid page...');";
btnImage.Click += new ImageClickEventHandler(btn_Click2);
}

ph.Controls.Add(btnImage);

// Pager page links
LinkButton lnb;
int numberOfPageLinks = gv.PagerSettings.PageButtonCount;
int startPage = (((gv.PageIndex) / numberOfPageLinks) * numberOfPageLinks) + 1;
if (startPage &rt; gv.PageCount - numberOfPageLinks && startPage &rt; numberOfPageLinks) startPage = (gv.PageCount - numberOfPageLinks) + 1;
for (int i = startPage; i < numberOfPageLinks + startPage && i <= gv.PageCount; i++)
{
if (i &rt; startPage)
{
lit = new Literal();
lit.Text = " <span class=\"pageSeperator\"&rt;|</span&rt; ";
ph.Controls.Add(lit);
}
else
{
lit = new Literal();
lit.Text = "<span class=\"pagerPageNumbers\"&rt;";
ph.Controls.Add(lit);
}
if (gv.PageIndex + 1 == i)
{
lit = new Literal();
lit.Text = i.ToString();
ph.Controls.Add(lit);
}
else
{
lnb = new LinkButton();
lnb.CommandName = "Page";
lnb.CommandArgument = i.ToString();
lnb.Text = i.ToString();
ph.Controls.Add(lnb);
}
}
lit = new Literal();
lit.Text = "</span&rt;";
ph.Controls.Add(lit);

// Next page button
btnImage = new ImageButton();
btnImage.ID = "btnNextPage";
btnImage.CommandName = "Page";
btnImage.CommandArgument = "Next";
btnImage.ToolTip = "Next Page";
//btnImage.ImageAlign = ImageAlign.Middle;
if (gv.PageIndex == gv.PageCount - 1)
{
btnImage.Enabled = false;
btnImage.ImageUrl = "~/images/next_icon.jpg";
}
else
{
btnImage.Enabled = true;
btnImage.ImageUrl = "~/Images/next_icon.jpg";
btnImage.Click += new ImageClickEventHandler(btn_Click1);
//btnImage.OnClientClick = "GeneaSys.Common.addProgressText('Next grid page...');";
}
ph.Controls.Add(btnImage);


lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

lit = new Literal();
lit.Text = "<span style=\"width:600px;\"&rt; Go to page: ";
ph.Controls.Add(lit);

TextBox tb = new TextBox();
tb.ID = "txtGoToPage";
tb.ToolTip = "Enter the Page Number";
tb.Width = Unit.Pixel(30);
tb.CausesValidation = true;
tb.ValidationGroup = "GoToPage";
ph.Controls.Add(tb);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

ImageButton btn = new ImageButton();
btn.ID = "btnGoToPage";
//btn.Text = "Go";
btn.ImageUrl = "~/Images/go_btn.gif";
btn.CausesValidation = true;
btn.ValidationGroup = "GoToPage";
btn.ToolTip = "Go To Page";
btn.Click += new ImageClickEventHandler(btn_Click);
//btn.OnClientClick = "GeneaSys.Common.addProgressText('Going to grid page...');";
ph.Controls.Add(btn);

RangeValidator rv = new RangeValidator();
rv.ID = "valPager";
rv.Type = ValidationDataType.Integer;
rv.MinimumValue = "1";
rv.MaximumValue = gv.PageCount.ToString();
rv.Text = "not a page";
rv.EnableClientScript = true;
rv.ValidationGroup = "GoToPage";
rv.ControlToValidate = "txtGoToPage";
rv.SetFocusOnError = true;
ph.Controls.Add(rv);
// html markup
lit = new Literal();
lit.Text = "</div&rt;<div style=\"float:right;\"&rt;";
ph.Controls.Add(lit);



lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt; </span&rt;";
ph.Controls.Add(lit);

RangeValidator rv1 = new RangeValidator();
rv1.ID = "valPager1";
rv1.Type = ValidationDataType.Integer;
rv1.MinimumValue = "1";

rv1.MaximumValue = GridRecordCount;

rv1.Text = "Exceeds max records";
rv1.EnableClientScript = true;
rv1.ValidationGroup = "PageSize";
rv1.ControlToValidate = "txtPageSize";
rv1.SetFocusOnError = true;

ph.Controls.Add(rv1);

lit = new Literal();
lit.Text = "<span style=\" margin: 0pt; width: 200px; padding-left: 35px;\"&rt;Results per page: ";
ph.Controls.Add(lit);


TextBox tb1 = new TextBox();
tb1.ID = "txtPageSize";
tb1.Text = gv.PageSize.ToString();
tb1.ToolTip = "Enter the results per Page Size";
tb1.Width = Unit.Pixel(30);
tb1.CausesValidation = true;
tb1.ValidationGroup = "PageSize";
ph.Controls.Add(tb1);

lit = new Literal();
lit.Text = "<span class=\"EmptySpan\"&rt;  </span&rt;";
ph.Controls.Add(lit);

ImageButton btn1 = new ImageButton();
btn1.ID = "btnGoToPageSize";
//btn.Text = "Go";
btn1.ImageUrl = "~/Images/go_btn.gif";
btn1.CausesValidation = true;
btn1.ValidationGroup = "PageSize";
btn1.ToolTip = " Page Size";
btn1.Click += new ImageClickEventHandler(btn2_Click);
//btn.OnClientClick = "GeneaSys.Common.addProgressText('Going to grid page...');";
ph.Controls.Add(btn1);


}


private static void btn2_Click(object sender, EventArgs e)
{


GridViewRow gvr = (GridViewRow)((ImageButton)sender).NamingContainer;
TextBox tb = (TextBox)gvr.FindControl("txtPageSize");
GridView gv = (GridView)gvr.NamingContainer;

int pageSize;

if (int.TryParse(tb.Text, out pageSize))
{
if (pageSize <= 0)
{
//gv.PageIndex = 0;
}
else
{

gv.PageSize = int.Parse(tb.Text);
}


}

}
private static void btn_Click(object sender, EventArgs e)
{
//GridViewRow gvr = (GridViewRow)((ImageButton)sender).NamingContainer;
//TextBox tb = (TextBox)gvr.FindControl("txtGoToPage");
//GridView gv = (GridView)gvr.NamingContainer;

//int pageNumber;

//if (int.TryParse(tb.Text, out pageNumber))
//{
// gv.PageIndex = (pageNumber - 1);
//}

GridViewRow gvr = (GridViewRow)((ImageButton)sender).NamingContainer;
TextBox tb = (TextBox)gvr.FindControl("txtGoToPage");
GridView gv = (GridView)gvr.NamingContainer;

int pageNumber;

if (int.TryParse(tb.Text, out pageNumber))
{
if (pageNumber <= 0)
{
//gv.PageIndex = 0;
}
else if (pageNumber <= gv.PageCount)
{
gv.PageIndex = (pageNumber - 1);
}


}

}
private static void btn_Click1(object sender, EventArgs e)
{

GridViewRow gvr = (GridViewRow)((ImageButton)sender).NamingContainer;
TextBox tb = (TextBox)gvr.FindControl("txtGoToPage");
GridView gv = (GridView)gvr.NamingContainer;
int pageNumber = gv.PageIndex;
gv.PageIndex = pageNumber + 1;

}
private static void btn_Click2(object sender, EventArgs e)
{

GridViewRow gvr = (GridViewRow)((ImageButton)sender).NamingContainer;
TextBox tb = (TextBox)gvr.FindControl("txtGoToPage");
GridView gv = (GridView)gvr.NamingContainer;
int pageNumber = gv.PageIndex;
if (pageNumber != 0)
gv.PageIndex = pageNumber - 1;

}
}


Coding in CS page

/// <summary&rt;
/// Method for prerender of grid grvManageRequest
/// </summary&rt;
/// <param name="sender"&rt;</param&rt;
/// <param name="e"&rt;</param&rt;
protected void grvManageRequest_PreRender(object sender, EventArgs e)
{
try
{
//SetGridHeaderSortDirectionArrow((GridView)sender, "SharedEquipmentName");
if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.Params["pgn"]))
{
grvManageRequest.PageIndex = Convert.ToInt32(Request.Params["pgn"]);
}
}
catch (Exception ex)
{
SetResultMessageToLabel("Error :" + ex.Source + " <BR/&rt;Error Message : Error Occured while loading the data to the grid view " + ex.Message, false);
}

}

----------------
protected void grvManageRequest_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
GridView gv = (GridView)sender;
// This is for the custom grid pager.
if (e.Row.RowType == DataControlRowType.Pager)
{
TotalRecordCount = ocdsManageRequest.Items.Count;
GridViewPageFooter.GetGridViewPager(gv, e.Row);
}
}
catch(Exception ex)
{
}
}