Click to See Complete Forum and Search --> : Event Handler problem .NET 2.0


Frankinstien
June 16th, 2008, 08:05 PM
Hello I'm creating controls dynamically within a customized control. I can get all the events to work when the event handlers are assigned within the custom control. However when I try to set the DataGridCommandEventHandler to a datagrid from within another class instantiated by the custom control the method for the event handler will not invoke. The buttons on the datagrid do fire an event and it does postback but the actual method to handle the event is never called. All the other event handlers for other controls will work fine even though the event handler method resides within another class.

This is driving mad; does anyone have any idea as to why this can happen?

This is the code that will work for all event handlers:

foreach (Object d in ControlsWithEvents)
{
theControl = (Control)d;
if(!theControl.GetType().Name.Equals("DataGrid"))
ctrlFactory.setEventHandler(theControl, new EventHandler(this.mainEvent));
else
{
myGrid = (DataGrid)theControl;
myGrid.ItemCommand += new DataGridCommandEventHandler(this.mainEvent2);
myGrid.DataBind();

}

}


Here is the code that won't work with the datagrid:


ArrayList PostBacks = CommonFunctions.GetControls();
Control theControl = null;
DataGrid myGrid = null;
foreach (Object d in PostBacks)
{
theControl = (Control)d;

if (!theControl.GetType().Name.Equals("DataGrid"))
{
ctrlFactory.setEventHandler(theControl, new EventHandler(this.mainEvent));
}
else
{
myGrid = (DataGrid)theControl;
myGrid.ItemCommand += new DataGridCommandEventHandler(this.mainEvent2);
myGrid.DataBind();

}

}

Frank