Home Contact

Using Profile Service in ASP.NET Atlas

As we know, ASP.NET 2.0 provides us a build-in feature called profile for storing the users’ personalization data. Atlas extends this feature on the client side in the AJAX way. Within its help, you can access user profile data on the client side using JavaScript code and commit the changes back to server when needed.

The Sys._Profile class is the client side Atlas class represents the profile while there’s only one instance created automatically by Atlas framework, Sys.Profile, which represents the current user’s profile. Atlas also provides a client side control Profile, which enables you to bind other Atlas controls to the current profile declaratively.

The Sys.Profile object has several properties:

  1. autoSave: Boolean value indicate whether the changed to profile data will be commit to server automatically.
  2. initialData: The initial profile data coming with the page source code.
  3. isDirty: Boolean value indicate whether current profile data is modified.
  4. propertyNames: Name-value collection represents the current profile data. E.g., you may use Sys.Profile.properties.FirstName or Sys.Profile.properties[”FirstName”] to access the FirstName profile property.

And following methods:

  1. load: Loads user’s profile data from server.
  2. save: Saves current user’s profile data back to server.

Also events:

  1. loaded: Gets fired when loading profile completed.
  2. saved: Gets fired when saving profile completed.

By using properties/methods/events above provided by the Sys.Profile object, we may easily build full customizable Atlas applications. Now let’s go through a demo about the Atlas profile operations.

In this demo, the profile data is very simple, just a first name and a last name, defined below in web.config:

<profile>

  <properties>

    <add name=FirstName />

    <add name=LastName />

  < /properties>

< /profile>

Then we need following sections in web.config to enable Atlas profile service:

<configSections>

  <sectionGroup name=microsoft.web type=Microsoft.Web.Configuration.MicrosoftWebSectionGroup>

    <section name=webServices type=Microsoft.Web.Configuration.WebServicesSection requirePermission=false/>

    <section name=profileService type=Microsoft.Web.Configuration.ProfileServiceSection requirePermission=false/>

  < /sectionGroup>

< /configSections>

Still, in the microsoft.web section, we need:

<webServices enableBrowserAccess=true/>

<profileService enabled=true

                setProperties=FirstName;LastName

                getProperties=FirstName;LastName />

Notice we make the FirstName and LastName properties available on the client side by above sections. Then you have to add at least one user in your database. To do this, you may use the ASP.NET Web Site Administration Tool.

Ok for the configurations, let’s create an ASP.NET page, and add a ScriptManager control:

<atlas:ScriptManager ID=”scriptManager” runat=”server” />

Then we added an html table with two inputs shows the profile properties:

<table id=”tbProfile” style=”border: 1px solid black;”>

    <tr align=”center”>

        <td colspan=”2″><b>Your Profile Valuesb>< /td>

    < /tr>

    <tr>

        <td>First name:< /td>

        <td><input type=”text” id=”txtFirstName” />< /td>

    tr>

    <tr>

        <td>Last name:< /td>

        <td><input type=”text” id=”txtLastName” />< /td>

    < /tr>

< /table>

And a button for updating:

<input type=”button” id=”update” value=”Update!” />

In this demo, for user login, we just simply add an ASP.NET server control Login to the page and it leads a PostBack. But keep in mind we can also make it on the client side in the AJAX way.

<asp:Login ID=”Login1″ runat=”server”>

< /asp:Login>

Then some JavaScript code is required for working on the Sys.Profile object.

function OnLoad() {

    Sys.Profile.saved.add(onSaveComplete);

}

function OnSave() {

    Sys.Profile.save();

}

function onSaveComplete() {

    alert(‘The profile has been saved.’);

}

And following is the Atlas XML scripts. We can find when the page is loaded, the OnLoad() function above gets executed which attach the event handler onSaveComplete() for saving profile completed. And when the update button is clicked, the OnSave() function abouve gets executed which save the changed profile to server.

<script type=”text/xml-script”>

“http://schemas.microsoft.com/xml-script/2005″>

       

“profilecontrol” autoSave=“false” />

           

            “txtFirstName” >

               

                    “profilecontrol” dataPath=“FirstName” property=“text” direction=“InOut” />

                < /bindings>

            < /textBox>

            “txtLastName” >

               

                    “profilecontrol” dataPath=“LastName” property=“text” direction=“InOut” />

                < /bindings>               

            < /textBox>