473,387 Members | 1,798 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,387 software developers and data experts.

Referencing a parent control from a child?

I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.
In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.
Jul 30 '06 #1
4 2047
Steve Hershoff schrieb:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.
In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.

Hello,

first I would set a breakpoint to this line and check what type of
object this.Parent is. Maybe it's a Panel .. or some other Container...

....
parent = (MyParentControl) this.Parent
....
Jul 30 '06 #2
If it doesn't absolutely "have to be" abstract, that's another area that
could cause this problem.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Steve Hershoff" wrote:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.
In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.
Jul 30 '06 #3
Easiest way I can think of is:

ParentControlType theParent=null;
Control tempControl=this;
while((!tempControl is ParentControlType)&&(!tempControl is Page))
tempControl=tempControl.Parent;
if(tempControl is ParentControlType)
theParent = (ParentControlType)tempControl;


"Steve Hershoff" wrote:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.
In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.
Jul 31 '06 #4
This is great-- just what I was looking for. Thanks very much, and thanks
to everyone who replied!

-Steve
"David Jessee" <Da*********@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.com...
Easiest way I can think of is:

ParentControlType theParent=null;
Control tempControl=this;
while((!tempControl is ParentControlType)&&(!tempControl is Page))
tempControl=tempControl.Parent;
if(tempControl is ParentControlType)
theParent = (ParentControlType)tempControl;


Jul 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sreddy | last post by:
I am trying to write a sql query on self referencing table. Just to brief ..Database is related to a Hiring department of the Qwest company. I need to generate a Report used by in HR...
2
by: Reply Via Newsgroup | last post by:
Folks, Windows, and refering to them (be they in seperate frames or in a popup) always catch me... There is parent, top, self, the name of the window itself and where it sits when refering to...
25
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I...
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
2
by: George | last post by:
I am just moving from VB to C# so I hope this isn't going to be too obvious... I am trying to reference a control (status bar) on an MDI parent from a child form and can't seem to get the syntax...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
2
by: libsfan01 | last post by:
Hi all How do u reference the id (and also now i think about it the name) of the enclosing div tag of a form element such as a button or div tag (without specifying its name)? I've tried:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.