[C #. Net] Asp.net status management (7) People-oriented Profile

Source: Internet
Author: User

7.1 use profile to create personalized pages
A user-friendly website provides many personalized choices for users. For example, you can select your favorite website style and choose whether to automatically Pop Up A Message notification. The data needs to be saved after the user closes the browser. Therefore, you can only select a database to save the data. It is easier for users who have logged on to the database. We can store the user name and user selection in the database. How can we save the user selection for non-logged-on users (anonymous users? The unique method can only assign an ID to the user as the session, and store the ID in the cookie (of course, it can also be placed in the URL ), then, save some configuration information related to this ID in the database.

ASP. NET 2.0 provides a profile mechanism to help us complete similar functions. Profile not only supports logon users but also anonymous users. The stored data can be of any serializable type. You can easily save personalized data without writing a line of code. We have configured a web. config file, as shown below.

<? XML version = "1.0"?>

<Configuration>

<System. Web>

<Anonymousidentification enabled = "true"/>

<Profile automaticsaveenabled = "true">

<Properties>

<Group name = "UI">

<Add name = "forecolor" defaultvalue = "black" allowanonymous = "true" type = "string"/>

<Add name = "enablebold" defaultvalue = "false" allowanonymous = "true" type = "bool"/>

</Group>

<Group name = "userinfo">

<Add name = "username" defaultvalue = "" allowanonymous = "false" type = "string"/>

<Add name = "userage" defaultvalue = "0" allowanonymous = "false" type = "int"/>

</Group>

</Properties>

</Profile>

<Compilation DEBUG = "true"/>

<Authentication mode = "forms"/>

</System. Web>

</Configuration>

· <Anonymousidentification enabled = "true"/> indicates that profile is also enabled for anonymous users. The system assigns an ID composed of random strings to anonymous users.

· <Profile automaticsaveenabled = "true"> indicates that the Profile settings are automatically saved to the database at the end of the page request.

· In <Properties>, the profile format is formally defined. We use the <group> label to divide the profile into two groups, <group name = "UI"> and <group name = "userinfo">.

· The real profile in each <group>. Name indicates the profile name, defaultvalue indicates the default value, allowanonymous indicates whether anonymous users can use it, and type indicates the data type.

· <Authentication mode = "forms"/> indicates that form authentication is enabled for the system (this is an ASP. NET authentication method, which will be detailed in later sections ),

Then, we add some controls for the page to personalize the page.

N text color:

<Asp: dropdownlist id = "ddl_textcolor" runat = "server">

<Asp: listitem selected = "true"> black </ASP: listitem>

<Asp: listitem> blue </ASP: listitem>

<Asp: listitem> Red </ASP: listitem>

</ASP: dropdownlist>

<Asp: checkbox id = "cb_isblod" runat = "server" text = "bold"/>

<Br/>

<Asp: button id = "btn_savesettings" runat = "server" text = "Save personalized settings" onclick =

"Btn_savesettings_click"/> & nbsp;

<Asp: button id = "btn_login" runat = "server" onclick = "BTN _

Login_click "text =" Logon "/> <br/>

<Asp: Label id = "lab_text" runat = "server" font-names ="

Body "font-size =" 50pt "Height =" 77px "text =" happy programming "width =

"293px"> </ASP: Label>

The effect is 12-24.

In the drop-down box, you can set the text color to black, blue, or red. You can use the check box to set whether the text is in bold. Click "Save personalized settings" to save the settings.

Protected void btn_savesettings_click (Object sender, eventargs E)

{

Profile. UI. forecolor = ddl_textcolor.selectedvalue;

Profile. UI. enablebold = cb_isblod.checked;

Applyuisettings ();

}

I was surprised to see that we only configured profile information in the web. config file. How can I directly access a strong profile in the code? In fact, the system will generate a temporary code file under app_code, as shown in 12-25.

 

Figure 12-25 temporary code file generated by the system

Here, we have customized an applyuisettings () method to apply a style to tags on the page.

Private void applyuisettings ()

{

Lab_text.forecolor = color. fromname (profile. UI. forecolor );

Lab_text.font.bold = profile. UI. enablebold;

Ddl_textcolor.selectedvalue = profile. UI. forecolor;

Cb_isblod.checked = profile. UI. enablebold;

}

At the same time, in order to ensure that the page can be displayed according to the user's personalized configuration during the first loading, we also need to apply the configuration during page_load.

Protected void page_load (Object sender, eventargs E)

{

If (! Ispostback)

{

Applyuisettings ();

}

}

Now open the page and set the style to blue text and bold, as shown in 12-26.

The first operation is slow because the system is generating a database for saving information for you. By default, the SQL express database is used, and the files are stored in the app_data folder of the website, as shown in 12-27.

Figure 12-26 save your personalized settings Figure 12-27 aspnetdb database file under app_data

When the page is closed, you can see that the page still maintains the original style settings. You may ask, how does the system know that we are the original user? In fact, the system generates an ID string for anonymous users and stores it in cookies. During page loading, the system reads data from the database to fill the profile. After testing the profile of an anonymous user, we can add a click event processing method for the logon button.

Protected void btn_login_click (Object sender, eventargs E)

{

Formsauthentication. setauthcookie ("test", false );

Response. Redirect (request. Path );

}

Here, we assume that a user named test has logged on to the system and redirected the page to this page. Then, we add a placeholder control on the page, where some controls allow users to enter profile information and log out.

<Asp: placeholder id = "ph_userinfo" runat = "server">

Name: <asp: textbox id = "tb_name" runat = "server"> </ASP: textbox>

Age: <asp: textbox id = "tb_age" runat = "server"> </ASP: textbox>

<Br/>

<Asp: button id = "btn_saveuserinfo" runat = "server" text = "Save User Information" onclick =

"Btn_saveuserinfo_click"/> & nbsp;

<Asp: button id = "btn_logout" runat = "server" onclick = "btn_logout_click" text ="

Output "/>

</ASP: placeholder>

The click event of the Save User information button is implemented as follows:

Protected void btn_saveuserinfo_click (Object sender, eventargs E)

{

Profile. userinfo. Username = tb_name.text;

Profile. userinfo. userage = int. parse (tb_age.text );

Getuserinfo ();

}

Here, getuserinfo is used to display User Information (login user information and non-Login User information ).

Private void getuserinfo ()

{

If (user. Identity. isauthenticated)

{

Ph_userinfo.visible = true;

Response. Write ("Current Login User:" + User. Identity. Name + "<br/> ");

Response. Write ("profile associated users:" + profile. username + "<br/> ");

Response. Write ("profile. userinfo. Username:" + profile. userinfo. username +

"<Br/> ");

Response. Write ("profile. userinfo. userage:" + profile. userinfo. userage +

"<Br/> ");

}

Else

{

Ph_userinfo.visible = false;

Response. Write ("profile associated users:" + profile. username + "<br/> ");

}

}

The click event of the Logout button is implemented as follows:

Protected void btn_logout_click (Object sender, eventargs E)

{

Formsauthentication. signout ();

Profile. userinfo. userage = 0;

Profile. userinfo. Username = "";

Response. Redirect (request. Path );

}

At the same time, modify page_load to display the user information when the page is loaded for the first time.

Protected void page_load (Object sender, eventargs E)

{

If (! Ispostback)

{

Applyuisettings ();

Getuserinfo ();

}

}

Open the page again, as shown in figure 12-28.

We can see that for anonymous users, the profile. Username attribute is a random string, and the "login" button is clicked 12-29.

Figure 12-28 an anonymous user is associated with a random string as the ID. Figure 12-29 the user information is displayed after the logon button is clicked.

Now we can see that the username associated with the profile is the logon user test. Now, set name to "Xiao Zhu", age to 24, and click "Save User Information. Then, change the style to Red bold and click "Save personalized settings. 12-30.

N if you close the window and re-open it, you will find that the personalized settings are still in the blue bold format originally saved in the anonymous state. Therefore, the logon status is no longer available. Compare the current ID with the previous ID. After clicking the "Log on" button again, the user test's personalized settings are loaded on the page.

7.2 profile Summary
Profile is much more than that. Here we just give a brief introduction to it. Before we end, we will end the discussion of profile with several questions in section 1.

· Physical storage location. Client Cookie/URL and server database.

· Storage type restrictions. Serializable type.

· Range of status usage. The context of the current request, which is independent of each user.

· Storage size limit. It is not recommended to store frequently written data to the profile at any size.

· Lifecycle. The lifecycle is the same as that of the associated cookie.

· Security and performance. Data is always stored on the server, which is highly secure but not easy to store too much data.

· Advantages and disadvantages and precautions. You can easily save the settings of users (anonymous users and logged-on users.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ojekleen/archive/2007/10/23/1839430.aspx

Related Article

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.