How to refer Page level variable from a control?

Discussion in 'ASP .Net Building Controls' started by Oleg Slyuasrchuk, Dec 15, 2003.

  1. Hi,

    I have a composite control inherited from System.Web.UI.Control.

    Inside of this control I need to refer to a Page level variable decalared
    as
    public string channelStructure.

    In a debugger I see value of this variable as :

    ((CmsFormEditor.EditForm)(((ASP.EditForm_aspx)(((System.Web.UI.Page)((this.P
    age))))))).channelStructure

    As one can guess, ASP.EditForm_aspx is class created by ASP.NET runtime from
    the page EditForm.aspx
    where I place the control and this.Page is starting point to find the
    variable.

    Since I'm trying to write a generik control, I don't want to refer to
    specific page name.
    So, I need more generic way to refer the Page level variable.

    Any help is appreciated.

    Oleg
    Oleg Slyuasrchuk, Dec 15, 2003
    #1
    1. Advertising

  2. Why is your control trying to reference a variable on the page. Its supposed
    to be the other way round.

    "Oleg Slyuasrchuk" <> wrote in message
    news:#...
    > Hi,
    >
    > I have a composite control inherited from System.Web.UI.Control.
    >
    > Inside of this control I need to refer to a Page level variable

    decalared
    > as
    > public string channelStructure.
    >
    > In a debugger I see value of this variable as :
    >
    >

    ((CmsFormEditor.EditForm)(((ASP.EditForm_aspx)(((System.Web.UI.Page)((this.P
    > age))))))).channelStructure
    >
    > As one can guess, ASP.EditForm_aspx is class created by ASP.NET runtime

    from
    > the page EditForm.aspx
    > where I place the control and this.Page is starting point to find the
    > variable.
    >
    > Since I'm trying to write a generik control, I don't want to refer to
    > specific page name.
    > So, I need more generic way to refer the Page level variable.
    >
    > Any help is appreciated.
    >
    > Oleg
    >
    >
    Alessandro Zifiglio, Dec 16, 2003
    #2
    1. Advertising

  3. Thanks for the answer.

    I DO need to access a variable that holds a property that will be common
    for all of my controls on the page.
    For simplicity, it can be page's virtual path (maybe modified in some
    way). This value will be set up dynamically (calculated) at page level.

    Oleg

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Oleg Slyusarchuk, Dec 16, 2003
    #3
  4. Oleg Slyuasrchuk

    Kelly Leahy Guest

    >Since I'm trying to write a generik control, I don't
    want to refer to
    >specific page name.
    >So, I need more generic way to refer the Page level

    variable.
    >


    I agree with the other posting. It should be the other
    way around, no control should ever be able to access a
    page level variable on the page that contains it.
    Generally, when you are writing a custom control that
    needs information from the page, it should provide an
    event with an out or ref variable (ByRef in VB) so that
    the page can catch the event and provide the necessary
    information. However, that's not possible, as far as I
    can tell in the .NET event framework, so instead you need
    a custom EventArgs object. For instance, see the
    ImageClickEventArgs in the ImageButton control. You
    would define your own such public type, and make it the
    second argument for the delegate declaration for the
    event handler type (see ImageClickEventHandler). In your
    custom EventArgs object, you would expose a property that
    allows the containing page to provide the information you
    need (be setting properties on the custom EventArgs
    object).

    I'm sorry if this sounds complicated, but it really is
    the best way to get information from the page.
    Alternatively, you can require the page developer to
    provide the information as a property of your control and
    keep it synchronized whenever it changes, but that would
    be messy.

    Feel free to give more details about the nature of the
    variable you need to access and perhaps we can provide
    more assistance...

    Kelly Leahy

    For instance, if your custom control needs information
    for a property called X, you would expose an event called
    GetX with a signature such as GetX(ref X as object)
    Kelly Leahy, Dec 18, 2003
    #4
  5. Oleg Slyuasrchuk

    Kelly Leahy Guest

    >For instance, if your custom control needs information
    >for a property called X, you would expose an event

    called
    >GetX with a signature such as GetX(ref X as object)
    >.
    >


    Sorry, that last stuff somehow made it's way in, but it
    shouldn't be there... I thought I deleted it from the
    message.

    Hope it didn't confuse too much...
    Kelly Leahy, Dec 18, 2003
    #5
  6. Thank you, kelly

    I posted more detailed message "Communications between controls" in
    microsoft.public.dotnet.framework.aspnet.webcontrols

    My idea is to make controls self-contained and not force
    developers that are going to use my controls to code anything.
    I undestand that I make assumptions about existence of another
    control(s) on the page, but this is situation easily catchable.

    Oleg



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Oleg Slyusarchuk, Dec 18, 2003
    #6
    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. Joe Fallon

    User Control needs to refer to Page

    Joe Fallon, Feb 12, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,345
    Joe Fallon
    Feb 12, 2004
  2. Replies:
    2
    Views:
    428
  3. =?Utf-8?B?Sm9l?=

    page-level vs control-level enableViewState

    =?Utf-8?B?Sm9l?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,906
    S. Justin Gengo
    Oct 26, 2005
  4. pabbu
    Replies:
    8
    Views:
    707
    Marc Boyer
    Nov 7, 2005
  5. Jerry Qu
    Replies:
    1
    Views:
    445
    lee atkinson
    Feb 20, 2009
Loading...

Share This Page