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:
- autoSave: Boolean value indicate whether the changed to profile data will be commit to server automatically.
- initialData: The initial profile data coming with the page source code.
- isDirty: Boolean value indicate whether current profile data is modified.
- 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:
- load: Loads user’s profile data from server.
- save: Saves current user’s profile data back to server.
Also events:
- loaded: Gets fired when loading profile completed.
- 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”>
< /bindings>
< /textBox>
< /bindings>
< /textBox>
>
< /click>
< /button>
< /application>
< /components>
< /page>
< /script>
Keep in mind that we set the binding directions to InOut in the above bindings, thus the modifications to the input will reflect to the actual profile data immediately.
Done! Let’s test in browser:
Page load:
Logged in, you can find the two inputs are filled with user’s profile data.
Upgrade me to Bill. Wow! It just works in less than 1 second. (Though in the real life it is almost impossible.)
The source code for the demo is available here:
WordPress database error: [Table 'me.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '4' AND comment_approved = '1' ORDER BY comment_date
5549 Comments »
RSS feed for comments on this post.
| TrackBack URI
You can also bookmark
this on del.icio.us or check the cosmos