can control call parent & have value returned to it?

J

Jason Shohet

I have an ascx control, and it needs a value called 'orgID' which the parent
page will have. I want to avoid the parent page setting values in fields on
the ascx control since this violates encapsulation principles,
SO...
I want to know if InvokeMember can be used to return a value from the
parent. ie:

Type myParent = this.Page.GetType();
string strOrgid = myParent.InvokeMember("GetOrgID", System.Reflection...
null, this.Page, new object[]{ });

If I make GetOrgID a function on the parent that returns a string, can the
above work?
Or is there a better way for the ascx to request values from its parent.

TY Jason Shohet
 
J

Jim Corey

Sort of similar to your problem, I was looking at various ways of
calling a function on my parent page from a ascx control, and settled on
the one below. I'm in VB and calling a function rather than returning a
property (and you probably want to have a property rather than a
variable).
(The page in this case is MainReport.aspx)

Dim myParent As MainReport
myParent = CType(HttpContext.Current.Handler, MainReport)
myParent.LoadFilter()
 
J

Jason Shohet

I know how to call a func on the parent page from the ascx control... but
can that fun on the parent page RETURN a value to the ascx? Thats what i'm
looking for. Jim your LoadFilter() func, can it return a value to the
calling ascx? Or my Invoke function (shown further up in the thread), can
that return a value...

TY Jason
 
G

Guest

You might try looking at
Page.Context.Items - MSDN for HttpContext Members and check out the
"Items" under Public Properties

Hope this helps. Posting is provided as Is
 
J

Jason Shohet

Nothing comes up on a search of page.context.items & msdn.
I don't think there is a way to get a value back from the parent page using
this tactic.
Anyone have any other way to do this. The ascx page wants a value from the
parent page...
 
J

Jason Shohet

sorry i don't have the time to research this on msdn...
Plus i don't see anything on this on any other .NET site, so i'm begining to
doubt whether it can be done (otherwise, someone would have code showing
it).
i'll have to just modify values on the controls directly from the page
(instead of the controls requesting the values from the parent)
 
K

Kevin Spencer

You don't have time to research this on MSDN, which is available to you 24
hours a day? You started this thread nearly 24 hours ago! I hope
optimization isn't your specialty...

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jason Shohet

kev, i'm holding down 3 projects... barely. I've lost 1 programmer and
another is off this week ;)
Look if this solution isn't readily available anywhere, (i have looked
around for about 20 minutes at various sites on ascx controls -- but all i
see is event / delegate stuff), I just can't do it ;)
 
K

Kevin Spencer

Well, Jason, let me help you out. The Control is hosted in tyhe Page, and is
a part of the WebForm's Controls Collection. Any method that you can call
which returns a value will return a value to the caller. The .ascx (User
Control) is simply a class. As long as the method is accessible at the scope
at which the Control resides (public or internal), it will not require any
special instructions. Just call it as you would any other public or internal
method.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jason Shohet

Well Kev ty for the useful info. If thats true -- any method you can call
which returns a value will return a value to the caller -- Then that should
apply to this line:
string strOrg = myParent.InvokeMember("GetOrg",
System.Reflection..., new object[]{});

I've used the above many times -- without returning anything -- to call some
function on the parent. I don't know any other way -- besides this bizarre
reflection function, to call func's on the parent. Currently the
InvokeMember above does not work because the string being returned is 'not
of type object'.
Perhaps if new object[] above can be replaced by string -- and voila I
can return a string from the parent func?
 
K

Kevin Spencer

Well, Jason, I'm not sure what the reference to "MyParent" is, but you can
refer to the WebForm (by any Control) as the Page property of the Control
(this is a common property to all System.Web.UI.Controls). So, you can get
to the Page (if that's where the function is) from your User Control by
simply calling:

string strOrg = Page.GetOrg(some Parameter);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jason Shohet

myParent refers to the webpage. The problem is that in the line below,
GetOrg represents a function on that parent webpage: BUT IT CANNOT RETURN A
VALUE !!! It can only execute stuff, and return void. Thats the problem
with controls: Encapsulation & polymorphism don't really work with it (at
least using the INvokeMember method) because the control cannot say, "Ok
parent, I want the organization, & I don't care how you do it, what kind of
organization it is, just return me an organization number".


Well Kev ty for the useful info. If thats true -- any method you can call
which returns a value will return a value to the caller -- Then that should
apply to this line:
string strOrg = myParent.InvokeMember("GetOrg",
System.Reflection..., new object[]{});

I've used the above many times -- without returning anything -- to call some
function on the parent. I don't know any other way -- besides this bizarre
reflection function, to call func's on the parent. Currently the
InvokeMember above does not work because the string being returned is 'not
of type object'.
Perhaps if new object[] above can be replaced by string -- and voila I
can return a string from the parent func?
 
