By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Type information for UserControls

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


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

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

--
Scott
http://www.OdeToCode.com/blogs/scott/
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

Nov 19 '05 #2

P: n/a
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..
I'm a little confused. Is there a reason you can't use:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/
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


Nov 19 '05 #3

P: n/a
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

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..
I'm a little confused. Is there a reason you can't use:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/
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



Nov 19 '05 #4

P: n/a
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

"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..
I'm a little confused. Is there a reason you can't use:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

> 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
>



Nov 19 '05 #5

P: n/a
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
"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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

"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..
> I'm a little confused. Is there a reason you can't use:
>
> MenuBar mb = (MenuBar)Page.LoadControl(...);
> mb.SavePropertyEvent += new CommandEventHandler(...);
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>
> > 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
> >
>
>



Nov 19 '05 #6

P: n/a
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/
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
"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..

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

MenuBar mb = (MenuBar)Page.LoadControl(...);
mb.SavePropertyEvent += new CommandEventHandler(...);
--
Scott
http://www.OdeToCode.com/blogs/scott/
> 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

Nov 19 '05 #7

P: n/a
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:54*********************@msnews.microsoft.com. ..
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/
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
"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...

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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:53*********************@msnews.microsoft.com. ..

> I'm a little confused. Is there a reason you can't use:
>
> MenuBar mb = (MenuBar)Page.LoadControl(...);
> mb.SavePropertyEvent += new CommandEventHandler(...);
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>> 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


Nov 19 '05 #8

P: n/a
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 ) );
}
"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:OP**************@TK2MSFTNGP11.phx.gbl...
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

"Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
news:54*********************@msnews.microsoft.com. ..
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/
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
"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...

> 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
>
> "Leszek Taratuta" <ad*@taratuta.net> wrote in message
> news:OR**************@TK2MSFTNGP14.phx.gbl...
>
>> 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
>>
>> "Scott Allen" <sc***@nospam.OdeToCode.com> wrote in message
>> news:53*********************@msnews.microsoft.com. ..
>>
>>> I'm a little confused. Is there a reason you can't use:
>>>
>>> MenuBar mb = (MenuBar)Page.LoadControl(...);
>>> mb.SavePropertyEvent += new CommandEventHandler(...);
>>> --
>>> Scott
>>> http://www.OdeToCode.com/blogs/scott/
>>>> 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



Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.