User Control and Variable Question

Discussion in 'ASP .Net' started by David Hearn, May 1, 2006.

  1. David Hearn

    David Hearn Guest

    I am creating a user control for use on several of my pages. I have some
    variables that are set on my pages where the user control will be used. I
    need my user control to automatically be able to pick up on these variables
    and read the values out of them when it is placed on one of these pages. How
    is this accomplished? I know I could call a function that is in my user
    control and pass it the variable but that would take extra coding for each
    page that the user control is placed on and that sort of defeats the purpose
    of using them. This needs to be automatic.

    Thanks in advance!
    David Hearn, May 1, 2006
    #1
    1. Advertising

  2. In that case your pages that use this control need to inherit from some sort
    of base page, or implement some sort of interface. There would then be
    methods common to the base page or interface, that the user control could
    call. So it would cast its parent page to this base page or interface type,
    then access the appropriate property to return the variable value.
    That means that a page that doesn't inherit from your base class or
    implement the interface would not be able to host the user control.

    Another way is to have the a base page class, that has the code in it to set
    all the right properties on the user control, or call some method to set
    these variables. That way you only write it once. So you define the
    variables in the base page class, and write a method to set them that the
    base page calls in page_load or whenever is appropriate. Then, the
    descendent pages have to set the variable values, but don't have to do it
    for the user control because the base page method will do it.

    Either way, this is going to require extra coding though.

    "David Hearn" <> wrote in message
    news:...
    >I am creating a user control for use on several of my pages. I have some
    >variables that are set on my pages where the user control will be used. I
    >need my user control to automatically be able to pick up on these variables
    >and read the values out of them when it is placed on one of these pages.
    >How is this accomplished? I know I could call a function that is in my user
    >control and pass it the variable but that would take extra coding for each
    >page that the user control is placed on and that sort of defeats the
    >purpose of using them. This needs to be automatic.
    >
    > Thanks in advance!
    >
    Marina Levit [MVP], May 1, 2006
    #2
    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. Tom Rowton
    Replies:
    2
    Views:
    643
    Tom Rowton
    Aug 1, 2003
  2. vatech1993
    Replies:
    4
    Views:
    5,673
    =?Utf-8?B?U8Opcmdpbw==?=
    Dec 11, 2004
  3. Replies:
    0
    Views:
    427
  4. Mad Scientist Jr
    Replies:
    0
    Views:
    625
    Mad Scientist Jr
    Mar 22, 2006
  5. 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
Loading...

Share This Page