invoke methods on the parent page from the user control

  • Thread starter Thread starter Max
  • Start date Start date
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
 
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.
 
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)
 
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
 
Back
Top