S
Sjaakie
Hi all,
I have a Usercontrol containing a pre-formatted DataGrid and some code
which by default enables paging (with links 1..20). I now want to add
some extra paging information to the PagerTemplate using the code below.
It does show me all the labels and links, but the event I attach to the
previous and next links isn't triggered on click. I must be doing
something wrong, perhaps you can help me out?
TIA
--- the code (hope it's readable) ---
private void grid_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
// Extract the Pager
TableCell pager = (TableCell)e.Item.Controls[0];
//Add Cell to Row to Hold Row Count Label
TableCell newcell = new TableCell();
newcell.ColumnSpan = this.grid.Columns.Count / 2;
newcell.HorizontalAlign = HorizontalAlign.Left;
newcell.ID = "pagerInfo";
//Add Table Cell to Pager
e.Item.Controls.AddAt(0, newcell);
//Subtract from Colspan of Original Pager to Account for New Row
pager.ColumnSpan = pager.ColumnSpan - 1;
}
}
private void grid_PreRender(object sender, EventArgs e)
{
try
{
// add pagerinfo to top and bottom pagertemplate
this.GridPagingInfo(((TableCell)grid.Controls[0].Controls[0].FindControl("pagerInfo")));
this.GridPagingInfo(((TableCell)grid.Controls[0].Controls[grid.Controls[0].Controls.Count
- 1].FindControl("pagerInfo")));
}
catch
{}
}
private void GridPagingInfo(TableCell p) {
if (this.RecordCount > 0)
{
Label pageInfo = new Label();
/* paging info */
pageInfo.Text = this.RecordCount.ToString() + " records ";
pageInfo.Text += "| " +
this.grid.Items.Count.ToString() + " shown | " +
"Page " + (this.grid.CurrentPageIndex + 1).ToString() + " of " +
Math.Ceiling(Convert.ToDouble(this.RecordCount)/this.grid.PageSize);
p.Controls.Add(pageInfo);
p.Controls.Add(new Seperator());
// previous-button
if (this.grid.CurrentPageIndex > 0)
{
LinkButton gridPreviousLink = new LinkButton();
gridPreviousLink.Text = "Previous";
p.Controls.Add(gridPreviousLink);
gridPreviousLink.Click += new EventHandler(gridPreviousLink_Click);
}
else
{
Label gridPreviousLabel = new Label();
gridPreviousLabel.Text = "Previous";
gridPreviousLabel.ForeColor =
System.Drawing.ColorTranslator.FromHtml("#999999");
p.Controls.Add(gridPreviousLabel);
}
p.Controls.Add(new Seperator());
// next-button
if ((this.grid.CurrentPageIndex ) < (this.RecordCount / this.PageSize))
{
LinkButton gridNextLink = new LinkButton();
gridNextLink.Text = "Next";
p.Controls.Add(gridNextLink);
gridNextLink.Click += new EventHandler(gridNextLink_Click);
}
else
{
Label gridNextLabel = new Label();
gridNextLabel.Text = "Next";
gridNextLabel.ForeColor =
System.Drawing.ColorTranslator.FromHtml("#999999");
p.Controls.Add(gridNextLabel);
}
}
}
I have a Usercontrol containing a pre-formatted DataGrid and some code
which by default enables paging (with links 1..20). I now want to add
some extra paging information to the PagerTemplate using the code below.
It does show me all the labels and links, but the event I attach to the
previous and next links isn't triggered on click. I must be doing
something wrong, perhaps you can help me out?
TIA
--- the code (hope it's readable) ---
private void grid_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
// Extract the Pager
TableCell pager = (TableCell)e.Item.Controls[0];
//Add Cell to Row to Hold Row Count Label
TableCell newcell = new TableCell();
newcell.ColumnSpan = this.grid.Columns.Count / 2;
newcell.HorizontalAlign = HorizontalAlign.Left;
newcell.ID = "pagerInfo";
//Add Table Cell to Pager
e.Item.Controls.AddAt(0, newcell);
//Subtract from Colspan of Original Pager to Account for New Row
pager.ColumnSpan = pager.ColumnSpan - 1;
}
}
private void grid_PreRender(object sender, EventArgs e)
{
try
{
// add pagerinfo to top and bottom pagertemplate
this.GridPagingInfo(((TableCell)grid.Controls[0].Controls[0].FindControl("pagerInfo")));
this.GridPagingInfo(((TableCell)grid.Controls[0].Controls[grid.Controls[0].Controls.Count
- 1].FindControl("pagerInfo")));
}
catch
{}
}
private void GridPagingInfo(TableCell p) {
if (this.RecordCount > 0)
{
Label pageInfo = new Label();
/* paging info */
pageInfo.Text = this.RecordCount.ToString() + " records ";
pageInfo.Text += "| " +
this.grid.Items.Count.ToString() + " shown | " +
"Page " + (this.grid.CurrentPageIndex + 1).ToString() + " of " +
Math.Ceiling(Convert.ToDouble(this.RecordCount)/this.grid.PageSize);
p.Controls.Add(pageInfo);
p.Controls.Add(new Seperator());
// previous-button
if (this.grid.CurrentPageIndex > 0)
{
LinkButton gridPreviousLink = new LinkButton();
gridPreviousLink.Text = "Previous";
p.Controls.Add(gridPreviousLink);
gridPreviousLink.Click += new EventHandler(gridPreviousLink_Click);
}
else
{
Label gridPreviousLabel = new Label();
gridPreviousLabel.Text = "Previous";
gridPreviousLabel.ForeColor =
System.Drawing.ColorTranslator.FromHtml("#999999");
p.Controls.Add(gridPreviousLabel);
}
p.Controls.Add(new Seperator());
// next-button
if ((this.grid.CurrentPageIndex ) < (this.RecordCount / this.PageSize))
{
LinkButton gridNextLink = new LinkButton();
gridNextLink.Text = "Next";
p.Controls.Add(gridNextLink);
gridNextLink.Click += new EventHandler(gridNextLink_Click);
}
else
{
Label gridNextLabel = new Label();
gridNextLabel.Text = "Next";
gridNextLabel.ForeColor =
System.Drawing.ColorTranslator.FromHtml("#999999");
p.Controls.Add(gridNextLabel);
}
}
}