Custom Usercontrol; Event not triggered

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);
}

}

}
 
Joined
Aug 6, 2006
Messages
1
Reaction score
0
Hi,

When I need to have a usercontrol fire events I create a class that inherits from control, usercontrol or webcontrol and define the event XXX and an OnXXX function to allow derived classes to raise the event. Then rather than the usercontrol inheriting from usercontrol, it inherits from my class with the event.

Hope this helps.
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top