How can a custom control set the value of a control on the parent page?

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

Hello,

If I have a page that contains (say) a Literal control and a custom
control, can the code in the custom control set the value of the
Literal?

I tried doing this in the custom control...

Literal litPageNo = (Literal)this.Parent.FindControl("litPageNo");

to get a reference to the Literal, but this came back null. I tried
removing the Parent, but that did the same.

How can this be done? TIA
 
You're breaking the notion of encapsulation by taking this approach. I'd
suggest having your control raise an event, the page then handles the event
and then does the update on its own control.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
Brock said:
You're breaking the notion of encapsulation by taking this approach. I'd
suggest having your control raise an event, the page then handles the event
and then does the update on its own control.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Or if the custom control *always* needs a literal control, add a
property to it. That way you've got the reference that you need.
 
You're breaking the notion of encapsulation by taking this approach. I'd
I realised this last night. I was still thinking in the Classic ASP way,
not the ASP.NET way. I still hadn't worked out the best way to pass the
info back, but...
Or if the custom control *always* needs a literal control, add a
property to it. That way you've got the reference that you need.

Well, the control generates some information that the parent page is
likely (but not forced) to use. A property is an ideal answer to that as
it allows the parent page to get the info without forcing it back, and
without insisting on it having the literal or capturing an event.

Thanks to both of you.
 
Back
Top