Access user control from other user control

Discussion in 'ASP .Net' started by Chris Zopers, Feb 20, 2008.

  1. Chris Zopers

    Chris Zopers Guest

    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:

    FirstUserControl first =
    (FirstUserControl)this.Parent.FindControl("FirstUserControl")

    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 ***
    Chris Zopers, Feb 20, 2008
    #1
    1. Advertising

  2. 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]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Chris Zopers" <> wrote in message
    news:OXK3I$...
    > 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:
    >
    > FirstUserControl first =
    > (FirstUserControl)this.Parent.FindControl("FirstUserControl")
    >
    > 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, Feb 21, 2008
    #2
    1. Advertising

  3. Chris Zopers

    David Bunge Guest

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

    I have one user control declared as "public partial class
    controls_SecondaryDisabilities : System.Web.UI.UserControl" on a page
    with a second user control declared as "
    public partial class controls_StateList : System.Web.UI.UserControl".

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

    Thank you in advance.

    *** Sent via Developersdex http://www.developersdex.com ***
    David Bunge, Aug 5, 2008
    #3
  4. I assume both the UserControls are in the same page. If So, You can use
    public properties in both the UserControls and set them when needed.
    I have poseted a similar article with example code here
    http://ctrlf5.net/?p=53

    --

    Thank You,
    Nanda Lella,
    http://www.CtrlF5.net
    This Posting is provided "AS IS" with no warranties, and confers no rights.
    Nanda Lella[MSFT], Apr 27, 2009
    #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. Richard Dixson
    Replies:
    2
    Views:
    327
    Richard Dixson
    May 19, 2004
  2. Replies:
    0
    Views:
    481
  3. Chris Dollin
    Replies:
    8
    Views:
    339
    Eric Sosman
    Feb 8, 2007
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    530
    Jonathan Wood
    Feb 2, 2010
  5. Diego
    Replies:
    2
    Views:
    142
    Stefan Rusterholz
    Aug 16, 2008
Loading...

Share This Page