473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2789

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jason Shohet | last post by:
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...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.