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

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
 
B

Brock Allen

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.
 
D

Damien

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.
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.
 
A

Alan Silver

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top