views:

45

answers:

2

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.

+2  A: 

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.

bszom
+1  A: 

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: http://west-wind.com/WebLog/posts/5127.aspx

Alex de Groot
I tried this.page.findcontrol but somehow it wouldn't find my webcontrol there. I've now set a sc:placeholder on the searchfilter control and can now just use parent.findcontrol("nameofcontrol"). Thx for the reply here tho.
Younes
As I wrote, you need to have the recursive one. Not the default :)
Alex de Groot
I solved this by just placing my searchresults in a sc:placeholder on the searchfilter. I can now access the labels.
Younes