challengeAccepted
challengeAccepted

Reputation: 7590

Accessing a UserControl from different Usercontrol

I am trying to use the already written code which accesses a control from an other control in the following code.

Controls_SearchFeaturedMerchants UCMerchant = (Controls_SearchFeaturedMerchants)this.Parent.FindControl("UCSearchFeaturedMerchants1");

I am wondering what this statement says. Can someone please give me some idea :)

Thanks in advance!

Upvotes: 0

Views: 5565

Answers (2)

EvanGWatkins
EvanGWatkins

Reputation: 1457

in plain text it is saying: Get the Parent Control of the current control and then find UCSearchFeaturedMerchants1 on said control. Cast the result into a Controls_SearchFeaturedMerchants.

Hope that sums it up for you

Upvotes: 0

JPReddy
JPReddy

Reputation: 65503

If I understood your question correctly here is the brief:

On a page (ContentPage) is hosting two user controls (UsrCtrl1, UsrCtrl2).

Now "UsrCtrl1" wanted to access some data in the "UsrCtrl2".

For that I'll write code like - "UsrCtrl1" parent is the "ContentPage" that page hosts "UsrCtrl2", so I'll first get the "UsrCtrl2" from the parent page with the following code:

this.Parent.FindControl("UsrCtrl2Name") -> this means current user control which is "UsrCtrl1" and Parent means is the "ContentPage" (it may be parent control or page) in that you are trying to find a contrl with Id "UsrCtrl2Name" (this is the id of the 2nd user control in the content page).

When you use FindContrl method it always returns base type UserControl and you need to cast it to your usercontrol in this case it is of type "UsrCtrl2".

I hope this is clear now.

Upvotes: 1

Related Questions