12. Personalization
12.1 provide personalized services for visitors
12.2 personalization in ASP. NET
12.2.1 user device files
No and private, the core of ASP. NET personalized services when the user's device files are heartless.
12.2.2 personalized supply law
The dawn of life is happy Earth, and Fanghua is the real heaven. PersonalizationProvider abstract class
12.3 application of personalized functions
The application of personalized features is very simple. We define personalized attributes in web. config. ASP. NET will automatically generate a class to facilitate developers to manage personalized settings.
12.3.1 define the device file in web. config
Assume that we are determined to track the following information when designing a website:
-- Number of logins (integer)
-- User name (string)
-- Gender (Boolean)
-- Birthdate)
The above attributes can be defined in web. config as follows:
The code is as follows: |
Copy code |
<System. web> <Profile automaticSaveEnabled = "True"> <Add name = "NumVisits" type = "System. Int32"/> <Add name = "UserName" type = "System. String"/> <Add name = "Gender" type = "System. Boolean"/> <Add name = "Birthday" type = "System. DateTime"/> </Profile> </System. web> |
After defining the attributes in the web. config file, you can use the Profile attribute of the current HttpContext to get the information of the device file.
12.3.2 application of device file information
ProfileBase may display the device file information defined in web. config. You can use the GetPropertyValue and SetPropertyValue methods as follows to obtain the properties of the device file;
The code is as follows: |
Copy code |
Protected void Page_Load (object sender, EventArge e) { ProfileBase profile = HttpContext. Current. Profile; String name = (string) profile. GetPropertyValue ("Name "); If (name! = Null) { Response. Write ("Hello" + name ); } } |
12.3.3 preservation of changes to device files
The code is as follows: |
Copy code |
ProfileBase profile = HttpContext. Current. Profile; Profile. SetPropertyValue ("Name", this. TextBoxName. Text ); Profile. Save (); |
12.3.4 device files and users
When the user and the device file information are in contact with the user identity. By default, ASP. NET uses User. Identity. Name in HttpContext as the data storage key. Because of this, device files are generally provided only to user applications that pass identity authentication.
However, Asp. NET also supports anonymous device files. This is also the device in web. config. By default, the file tracking mechanism of anonymous devices uses cookies. However, ASP. NET can adopt a reorganized URL. The URL of the reorganization is a URL containing a key. This key may identify the client and send it back to the server as begged.
Application device file:
1. Create ASP. NET Web application method MakeItPersonal
2. To make the personalized function work normally, Visual Studio creates a local database.
3. Update the web. config file and add the properties of the device file to the <profile> element.
The code is as follows: |
Copy code |
<Profile> <Providers> <Clear/> <Add name = "AspNetSqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider" connectionStringName = "ApplicationServices" applicationName = "/"/> </Providers> <Properties> <Add name = "Theme" type = "System. String"/> <Add name = "Name" type = "System. String"/> <Add name = "Birthdate" type = "System. DateTime"/> <Group name = "Address"> <Add name = "StreetAddress" type = "System. String"/> <Add name = "City" type = "System. String"/> <Add name = "State" type = "System. String"/> <Add name = "ZipCode" type = "System. String"/> </Group> </Properties> </Profile> |
<! -- Enable anonymous personalized tracking -->
The code is as follows: |
Copy code |
<AnonymousIdentification enabled = "true" CookieName = ". ASPXANONYMOUSUSER" CookieTimeout = "120000" CookiePath = "/" CookieRequireSSL = "false" CookieSlidingExpiration = "true" /> </System. web> Protected void Page_Load (object sender, EventArgs e) { If (! This. IsPostBack) { ProfileBase profile = HttpContext. Current. Profile; // Display device information if any String theme = (string) profile. GetPropertyValue ("Theme "); This. tbName. Text = (string) profile. GetPropertyValue ("Name "); This. tbAddress. Text = (string) profile. GetPropertyValue ("Address. StreetAddress "); This. tbCity. Text = (string) profile. GetPropertyValue ("Address. City "); This. tbState. Text = (string) profile. GetPropertyValue ("Address. State "); This. tbZip. Text = (string) profile. GetPropertyValue ("Address. ZipCode "); This. DropDownList1.SelectedValue = (string) profile. GetPropertyValue ("Theme "); This. Calendar1.SelectedDate = (DateTime) profile. GetPropertyValue ("Birthdate "); } } Protected void btnSub_Click (object sender, EventArgs e) { If (this. User. Identity. IsAuthenticated) { ProfileBase profile = HttpContext. Current. Profile; Profile. SetPropertyValue ("Theme", "SeeingRed "); Profile. SetPropertyValue ("Name", this. tbName. Text ); Profile. SetPropertyValue ("Address. StreetAddress", this. tbAddress. Text ); Profile. SetPropertyValue ("Address. City", this. tbCity. Text ); Profile. SetPropertyValue ("Address. State", this. tbState. Text ); Profile. SetPropertyValue ("Address. ZipCode", this. tbZip. Text ); Profile. SetPropertyValue ("Theme", this. DropDownList1.SelectedValue ); Profile. SetPropertyValue ("Birthdate", this. Calendar1.SelectedDate ); Profile. Save (); // Save device information } } |
12.4 quick reference
Define settings for personalized device files
|
In web. config, the process <profile> is used to define the name and type pairs used to build the device file architecture.
|
Properties of the received device file
|
Get the object of the device file through the current HttpContext process, and apply the GetPropertyValue and SetPropertyValue methods.
|
Tracking Anonymous device files through cookies
|
Enable anomymousIdentification in web. config, and add the allowAnonymous feature for the required device file attributes.
|
13. Web Parts
13.1 brief history of "Web Parts"
13.1 advantages of Web Components
The "Web Parts" control can be used to implement portal websites. Workflow and collaborative management have become one of the top application targets of the website.
The development of ASP. NET "Web Components" is oriented to the following three scenarios:
N open up "Web Components" controls
N build a regular page using the "Web Parts" control
N implement the "Web Parts" page and "Web Parts" in the portal website"
13.3 development of "Web Components" controls
All "Web Components" controls can be seen as a superset of existing ASP. NET Server controls. You can grasp the "Web Components" control in a specific format in the specific scene, the developer can also through the process from the System. web. UI. webControls. webParts. the WebPart class is derived to create a custom "Web Parts" control.
13.3.1 opening of the "Web Parts" page
A general Web page may be able to apply "Web Parts ". Visual Studio supports creating pages that contain WebPart controls.
To open a WebPart page, you must add WebPartManager and several regions to the page and add WebPart controls for these regions.
13.3.2 development of "Web Components" application method
Finally, we can open up a full set of application methods through the WebPart control. For example, suppose you want to build a portal. If the WebPart control is applied, you can customize the page. "Web parts" are also suitable for building common application methods (such as sharing records or document application methods), and can package and publish them, and then batch schedule them to other companies' websites.
13.4 "Web Components" architecture
The "Web Components" architecture undertakes many tasks. Web components act as higher-level UI elements, so functional components are divided into page management and regional management. Non-conforming WebPart controls must be reconciled and consistent. In addition, each functional area on the page must be treated as a group of controls (such as for layout management ).
13.4.1 WebPartManager and WebPartZone
Web parts are managed by the region, and the region is managed by the WebPartManager. WebPartManager must be added as long as WebPart is applied on the page. WebPartManager is responsible for managing and coordinating controls in the region and region. WebPartZone can manage the UI elements in its own widget collection.
In the region, ZoneTemplate is used to accommodate "Web Parts ". If ZoneTemplate has a conventional ASP. NET control, ASP. NET will wrap it according to the WebPart situation.
13.4.2 built-in regions
The Web Parts area is used to manage the layout of a group of controls. ASP. NET has four built-in regions:
N WebPartZone contains the root function of the server control in the management area. This area can accommodate server-side controls and WebPart controls. Regular controls are encapsulated by the GenericWebPart control at runtime so that they have WebPart operations.
N CatalogZone (Directory area) is used to hold the CatalogPart control. "Contents" are generally used to manage the visibility of parts on the page. The CatalogZone control can display and hide its content in the display mode. It is called "contents" because it acts as the control directory that can be selected by the ultimate user.
N EditorZone this control allows the ultimate user to correct and personalize webpages according to their personal preferences. Customize the website, including personal information (if it is the birth, gender-specific title, website visits, etc.), color matching and layout .... EditorZone is designed to achieve this feature. It can also save and load the relevant settings, so that the user can continue to apply during the login.
N ConnectionZone "Web Components" often need dynamic connection and communication. ConnectionZone is set up for this purpose.
13.4.2 built-in "Web components"
N DeclarativeCatalogPart when building a WebPart page, you can dynamically add or declare parts.
N the ultimate user may customize the website by opening and closing controls.
N PageCatalogPart the ultimate user may customize the website by opening and closing controls during the process.
N ImportCatalogPart application ImportCatalogPart. You can import the description of "Web parts" from xml data.
N AppearanceEditorPart this control is used to edit the appearance attributes of a contact relationship with a WebPart or GenericWebPart.
N BehaviorEditorPart this control implements editing of WebPart or GenericWebPart actions
N LayoutEditorPart this control is used to edit the layout attribute of the contact relationship with a WebPart or GenericWebPart.
N PropertyGridEditorPart: this control allows you to edit custom attributes of WebPart. (other EditorPart controls only support editing existing attributes of WebPart)
Application "Web Parts ":
Application websites support "Web components"
|
Run aspnet_regsql for the application method database to ensure that the device files and roles are enabled.
|
Application Page support WebPart controls
|
Add WebPartManager to the page
|
Editing supported by the Web Parts page
|
Add EdiorZone to the page
|
Add a region that allows the server control to take over the "Web Components" architecture
|
Add WebPartZone to the page
|
Allows users to dynamically add controls from controls.
|
Add a CatalogZone for the page, and add the control that the desire to present in the directory in "edit template" mode.
|
Create Web Parts"
|
Derived from the System. Web. UI. WebControls. WebParts. WebPart class subclass, and then choose the following random rate a way to emerge content: N HTML appears in the Reader method of Web Components
N in the ASP. NET control, and then add it to the "WEB Parts" Controls collection to achieve active emergence
|
Condition management and caching
14. Session status
14.1 What Is session status?
14.2 ASP. NET and session status
14.3 session status overview
Practice session Status:
1. Create an ASP. NET empty Web application method SeesionState.
2. Add a Web form named Default. aspx. Add a text box on this page to enter the value to be stored in the session status. Add a label for this text box.
3. Add two tags to the two boxes.
4. Add a string member variable.
5. Run this page. Type some text in the text box and click "add to session status ". Member variables cannot be saved. The lifecycle of a page object is very short. It is valid only in the era of begging for punishment, and will be burned down later. Member variables will be cleared immediately after the pleading for punishment is completed. The new page we get after submitting is a new object, and the member variables will also be initialized from the beginning.
6. You can solve this problem by using session conditions. Write the code to store the string in the session state.
7. Obtain and save the running result data.
14.4 session status and error data types
Apply the ADO. NET object, and bind controls and sessions to the data:
14.5 session status and device
1. Completely disabled
2. Store session status during the process
3. Store the session status in the health server
4. Store session status in the database
14.5.1 disable session status
Through ASP. NET session status of IIS, the device object is located in the web. config file.
14.5.2 store session status during the process
14.5.3 store session status in the status server
14.5.4 store session conditions in the database
14.6 session status tracking
14.6.1 track session status through cookies
14.6.2 trace session status through URL
14.6.3 active detection
14.6.4 application device file
14.6.5 Session Timeout
14.7 Other session settings
14.8 Wizard control-a scheme for changing session conditions
14.8 quick reference
Session status of the current client
|
Apply the Page. Session attribute or the HttpContext. Session attribute of the current level.
|
Values in the current client session status
|
A session is a collection of key/value pairs. The response data can be received through the key (string) applied during data storage.
|
Store session status during the process
|
Edit the seessionState node features in web. confgi. Set mode to Inproc
|
Store session status in the status server
|
Edit the features of the sessionState section in web. config. Set mode to StateServer and stateConnectionString
|
Store session status in SQL Server
|
Edit the features of the sessionState section in web. config. Set mode to SQLServer and salConnectionString
|
Disable session status
|
Edit the features of the sessionState section in web. config. Set mode to Off
|
Use cookies to track session status
|
Edit the features of the sessionState section in Web. config. Set cookieless to false (default)
|
Apply URL to track session status
|
Edit the features of the sessionState section in web. config. Set cookieless to true
|
Set session status timeout
|
Edit the features of the sessionState section in web. config. Set timeout as the timeout tree (in minutes) MRMY
|