I know i know,
Here's the custom profileprovider class that should be placed in app_code
folder. you also need to amend web.config (that's actual place where you
define all properties)
-- begin MyProfileProvider.cs c# code --
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyProfileProvider
/// </summary>
public class MyProfileProvider : System.Web.Profile.ProfileProvider
{
public MyProfileProvider()
: base()
{
}
public override int
DeleteInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
throw new Exception("The method or operation is not implemented.");
}
public override int DeleteProfiles(string[] usernames)
{
throw new Exception("The method or operation is not implemented.");
}
public override int DeleteProfiles(System.Web.Profile.ProfileInfoCollection
profiles)
{
throw new Exception("The method or operation is not implemented.");
}
public override System.Web.Profile.ProfileInfoCollection
FindInactiveProfilesByUserName(System.Web. Profile.ProfileAuthenticationOption
authenticationOption, string usernameToMatch,
DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int
totalRecords)
{
throw new Exception("The method or operation is not implemented.");
}
public override System.Web.Profile.ProfileInfoCollection
FindProfilesByUserName(System.Web.Profile.ProfileAuthenticationOption
authenticationOption, string usernameToMatch, int pageIndex, int pageSize,
out int totalRecords)
{
throw new Exception("The method or operation is not implemented.");
}
public override System.Web.Profile.ProfileInfoCollection
GetAllInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption
authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int
pageSize, out int totalRecords)
{
throw new Exception("The method or operation is not implemented.");
}
public override System.Web.Profile.ProfileInfoCollection
GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption
authenticationOption, int pageIndex, int pageSize, out int totalRecords)
{
throw new Exception("The method or operation is not implemented.");
}
public override int
GetNumberOfInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption
authenticationOption, DateTime userInactiveSinceDate)
{
throw new Exception("The method or operation is not implemented.");
}
// ApplicationName property is used only if profile data
// depends on application instance - i.e. in case there are many
// applications and the data
public override string ApplicationName
{
get
{
return String.Empty;
}
set
{
}
}
public override SettingsPropertyValueCollection GetPropertyValues(
SettingsContext context, SettingsPropertyCollection collection)
{
System.Web.SessionState.HttpSessionState session =
HttpContext.Current.Session;
SettingsPropertyValueCollection settings =
new SettingsPropertyValueCollection();
foreach (SettingsProperty property in collection)
{
SettingsPropertyValue settingsPropertyValue =
new SettingsPropertyValue(property);
// remember this logic would only work if session
// keys are the same as properties name including grouping
object value = session[property.Name];
// set value for non null PropertyValues only in
// order to force default value defined in the web.config
if (value != null)
{
settingsPropertyValue.PropertyValue = value;
settingsPropertyValue.IsDirty = false;
settingsPropertyValue.SerializedValue = value;
}
settings.Add(settingsPropertyValue);
}
return settings;
}
public override void SetPropertyValues(SettingsContext context,
SettingsPropertyValueCollection collection)
{
System.Web.SessionState.HttpSessionState session =
HttpContext.Current.Session;
if (session == null)
return;
foreach (SettingsPropertyValue settingsPropertyValue in collection)
{
// i assume every sigle property defined for profile is stored in session,
// other wise you have to update session for selected items only
if (settingsPropertyValue.IsDirty)
{
if (settingsPropertyValue.PropertyValue == null)
{
session.Remove(settingsPropertyValue.Name);
}
else
{
session[settingsPropertyValue.Name] =
settingsPropertyValue.PropertyValue;
}
}
}
}
}
-- end code --
-- web.config changes--
<anonymousIdentification enabled="true"/>
<profile enabled="true" defaultProvider="MyProfileProvider"
automaticSaveEnabled="true">
<providers>
<add name="MyProfileProvider" type="MyProfileProvider"/>
</providers>
<properties>
<group name="RoleA">
<add name="MBCount" type="System.Int32" defaultValue="0" readOnly="false"
allowAnonymous="true"/>
</group>
</properties>
</profile>
-- end web.config changes --
-- how to use it on the page --
<asp:Button runat="server" ID="btn" Text="Postback!" />
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// you have to call Profile.Save()
// everytime you change properties to force values
// to be updated in underlying data storage which
// on our case is session. the reason for that is
// very simple - profileprovider.setpropertyvalues
// is called at the final stage of page execution
// after session state is serialized and destroyed
// therefore is not acessible. remember save should
// be called onnce per execution i.e. after you set
// all properties you want.
if (IsPostBack)
{
Profile.RoleA.MBCount++;
Profile.Save();
}
else
{
Profile.RoleA.MBCount = 3;
Profile.Save();
}
Response.Write(Profile.RoleA.MBCount);
}
</script>