invoke methods on the parent page from the user control


M

Max

I want to do something like this in the user control:
Call myParentPage.BindSQL()

BindSQL is a sub on the parent page. This user control will be used on
multiple pages. Perhaps this is not a good idea? This is why it is so
difficult?

-Max
 
Ad

Advertisements

C

Craig Deelsnyder

Max said:
I want to do something like this in the user control:
Call myParentPage.BindSQL()

BindSQL is a sub on the parent page. This user control will be used on
multiple pages. Perhaps this is not a good idea? This is why it is so
difficult?

-Max
You have a .Page property in the user control, the only problem is you
need to cast it before calling that function....

((MyPageClass)this.Page).BindSQL

But if you do this, you're tying your user control to one type of
containing page (which is usually one page). If you do it this way, I'd
pry create a superclass for all pages that share this function and then
cast to that type.....

The other way to do it (and is pry better from an OO encapsulation
standpoint) is to expose an event in the user control that it raises
when it wants to call the .Page's function. But instead of having to
know the page type, or function name, the user control lets the page be
responsible for registering an event handler/listener to that event when
it creates one of your user controls.
 
B

bruce barker

its trival, either use reflection to call the method:

MethodInfo mi = this.Page.GetType().GetMethod("BindSQL", BindingFlags.Public
| BindingFlags.Instance);
if (mi) mi.Invoke(null, new Object[] {});

or have pages inherit from a base and cast:

myBasePage bp = this.Page as myBasePage;
if (bp) bp.BindSQL();

-- bruce (sqlwork.com)
 
M

Max

I don't understand how reflection will work. I understand it retrieves the
method from assembly, but I don't know how to use it in my case.

MethodInfo mi = Me.Page.GetType().GetMethod("BindSQL", BindingFlags.Public
....
and then you lost me...

Your casting example won't appear to work, because you're saying I need to
use the page's class, but I want to call this sub from a user control on
every page.


bruce barker said:
its trival, either use reflection to call the method:

MethodInfo mi = this.Page.GetType().GetMethod("BindSQL", BindingFlags.Public
| BindingFlags.Instance);
if (mi) mi.Invoke(null, new Object[] {});

or have pages inherit from a base and cast:

myBasePage bp = this.Page as myBasePage;
if (bp) bp.BindSQL();

-- bruce (sqlwork.com)



Max said:
I want to do something like this in the user control:
Call myParentPage.BindSQL()

BindSQL is a sub on the parent page. This user control will be used on
multiple pages. Perhaps this is not a good idea? This is why it is so
difficult?

-Max
 
Ad

Advertisements

Joined
Jul 15, 2011
Messages
1
Reaction score
0
This thread is old, but still a top hit on google.

@max or any other devs. stumbling upon this thread:

Bruce suggestion does work with a small modification. Make sure to pass in "this.Page" instead of "null" to the method invoke and it should execute no problem.

If not, you will likely get an "Non-static method requires a target" error.

After changing "null" to "this.Page" and specifying object parameters, I ran through a quick test and verified this works.

Visit my website http://www.ronniediaz.com for specific code example for this where I call a repeater item command method on a parent page from the user control by prototyping the same function in the user control and using Bruce syntax above.
 

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