Reputation: 4823
I'm using one Sublayout (Sitecore) and have a placeHolder that currently holds 2 webcontrols. I want to access the Label from one Webcontrol to the other Webcontrol. Do i have to find the Label recursively or can i just access the Label on another way? I tried different methods like:
this.Page.Findcontrol this.Parent.Findcontrol etc..
Label lblSearchTerm = (Label)this.Parent.FindControl("lblSearchTerm");
Label lblResults = (Label)this.Parent.FindControl("lblResults");
Wouldn't give me any result as being Label lblSearchTerm = null. I hope someone here knows a way to fix this.
Upvotes: 1
Views: 531
Reputation: 881
Why not use this.Page.FindControl? Of course this one isn't performing the search recursively. But then you could use the code you can find over here.
Upvotes: 1
Reputation: 1235
I'm not familiar with Sitecore, but if I understand your question correctly then your labels are child controls of one of the webcontrols. If this is true, then to find those labels you need to find their parent (ie: the webcontrol) first.
Assume the following control hierarchy:
Page
> WebControl1
> Label
> WebControl2
> Label
> Button
If you are trying to access the label on WebControl2 from WebControl1, then
Label lblSearchTerm = (Label)this.Parent.FindControl("lblSearchTerm");
will not work, because this.Parent will return the Page object, and the label you are looking for is not a child of the Page. Instead it is a child of 'WebControl2', which itself is a child of the Page. So something like the following should work:
Label lblSearchTerm = (Label)this.Parent.FindControl("WebControl2").FindControl("lblSearchTerm");
Really it would be better if the label's owner were the only one to modify it, but that is another discussion entirely.
Upvotes: 4