Access user control from other user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Zopers

    Access user control from other user control

    Hello,

    I have two UserControls on a aspx page. I need to read a property of the
    first UserControl from within the second UserControl. The UserControls
    only know about themselves, so I can't declare a variable of the other
    usercontrol's type. So I can't do something like this in the second
    UserControl:

    FirstUserContro l first =
    (FirstUserContr ol)this.Parent. FindControl("Fi rstUserControl" )

    Additionally I don't like to use the FindControl method, because it
    returns an Object.

    What's the best way to get to a usercontrol's properties from within an
    other user control?

    Greetings,
    Chris




    *** Sent via Developersdex http://www.developersdex.com ***
  • Eliyahu Goldin

    #2
    Re: Access user control from other user control

    In the first user control, you can declare a property of the type matching
    the second user control and set this property from the page containing both
    controls.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]




    "Chris Zopers" <test123test12@ 12move.nlwrote in message
    news:OXK3I$8cIH A.1208@TK2MSFTN GP03.phx.gbl...
    Hello,
    >
    I have two UserControls on a aspx page. I need to read a property of the
    first UserControl from within the second UserControl. The UserControls
    only know about themselves, so I can't declare a variable of the other
    usercontrol's type. So I can't do something like this in the second
    UserControl:
    >
    FirstUserContro l first =
    (FirstUserContr ol)this.Parent. FindControl("Fi rstUserControl" )
    >
    Additionally I don't like to use the FindControl method, because it
    returns an Object.
    >
    What's the best way to get to a usercontrol's properties from within an
    other user control?
    >
    Greetings,
    Chris
    >
    >
    >
    >
    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • David Bunge

      #3
      Re: Access user control from other user control

      How would you go about doing this? Could you post an example?

      I have one user control declared as "public partial class
      controls_Second aryDisabilities : System.Web.UI.U serControl" on a page
      with a second user control declared as "
      public partial class controls_StateL ist : System.Web.UI.U serControl".

      I need to get a value from the second user control (the StateList) into
      a variable in the first (the SecondaryDisabi lities). The value is a
      public string, so it is accessible. However, when I declare "protected
      controls_StateL ist ddlPartState;", the controls_StateL ist is not being
      found.

      Thank you in advance.

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      Working...