G

Guest

#1 - First off it sounds like you've not tried a sample code and would
rather be spoon fed.
#2 - InvokeMember returns what the function that it invoked retunred
#3 - If you are unable to copy /paste code then why dont you set a public
property or one of the other aforementioned methods of sharing data. Call
the function. The function stores the data in there with a uniquekey.
Whenyou return you check the place that was stored for the data with the
unique key.



Jason Shohet said:
myParent refers to the webpage. The problem is that in the line below,
GetOrg represents a function on that parent webpage: BUT IT CANNOT RETURN
A
VALUE !!! It can only execute stuff, and return void. Thats the problem
with controls: Encapsulation & polymorphism don't really work with it (at
least using the INvokeMember method) because the control cannot say, "Ok
parent, I want the organization, & I don't care how you do it, what kind
of
organization it is, just return me an organization number".


Well Kev ty for the useful info. If thats true -- any method you can call
which returns a value will return a value to the caller -- Then that should
apply to this line:
string strOrg = myParent.InvokeMember("GetOrg",
System.Reflection..., new object[]{});

I've used the above many times -- without returning anything -- to call some
function on the parent. I don't know any other way -- besides this bizarre
reflection function, to call func's on the parent. Currently the
InvokeMember above does not work because the string being returned is 'not
of type object'.
Perhaps if new object[] above can be replaced by string -- and voila I
can return a string from the parent func?
 
J

jason

Doe -- your English is, interesting. "try a sample code"...
And then you write, "InvokeMember returns...invoked retunred"

Wow, a lot to meditate on there guru.

FYI, I've tried several ways to do this. .NET framework does not
allow this functionality in a straightforward way yet -- Apparently
the parent has to call methods directly on the control if that control
needs values from the parent. The control cannot 'ask' for values.
This limits use of polymorphism with controls... hopefully this is
addressed in c# v2 etc.
 
G

Guest

bool ReturnValue = (bool) TmpType.InvokeMember("MyFunction",
BindingFlags.InvokeMethod, null, this.Page, new object[] { 10,
tmpBuilder });

TmpBuilder is given a value of "d" initially and in the method MyFunction we
call the Append method and add "eee". We then return true.

ReturnValue has a value of true
tmpBuilder has a value of "deee"

1 Value was returned and one class was modified (which could thus of course
hold 10000s of variables, business logic or whatever)

I apologize the word "single" was missing from the first one and I seldom
ever run the spell checker on newsgroup postings because despite the makings
of a perfect post if somebody wants to flame somebody they just go find
something else to nitpick/flame about. What little newsgroup posting I do is
usually done on the fly while looking for solutions or information
references and I do very little because of responses like the one you just
gave.
You asked for a solution. How can I get returned values from a Method Called
from the page via a CustomControl. 2 Answers were provided. You stated that
one of the samples provided did not do what was stated. You ignored the
second option. The above code shows that solution #1 provided allows for a
return of infinite amount of data


Hope this helps. Posting is Provided "AS IS"
 
B

Bob

Jason, your sample code uses reflection to find the function. Should avoid
that for performance reason. I can see string strOrg = Page.GetOrg(some
Parameter); Kevin suggested won't work because GetOrg() is not a member of
Page, but rather a member of the particular derived page you created. So you
need to do a type case first:

string strOrg = ((MyOwnPage)Page).GetOrg(some Parameter);

Even though this is doable, it's very bad code, because it creates a
circular dependency between the aspx and the ascx classes. You should look
into the hooking an event handler to certain event to get the proper value,
or at least add a public member on your ascx class for the page class to
call.

Bob

Kevin Spencer said:
Well, Jason, I'm not sure what the reference to "MyParent" is, but you can
refer to the WebForm (by any Control) as the Page property of the Control
(this is a common property to all System.Web.UI.Controls). So, you can get
to the Page (if that's where the function is) from your User Control by
simply calling:

string strOrg = Page.GetOrg(some Parameter);

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Jason Shohet said:
Well Kev ty for the useful info. If thats true -- any method you can call
which returns a value will return a value to the caller -- Then that should
apply to this line:
string strOrg = myParent.InvokeMember("GetOrg",
System.Reflection..., new object[]{});

I've used the above many times -- without returning anything -- to call some
function on the parent. I don't know any other way -- besides this bizarre
reflection function, to call func's on the parent. Currently the
InvokeMember above does not work because the string being returned is 'not
of type object'.
Perhaps if new object[] above can be replaced by string -- and voila I
can return a string from the parent func?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top