469,642 Members | 1,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

can control call parent & have value returned to it?

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
Nov 18 '05 #1
27 2556

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()
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2

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()
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
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
Nov 18 '05 #4
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
Nov 18 '05 #5
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

" Jason Shohet" <as****@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #6
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

" Jason Shohet" <as****@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #7
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...
Nov 18 '05 #8
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...
Nov 18 '05 #9
#1 - "ascx" is a Custom control embedded inside a page. Therefore it has
access to the Page
#2 - if you convince yourself that you can't do it then you can't
#3 -
http://msdn.microsoft.com/library/en...mberstopic.asp
#4 -
http://www.google.com/search?hl=en&i...+Members+Items

" Jason Shohet" <as****@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
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...

Nov 18 '05 #10
#1 - "ascx" is a Custom control embedded inside a page. Therefore it has
access to the Page
#2 - if you convince yourself that you can't do it then you can't
#3 -
http://msdn.microsoft.com/library/en...mberstopic.asp
#4 -
http://www.google.com/search?hl=en&i...+Members+Items

" Jason Shohet" <as****@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
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...

Nov 18 '05 #11
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)
Nov 18 '05 #12
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)
Nov 18 '05 #13
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.

" Jason Shohet" <as****@hotmail.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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)

Nov 18 '05 #14
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.

" Jason Shohet" <as****@hotmail.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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)

Nov 18 '05 #15
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 ;)
Nov 18 '05 #16
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 ;)
Nov 18 '05 #17
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.

" Jason Shohet" <as****@hotmail.com> wrote in message
news:ep*************@TK2MSFTNGP12.phx.gbl...
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 ;)

Nov 18 '05 #18
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.

" Jason Shohet" <as****@hotmail.com> wrote in message
news:ep*************@TK2MSFTNGP12.phx.gbl...
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 ;)

Nov 18 '05 #19
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?

Nov 18 '05 #20
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?

Nov 18 '05 #21
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" <__******@yahoo.com> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
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?

Nov 18 '05 #22
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" <__******@yahoo.com> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
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?

Nov 18 '05 #23
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?


Nov 18 '05 #24
#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" <__******@yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
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?
>
>
>



Nov 18 '05 #25
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.
Nov 18 '05 #26
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"

"jason" <as****@hotmail.com> wrote in message
news:e3**************************@posting.google.c om...
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.

Nov 18 '05 #27
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" <uc*@ftc.gov> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <__******@yahoo.com> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
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?


Nov 18 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.