Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variables 1

Status
Not open for further replies.

Muddmuse

Programmer
Jul 31, 2001
85
US
Fairly new to .net and OO and am confused about accessing properties of my aspx page inside usercontrols. I have 3 web sites all with identical structure. Each site has its own solution with separate namespaces. I also have a BasePage class that overwrites the Page class to initialize certain properties. I also have a "usercontrols" project that is referenced by each of the sites that holds the menus, headers, footers etc. My problem is that when the page loads I initialize my BasePage class but then when the usercontrols load I do not know how to access those properties. I simply do not know how to say "create a variable of type BasePage in the usercontrol and set it to the one that is already in memory". Any ideas?
 
Each user control class has a Page property. The Page property is a reference to the page that the control is on. So inside your user control class, you can access properties of the page, such as Page.IsPostBack.

If you know that the page is derived from a base class other than System.Web.UI.Page, you can cast the Page reference to it's base class.
Code:
[VB]
[green]'your base page class[/green]
Public Class BasePage
    Inherits System.Web.UI.Page

    Private _internal as String = "xyz"

    Public Property MyProperty() as String
        Get
            return Me._internal
        End Get
        Set(ByVal Value as String)
            Me._internal= Value
        End Set
    End Property
End Class

[green]'in your user control code behind[/green]
Dim myString as String
Dim myPage as BasePage
myPage = DirectCast(Page, BasePage)
myString = myPage.MyProperty

[C#]
[green]//base page definition[/green]
public class BasePage : System.Web.UI.Page
{
    string internalString = "xyz";
    
    public string MyProperty
    {
        get{return this.internalString;}
        set{this.internalString = value;}
    }
}

[green]//in user control code behind[/green]
    
string fromBase = ((BasePage)Page).MyProperty;

Greetings,
Dragonwell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top