Referencing parent page controls

J

John Spiegel

Hi all (again),

Next question: I have a Web user control contained in a page. How would I
go about referencing the other objects in the page from within the control?
One way seems to use something like:

this.Parent.FindControl("pnlExample").Visible = true;

or if this is to be referenced with some frequency, store it to a field in
the control then reference that. Is there a better way?

TIA,

John
 
S

Shiva

Another way is to make the parent's control to be public and access it
directly:

(<parent_page_class>)(this.Parent).pnlExample.Visible = true;

assuming pnlExample is made as a public member.

Hi all (again),

Next question: I have a Web user control contained in a page. How would I
go about referencing the other objects in the page from within the control?
One way seems to use something like:

this.Parent.FindControl("pnlExample").Visible = true;

or if this is to be referenced with some frequency, store it to a field in
the control then reference that. Is there a better way?

TIA,

John
 
J

John Saunders

Shiva said:
Another way is to make the parent's control to be public and access it
directly:

(<parent_page_class>)(this.Parent).pnlExample.Visible = true;

assuming pnlExample is made as a public member.

Bad idea. This ties the user control and the page together. It's almost
never a good idea to expose public fields.

John Saunders
 

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

Top