Brian
Brian

Reputation: 1469

How can I find and interact with a user control on a page from a separate user control?

I have an aspx page that has two different user controls. I want to find user control A and be able to set properties, etc., from user control B.

I was thinking I could do something like this:

Dim CMFilters As Control = Me.Parent.FindControl("CMFilters")

...but that doesnt work to be able to set properties and call methods. I somehow need to get the user control and and declare it as that user control type.

Upvotes: 1

Views: 327

Answers (2)

David
David

Reputation: 73574

There's an article here explaining how to do it.

I would like to note that this is not considered a good design. This is the type of thing that's referred to as a "code smell". As a general rule of thumb, objects should be designed so that they are unaware of other objects, and can function independently of other objects.

A better approach would be to simply let the objects do what they do independently, and let the page class handle the interactions, since each is a child element of the page.

This design is listed as a code smell here:

http://www.codinghorror.com/blog/2006/05/code-smells.html

Inappropriate Intimacy

Watch out for classes that spend too much time together, or classes that interface in inappropriate ways. Classes should know as little as possible about each other.

Upvotes: 0

Uwe Keim
Uwe Keim

Reputation: 40736

You should not make control A dependent of control B.

Instead, read and write the properties of both controls from the page that contains the controls.

So expose all properties you want to set in both controls A and B as public properties (read/write or read-only) and connect them e.g. in the Page_Load event of your page.

Upvotes: 2

Related Questions