473,467 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ASP.NET Reflection problem

Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method
called DoSomething(params). My web page contains a UserControl (say
Control.ascx).

In the control class, i want to call the DoSomething method. I cannot use
type cast since in fact the base class Page structure is a little more
complex (see bottom of post). Hence i cannot do:
CType(Me.Page, BasePage).DoSomething(params)

instead i use reflection

Me.Page.GetType().InvokeMember("DoSomething", BindingFlags.InvokeMethod,_
Nothing, Me.Page, New Object() {param})
'This is pretty much the same type of code used to call methods from a
master
'page in a UserControl (since master page's data type in not accessible
from a
'usercontrol)

In any .NET App or Web app, this works well. But for some reason, it doesn't
work in a UserControl. (This code works fine in the aspx code behind but not
in the ascx code behind).

In a UserControl's code behind, this throws a MissingMethodException

Anyone already experienced this issue?

What is very strange here is that the following code does work

Dim mi As Reflection.MethodInfo = Me.Page.GetType().GetMethod("DoSomething")
mi.Invoke(Me.Page, Reflection.BindingFlags.InvokeMethod, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

==============================
Base Class Page structure

Class FoundationBasePage : System.Web.UI.Page
Class FoundationBaseFirstPage : FoundationBasePage
Class FoundationBaseLastPage : FoundationBasePage

Class BasePage : FoundationBasePage
Class BaseFirstPage : FoundationBaseFirstPage
Class BaseLastPage : FoundationBaseLastPage

The method DoSomething is located in BasePage, BaseFirstPage and BaseLastPage
The UserControl may be used in pages inheriting from any of those three base
classes.
Feb 12 '07 #1
2 2960
the methods needs to be public. a better approach is for the page to
implement an interface, then the user control can cast the Page to the
interface, and call the method directly, rather than use reflection.

-- bruce (sqlwork.com)

Renaud Langis wrote:
Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method
called DoSomething(params). My web page contains a UserControl (say
Control.ascx).

In the control class, i want to call the DoSomething method. I cannot use
type cast since in fact the base class Page structure is a little more
complex (see bottom of post). Hence i cannot do:
CType(Me.Page, BasePage).DoSomething(params)

instead i use reflection

Me.Page.GetType().InvokeMember("DoSomething", BindingFlags.InvokeMethod,_
Nothing, Me.Page, New Object() {param})
'This is pretty much the same type of code used to call methods from a
master
'page in a UserControl (since master page's data type in not accessible
from a
'usercontrol)

In any .NET App or Web app, this works well. But for some reason, it doesn't
work in a UserControl. (This code works fine in the aspx code behind but not
in the ascx code behind).

In a UserControl's code behind, this throws a MissingMethodException

Anyone already experienced this issue?

What is very strange here is that the following code does work

Dim mi As Reflection.MethodInfo = Me.Page.GetType().GetMethod("DoSomething")
mi.Invoke(Me.Page, Reflection.BindingFlags.InvokeMethod, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

==============================
Base Class Page structure

Class FoundationBasePage : System.Web.UI.Page
Class FoundationBaseFirstPage : FoundationBasePage
Class FoundationBaseLastPage : FoundationBasePage

Class BasePage : FoundationBasePage
Class BaseFirstPage : FoundationBaseFirstPage
Class BaseLastPage : FoundationBaseLastPage

The method DoSomething is located in BasePage, BaseFirstPage and BaseLastPage
The UserControl may be used in pages inheriting from any of those three base
classes.
Feb 12 '07 #2
Thanks for your reply. I feel a bit stupid that i didn't think about the
interface...

Yet, it should work. The function i am calling is public (it works when
called from the aspx code behind).

I already solved the problem using the GetMethod call (exposed later in my
previous post) but i think designing an interface is more neat and is
probably faster.

Thanks a lot

Renaud

"bruce barker" wrote:
the methods needs to be public. a better approach is for the page to
implement an interface, then the user control can cast the Page to the
interface, and call the method directly, rather than use reflection.

-- bruce (sqlwork.com)

Renaud Langis wrote:
Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method
called DoSomething(params). My web page contains a UserControl (say
Control.ascx).

In the control class, i want to call the DoSomething method. I cannot use
type cast since in fact the base class Page structure is a little more
complex (see bottom of post). Hence i cannot do:
CType(Me.Page, BasePage).DoSomething(params)

instead i use reflection

Me.Page.GetType().InvokeMember("DoSomething", BindingFlags.InvokeMethod,_
Nothing, Me.Page, New Object() {param})
'This is pretty much the same type of code used to call methods from a
master
'page in a UserControl (since master page's data type in not accessible
from a
'usercontrol)

In any .NET App or Web app, this works well. But for some reason, it doesn't
work in a UserControl. (This code works fine in the aspx code behind but not
in the ascx code behind).

In a UserControl's code behind, this throws a MissingMethodException

Anyone already experienced this issue?

What is very strange here is that the following code does work

Dim mi As Reflection.MethodInfo = Me.Page.GetType().GetMethod("DoSomething")
mi.Invoke(Me.Page, Reflection.BindingFlags.InvokeMethod, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

==============================
Base Class Page structure

Class FoundationBasePage : System.Web.UI.Page
Class FoundationBaseFirstPage : FoundationBasePage
Class FoundationBaseLastPage : FoundationBasePage

Class BasePage : FoundationBasePage
Class BaseFirstPage : FoundationBaseFirstPage
Class BaseLastPage : FoundationBaseLastPage

The method DoSomething is located in BasePage, BaseFirstPage and BaseLastPage
The UserControl may be used in pages inheriting from any of those three base
classes.
Feb 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything....
2
by: Marco | last post by:
Hi, I try to use reflection to retrieve class B members and not inherited members. I wonder why the myMember value is nothing. Any suggestions? Thanks Module Module1 Sub Main()
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.