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

Discussion in 'ASP .Net' started by Alan Silver, Aug 8, 2005.

  1. Alan Silver

    Alan Silver Guest

    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

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 8, 2005
    #1
    1. Advertising

  2. Alan Silver

    Brock Allen Guest

    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



    > 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
    >
    Brock Allen, Aug 8, 2005
    #2
    1. Advertising

  3. Alan Silver

    Damien Guest

    Brock Allen wrote:
    > 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.
    Damien, Aug 9, 2005
    #3
  4. Alan Silver

    Alan Silver Guest

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


    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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    542
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Kay
    Replies:
    1
    Views:
    374
  3. Noel Dolan
    Replies:
    0
    Views:
    216
    Noel Dolan
    Jul 18, 2004
  4. Replies:
    0
    Views:
    125
  5. PD
    Replies:
    6
    Views:
    699
Loading...

Share This Page