Asp.net basics Article 4

Source: Internet
Author: User

This is an article on the basics of asp.net beginners. For more information, see.12. personalization12.1 provide personalized services for visitors12.2 personalization in ASP. NET12.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

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.