Type information for UserControls

L

Leszek Taratuta

Hello,

I have the following code:

// Load the MenuBar.ascx user control. The control defines SavePropertyEvent
event.
UserControl ctrl =
(UserControl)Page.LoadControl("~/MyApp/Controls/MenuBar.ascx");

// Get type information of the loaded user control.
Type myType = ctrl.GetType();

// Get SavePropertyEvent event using reflection.
EventInfo events = myType.GetEvent( "SavePropertyEvent" );

if ( events != null )
{
// Assign event handler (does not work).

// PROBLEM: The following line does not work because ctrl is of generic
UserControl type
// that does not define SavePropertyEvent event.
ctrl.SavePropertyEvent += new CommandEventHandler(
SavePropertyEvent_Raise );

// The following line could work, but C# does not provide such
construction:
// casting to variables of type Type. Should I use a Converter? How?
( (myType)ctrl ).SavePropertyEvent += new CommandEventHandler(
SavePropertyEvent_Raise );
}

The question is how to cast the 'ctrl' variable to the type assigned to
'myType' variable.

Any hints?
Thanks

Leszek Taratuta
 
S

Scott Allen

I'm a little confused. Is there a reason you can't use:

MenuBar mb = (MenuBar)Page.LoadControl(...);
mb.SavePropertyEvent += new CommandEventHandler(...);
 
L

Leszek Taratuta

Sorry for confusion. I have sent just a part of code.

Actually I need to load many user controls of diffrent types. Some of them
define SavePropertyEvent some not. The ASPX page need to assign event
handler for those user controls that define SavePropertyEvent.

I wanted to create a generic code that would load user controls as
UserControl, then discover the specific derived type that defines (or not)
SavePropertyEvent, and assign event handler to this event.

Thanks for reply anyway.

Any other suggestions?

Leszek Taratuta
 
W

William F. Robertson, Jr.

You will look at the user control and see if it implements the
ISavePropertyEvent interface, if it does, you will cast the generic
UserControl to a ISavePropertyEvent to get the event to wire to.

I am not actually sure if this would work, but it compiled, but I didn't
actually run it.

<snippets>
public interace ISavePropertyEvent
{
event CommandEventHandler SavePropertyEvent;
}

public myUserControl : UserControl : ISavePropertyEvent
{
public event CommandEventHandler SavePropertyEvent;
}

UserControl control = Page.FindControl( "myControlName" );
if ( control is ISavePropertyEvent )
{
( ( ISavePropertyEvent ) control).SavePropertyEvent += new
CommandEventHandler(...)
}

HTH,

bill
 
L

Leszek Taratuta

Thanks.
I figured out this solution as well. The problem is that my user controls
can implement many different events. I just gave an example of
SavePropertyEvent, but there may be many other: LoadControlEvent,
ChangeIdEvent, RefreshParentEvent etc. It means I would need to create a
separate interface for each of those events:
ILoadControlEvent, IChangeIdEvent, IRefreshParentEvent etc

I hoped there would be a more generic way to discover the type of user
control (using reflection or descriptors) in run-time and assign proper
event handlers.

For now it seems there is no generic way.

Thanks anyway,
Leszek
 
W

William F. Robertson, Jr.

Ahh, I went back and reread your first post. Sorry for the confusion; I am
not convinced this is the best approach for you to take, but here is how to
do it.

You were almost there for your syntax.

<snippet>
if ( events != null )
events.AddEventHandler( ctrl, new CommandEventHandler(
SavePropertyEvent_Raise ) );

This will add the event handler to your control.

HTH,

bill
 
S

Scott Allen

Ah, I can see now what you need to do. I'm sure this can be done with reflection
- ASP.NET runtime does it all the time. Have you made any progress?
 
L

Leszek Taratuta

This is a part of a real project with a deadline, so I had to move on with
other things.
Temporarily I just duplicated some code, without using reflection.

Thanks,
Leszek
 
W

William F. Robertson, Jr.

Did you see the reflection code to accomplish what you wanting to
accomplish?

bill

UserControl ctrl =
(UserControl)Page.LoadControl("~/MyApp/Controls/MenuBar.ascx");
// Get type information of the loaded user control.
Type myType = ctrl.GetType();

// Get SavePropertyEvent event using reflection.
EventInfo events = myType.GetEvent( "SavePropertyEvent" );
if ( events != null )
{
//this is how to dynamically add events using reflection.
events.AddEventHandler( ctrl, new CommandEventHandler(
SavePropertyEvent_Raise ) );
}
 

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

Top