asp.netweb-user-controls

How to access web page public variable from user control in asp .net


How to access the web page public variable from user control in asp .net ?


Solution

  • Your page class derived from asp.net Page has this variable.

    Every control has a Page property. If you know that this control will be placed on a particular class derived from Page, you can always cast it to the proper type. Then you have access to the public variable.

    var propValue = ((MyControlBasePage)Page).MyProperty;
    

    Ensure that the base class for the pages on which you use this control is Page derived form MyControlBasePage.

    If you plan to use this on only one Page you could always directly use

    var propValue = ((MyPage)Page).MyProperty;
    

    But then, whats the user of a UserControl that you use on only one page.