Asynchronously calling the profile service of application services

Source: Internet
Author: User

In ASP. NET Ajax,

SYS. Services. profileservice provides a client proxy class for the personalized configuration file service,

So the applicationProgramYou can access the properties of the personalized configuration file through JavaScript.

For some usage of profile, you can refer to my blog.

Just like the authentication service and role service,

Let's start with how to use profile,

To use profile, you must first register the database of the website, that is, the 11 data tables,

If you use profile to access profile attributes,

Of course, you must declare these attributes first. You can declare these attributes in Web. config,

Some of these knowledge points must be learned if you do not understand them. My blog post will explain this part in detail,

You can refer

The following section defines the profile in Web. config and adds multiple profile attributes,

How to enable this profileservice

The profileservice is enabled in Web. config.

Here we will explain two attributes: writeaccessproperties and readaccessproperties,

Writeaccessproperties indicates the writable profile attribute,

Readaccessproperties indicates the readable profile attribute,

The above two values are set to = "*", indicating that all defined profile attributes are readable and writable,

If you do not define this,

For example, I define a constellation that is readable and writable,

Gender is readable and non-writable, while phone numbers are non-readable,

The following describes the profileservice class,

First

SYS. Services. profileservice. Load (propertynames,

Loadcompletedcallback,

Loadfailedcallback, usercontext)

Propertynames is a string array as a parameter. It contains the profile attribute to be loaded,

If you do not provide the propertynames parameter or the parameter is null,

AllRead PermissionProfile attribute field,

I will not describe the other three parameters. If you do not understand these parameters,

Then you have to look back and learn from my previous blog posts,

After successfully loading the profile, you can directly use the field properties to access the loaded configuration file,

The following describes the properties field.

SYS. Services. profileservice. properties. fieldname

Fieldname indicates the name of your profile attribute field,

Such as address, address, phone number, etc,

This properties field contains the profile data loaded using the load method,

Note that if you define a group-type attribute group in the Profile section,

The elements in properties can also be represented. You can access its sub-attributes through the elements,

For exampleSYS. Services. profileservice. properties. Phone. Mobile phone

Next we will introduce a very important method.Sava

SYS. Services. profileservice. Sava (propertynames,

Savacompletedcallback,

Savafailedcallback, usercontext)

The propertynames is

The usage of propertynames during the above interpretation of the load method is the same,

I will not explain the other two parameters,

This Sava method is used to save the properties of the configuration file specified by propertynames.

In addition to the methods and attributes described above

Defaultfailedcallback, defaloadloadcompletedcallback,

But I will not introduce it here, because they are all the same. I mean, it's boring to say it's not,

If you do not know anything, refer to msdn.

Here is the demo.

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">

Protected void page_load (Object sender, eventargs E)
{
// If the current user does not pass the verification, go to the logon page
If (User. Identity. isauthenticated = false)
{
System. Web. Security. formsauthentication. redirecttologinpage ();
}
Lblname. Text = user. Identity. Name;
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 30%;
Border: 1px solid # 8000ff;
Height: 314px;
}
# Btnsavaprofile
{
Width: 56px;
Text-align: right;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Scriptmanager Id =" Scriptmanager1 "Runat =" server ">
</ASP: scriptmanager>
<Div>
<Table class = "style1">
<Tr>
<TD>
User Name
</TD>
<TD>
<Asp: Label Id =" Lblname "Runat =" server "> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD>
Address
</TD>
<TD>
<Asp: Textbox Id =" Txtaddress "Runat =" server "> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Phone number
</TD>
<TD>
<Asp: Textbox Id =" Txtphone "Runat =" server "> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Constellation
</TD>
<TD>
<Asp: Textbox Id =" Txtconstellate "Runat =" server "> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD style = "text-align: Right">
< Input Id =" Btnsavaprofile "Type =" button "value =" save"
Onclick = "Return btnsavaprofile_onclick ()"/> & nbsp;
</TD>
<TD style = "text-align: Center">
< Input Id =" Btnreadprofile "Type =" button "value =" read"
Onclick = "Return btnreadprofile_onclick ()"/>
</TD>
</Tr>
<Tr>
<TD>
Current address
</TD>
<TD>
<Asp: Label Id ="Lbladdress "Runat =" server "> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD>
Current phone number
</TD>
<TD>
<Asp: Label Id =" Lblphone "Runat =" server "> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD>
Current Constellation
</TD>
<TD>
<Asp: Label Id =" Lblconstellate "Runat =" server "> </ASP: Label>
</TD>
</Tr>
</Table>
</Div>
</Form>

<Script language = "JavaScript" type = "text/JavaScript">
VaRLblphone= $ Get ('<% =Lblphone. Clientid %> ');
VaRLbladdress= $ Get ('<% =Lbladdress. Clientid %> ');
VaRLblconstellate= $ Get ('<% =Lblconstellate. Clientid %> ');
VaRTxtphone= $ Get ('<% =Txtphone. Clientid %> ');
VaRTxtaddress= $ Get ('<% =Txtaddress. Clientid %> ');
VaRTxtconstellate= $ Get ('<% =Txtconstellate. Clientid %> ');

// Save the user profile information
Function Btnsavaprofile_onclick () {
SYS. Services. profileservice. Properties. Phone. Mobile phone = Txtphone. value;
SYS. Services. profileservice. Properties. Constellation = Txtconstellate. value;
SYS. Services. profileservice. Properties. Address = Txtaddress. value;
// Use the Save method to save the specified profile file
// Here I set the first parameter propertynames to null
// Indicates that it will be saved in profileservice
// Set all profile attributes with read permission
SYS. Services. profileservice. Save (Null,
Savecompletedcallback ,
Savefailedcallback , "Load profile ");
}

FunctionSavecompletedcallback(Result, usercontext, methodname ){
// Refresh the displayed profile information
Btnreadprofile_onclick ();
}

FunctionSavefailedcallback(Error, usercontext, methodname ){
Alert ("exception information:" + error. get_message ());
}

// Read the user profile information
Function Btnreadprofile_onclick () {
// Use the load method to load the specified profile file
// Here I set the first parameter propertynames to null
// Indicates that it will be loaded in profileservice
// Set all profile attributes with read permission
SYS. Services. profileservice. Load ( Null ,
Loadcompletedcallback,
Loadfailedcallback, "load profile ");
}

Function Loadcompletedcallback (Result, usercontext, methodname ){
// The result returned by the load method indicates the number of attributes of the personalized configuration file.
// Read and display the data in the Profile
Lbladdress. innertext =
SYS. Services. profileservice. properties. address;
Lblphone. innertext =
SYS. Services. profileservice. properties. Phone. Mobile phone number;
Lblconstellate. innertext =
SYS. Services. profileservice. properties. constellation;
}

FunctionLoadfailedcallback(Error, usercontext, methodname ){
Alert ("exception information:" + error. get_message ());
}
</SCRIPT>

</Body>
</Html>

ThisCodeNow, let's see the results.

If you directly run this page, since no user has logged on,

So it will automatically jump to the specified page of loginurl defined in Web. config

 

Log On With Xiaozhen

After successful logon, the system will automatically jump to the page written above,

Then, press the "read" button to asynchronously read the profile of Xiaozhen.

Then, enter the profile attribute value to be modified in the text box.

Then press "save ".

You can see that the changes just made have also taken effect.

Cut down the image in the database.

The demo above mainly completes the load and save methods of profileservice,

As well as the properties field and some settings for related web. config,

The defaultfailedcallback attributes are not used,

However, these attributes are very easy to use. If you have the opportunity in the future, you can use them in a unified manner !!!

2010-2-11

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.