I have created a navigation bar that is designed to cycle through the pages
of a datagrid. It is created at runtime in the ItemCreated event of the
datagrid and added to the control list of one of the cells in the datagrid
header. In theory, when I click on one of the imagebuttons the control
contains then the event is bubbled to the control and in turn handled in the
code behind of the page on which it resides. Instead when I click on the
control, I get "System.NullReferenceException: Object reference not set to an
instance of an object." on the CommandEvent(this, e); line in
NavBar_Command(below).

I have sucessfully used this technique on two previous controls, but those
both exist as tags upon the ASPX page. This one is created wholly in the
codebehind. Below are what I belive to be the relevant pieces of code. Any
suggestions would be appreciated.

'------VB ----- Code Behind for the page on which the control will appear
'----- Datagrid Item Created event --- add the NavBar to the first cell
dgCell = New TableCell
dgCell.ColumnSpan = 2
dgCell.HorizontalAlign = HorizontalAlign.Center
'---------------------'
portNavBar = New NavBar
portNavBar.CurrentPage = CurrentPage
portNavBar.PageCount = PageCount

dgCell.Controls.Add(portNavBar)
dgItem.Cells.Add(dgCell)


-------Handle the bubbled event
Public Sub navbar_command(ByVal sender As Object, ByVal e As
CommandEventArgs) Handles portNavBar.CommandEvent
If e.CommandName = "navigate" Then
Dim ceiling As Integer = Math.Ceiling(PageCount / 10)
If ceiling < 5 Then ceiling = 5

UpdatePage()
Select Case e.CommandArgument
Case "reverse"
CurrentPage -= 1
Case "fastreverse"
CurrentPage -= ceiling
Case "forward"
CurrentPage += 1
Case "fastforward"
CurrentPage += ceiling
End Select
BindData(False)
End If
xcpMessage = "here"
End Sub
-----------END Code Behind snippets----

// C# ----From the NavBar Object ---
public delegate void OnCommandEvent(object sender, CommandEventArgs e);
public event OnCommandEvent CommandEvent;

private void NavBar_Command(object sender, CommandEventArgs e)
{
CommandEvent(this, e);
}


public NavBar()
{
ImageButton fastReverse = new ImageButton();
ImageButton reverse = new ImageButton();
ImageButton pagebar = new ImageButton();
ImageButton forward = new ImageButton();
ImageButton fastForward = new ImageButton();

fastReverse.ImageUrl = "images/nav_start.gif";
reverse.ImageUrl = "images/nav_reverse.gif";
pagebar.ImageUrl = "images/nav_middle.gif";
forward.ImageUrl = "images/nav_forward.gif";
fastForward.ImageUrl = "images/nav_end.gif";

fastReverse.Command += new CommandEventHandler(NavBar_Command);
reverse.Command += new CommandEventHandler(NavBar_Command);
pagebar.Command += new CommandEventHandler(NavBar_Command);
forward.Command += new CommandEventHandler(NavBar_Command);
fastForward.Command += new CommandEventHandler(NavBar_Command);

fastReverse.CommandName = "navigate";
reverse.CommandName = "navigate";
pagebar.CommandName = "navigate";
forward.CommandName = "navigate";
fastForward.CommandName = "navigate";

fastReverse.CommandArgument = "fastreverse";
reverse.CommandArgument = "reverse";
pagebar.CommandArgument = "pagebar";
forward.CommandArgument = "forward";
fastForward.CommandArgument = "fastforward";

t.Rows.Add(tr);
t.CssClass="noborders";
TableCell tc = new TableCell();
tc.Controls.Add(fastReverse);
tc.CssClass = "noborders";
tr.Cells.Add(tc);

tc = new TableCell();
tc.Controls.Add(reverse);
tc.CssClass = "noborders";
tr.Cells.Add(tc);

pageMeter = new TableCell();
pageMeter.Attributes.Add("background","images/nav_middle.gif");
pageMeter.Width = Unit.Pixel(90);
pageMeter.HorizontalAlign = HorizontalAlign.Center;
pageMeter.Controls.Add(pagebar);
pageMeter.CssClass = "noborders";
tr.Cells.Add(pageMeter);

tc = new TableCell();
tc.Controls.Add(forward);
tc.CssClass = "noborders";
tr.Cells.Add(tc);

tc = new TableCell();
tc.Controls.Add(fastForward);
tc.CssClass = "noborders";
tr.Cells.Add(tc);
this.Controls.Add(t);
}




--
Kasabaarde Sumta