views:

244

answers:

2

I am creating a base class from which specific pages are going to be derived. That is, instead of inheriting my pages from System.Web.UI.Page, they're inheriting from MyPage (which in turn, inherits from System.Web.UI.Page).

However, I can't seem to be able to access the Profile property. Even though they both inherit from Page, I can only access the Profile properties if I'm at the actual page level.

I'm sure this is just my misunderstanding of the page life cycle, but is there a way to access the Profile from my custom class which is defined in App_Code?

+1  A: 

When you are in the page you have the ProfileCommon class available to you for accessing the profile. The profilecommon class is automatically generated by asp.net from you web.config profile settings during compilation of the web project.

In case you want to use the profile from app_code folder, you will have to use the profilebase class. Profilecommon that is available in the page also derives from this class.

Profilebase can be access like this

HttpContext.Profile or HttpContext.Current.Profile

To read a profile value you need to do the following

HttpContext.Profile.GetPropertyValue("propertyName");

To write a value to the profile you need to write

HttpContext.Profile.SetPropertyValue("propertyName", "propertyValue");
Bootcamp
A: 

You can access the strongly typed profile properties by casting it to the ProfileCommon type like this:

Dim Profile as ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)

Profile.MyProperty1 = "Testing"
Profile.MyProperty2 = "Cool"
NightOwl888