473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Type information for UserControls

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
8 1329
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Emma | last post by:
Hi, I'm developing a GUI with almost all Usercontrols. It's been working wonderfull up till a few weeks ago, and really crashed yesterday. Suddenly I can no longer add some of my usercontrols...
4
by: Anders K. Jacobsen [DK] | last post by:
Hi I have some common UserControls i want to share between to sites (on the same mashine but on diffrent virtual paths). right now i have on solution file with aprox 10 projects. 2 of these is...
2
by: Alixx Skevington | last post by:
Hi there peeps, I am having a problem have just started to use usercontrols, and I am hitting a problem. I get the error message above, now each control has a table, and 1 asp:textbox...
2
by: Søren M. Olesen | last post by:
Hi I starting to get the following compiler error, but I really don't know how to resolve it. Value of type 'DXP.Shared.myPath' cannot be converted to 'DXP.Shared.myPath'. Type mismatch...
1
by: Søren M. Olesen | last post by:
Hi In my website I have a module, placed under App_Code with a number of utility functions. For one of these functions I'm trying to pass a usercontrol as an argument, however I keep getting...
1
by: Carlo Razzeto | last post by:
Hey, I've got a couple of production UserControls going in my companies web system, but I'm having a couple of annoying issues with them. One of them, for instance is a ScannerButton control which...
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
4
by: Nathan Sokalski | last post by:
In several of my UserControls I add properties. If I access these properties in the CodeBehind of the pages that use the controls, I recieve an error when compiling. The reason for this is because...
11
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C#...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.