Use profile service in ASP. NET Atlas to save user information
Http://me.dflying.net/2006/04/using-profile-service-in-aspnet-atlas.html
Author: dflying Chen (http://dflying.cnblogs.com /)
We know that ASP. NET 2.0 provides built-in profile support to store users' personal information. Atlas expands this function in Ajax mode on the client. Using the profile function in Atlas, you can access your profile data in JavaScript on the client side, modify the data on the client side, and submit the modified data back to the server for storage as appropriate, all these operations are completed using Ajax.
The profile object in Atlas is an instance of the SYS. _ profile class. SYS. Profile represents the profile of the current user. Atlas also provides a client control profile that you can use to bind other Atlas controls to the current profile.
The sys. Profile object has the following attributes:
1. autosave: Boolean value, indicating whether to automatically submit the modified profile data back to the server.
2. initialdata: the initial profile data, which will be transmitted along with the page to the client.
3. isdirty: Boolean value, indicating whether the current profile data has been modified and has not been submitted to the server.
4. propertynames: the set of name-value, which stores the current profile data. For example, to access the firstname attribute in profile, you can use SYS. profile. properties. firstname or SYS. profile. properties ["firstname.
There are also the following methods:
1. Load: Get the profile data of the current user from the server.
2. Save: Submit the profile data of the client user to the server.
The following events:
1. Loaded: triggered when profile data is obtained.
2. Saved: triggered when the profile data is submitted completely.
Using the above attributes/methods/events provided by the SYS. Profile object, we can easily write highly customizable Atlas applications.Program. Now let's use a sample program to familiarize ourselves with the use of SYS. profile.
This instance program allows us to retrieve and display our profile data, and submit it back to the server for storage after modification. The defined profile is very simple. See the following web. config definition:
<Profile>
<Properties>
<Add name = "firstname"/>
<Add name = "lastname"/>
</Properties>
</Profile>
We also need to enable the corresponding service in Web. config:
<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>
Also, in the Microsoft. Web segment:
<WebServices enablebrowseraccess = "true"/>
<Profileservice enabled = "true" setproperties = "firstname; lastname" getproperties = "firstname; lastname"/>
Note that the firstname and lastname attributes are visible on the client through the above settings. Then, we add several users for testing in the membership database. You can set and add users through ASP. NET web site administration tool.
So far as the configuration file works, we create an ASP. NET page and add a scriptmanager control to it:
<Atlas: scriptmanager id = "scriptmanager" runat = "server"/>
Add an HTML table containing two inputs to display and modify the user's profile data:
<Table id = "tbprofile" style = "border: 1px solid black;">
<Tr align = "center">
<TD colspan = "2"> <B> your profile values </B> </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>
There is also a button to trigger the save operation:
<Input type = "button" id = "Update" value = "update! "/>
In this example, for the sake of simplicity, we only use an ASP. NET Server Control login to Implement User Login, which will trigger a PostBack. If you need to log on using Ajax, refer to using membership in ASP. NET atlas for authentication.
<Asp: Login ID = "login1" runat = "server">
</ASP: Login>
Below are some JavascriptCodeTo control events and SYS. Profile objects:
Function onload (){
SYS. profile. Saved. Add (onsavecomplete );
}
Function onsave (){
SYS. profile. Save ();
}
Function onsavecomplete (){
Alert ('the profile has been saved .');
}
Below is the XML script of Atlas. We can see that after the page is loaded, the onload () method is executed, and the event processing function after the profile is saved is added. When the update button is clicked, the onload () method is executed and the client profile is submitted back to the server.
<SCRIPT type = "text/XML-script">
<Page xmlns: script = "http://schemas.microsoft.com/xml-script/2005">
<Components>
<Profile id = "profilecontrol" autosave = "false"/>
<Textbox id = "txtfirstname">
<Bindings>
<Binding datacontext = "profilecontrol" datapath = "firstname" property = "text" direction = "inout"/>
</Bindings>
</Textbox>
<Textbox id = "txtlastname">
<Bindings>
<Binding datacontext = "profilecontrol" datapath = "lastname" property = "text" direction = "inout"/>
</Bindings>
</Textbox>
<Button id = "Update">
<Click>
<Invokemethod target = "application" method = "onsave"/>
</Click>
</Button>
<Application ID = "application" load = "onLoad">
</Application>
</Components>
</Page>
</SCRIPT>
We have set the binding direction of the above binding to inout, so that changes to the data in the input will be automatically reflected to the actual profile data.
OK. In the browser, test the page loading:
After logging on to dflying, you can see that two inputs have filled with my profile information:
Modify the two inputs and upgrade me to Bill Gates. Well, it takes less than one second. (Yy ......)
The above sample program can be downloaded here: http://files.cnblogs.com/dflying/AtlasProfileTest.zip