Asp.net 2.0 tutorial personalized user configuration

Source: Internet
Author: User

Respect the author. Keep the words www.it55.com.

This section describes Asp.net 2.0's personalized service solution technical framework: personalized user configuration.
What is personalized user configuration? Here is a story from a friend of Daniel Pang on the Internet:

Storing and accessing user configuration data has always been a hot topic for developers. In the ASP. NET 1. x era, this function is implemented mainly through objects such as sessions and applications or database storage methods. Both methods have insurmountable shortcomings. For example, the former is prone to data loss, while the latter is cumbersome. For example, you need to design your own database and data access code. To solve the preceding problems, ASP. NET 2.0 has added the personalized user configuration feature.
The personalized user configuration function is mainly used to store individual user configuration data, which can be a simple data type, a complex data type, or even a custom object. At the same time, a single user can be either an anonymous user or a registered user. By default, all user configuration data is stored in the SQL Server database, and you do not need to create or maintain the database on your own. ASP. NET 2.0 automatically completes these tasks. The personalized user configuration function also allows you to access a variety of strong APIs from any location in the application to facilitate the storage, display, and management of user configuration information. It is very easy to use the personalized user configuration function. First, in the web. the config file defines the configuration information name, data type, and so on, and then calls the strong type API related to the user configuration function, for example, profile allows you to store, access, and manage user configuration information.

Development Environment requirements: Visual Studio 2005 + SQL Server 2005
If your database is not SQL Server 2005, configure the environment according to the following Tutorial:
Http://www.itgao.com/html/2007-05/45481.html
Program objective: to record user personalized data (this example uses anonymous users as an example)

Step 1 configure web. config to enable and define the configuration information stored and tracked by the user.

<System. Web>
<Profile>
<Properties>
<Add name = "name" allowanonymous = "true"/>
<Add name = "lastsubmit" type = "system. datetime" allowanonymous = "true"/>
<Group name = "Address">
<Add name = "city" allowanonymous = "true"/>
<Add name = "postalcode" allowanonymous = "true"/>
</Group>
</Properties>
</Profile>
</System. Web>

The preceding configuration defines the name and address group attributes, and the address group attributes include the city and postalcode attributes. By default, these attribute values are of the string type, of course, we can also explicitly specify the type by type = "value type. Allowanonymous = "true" specifies that this configuration supports anonymous users.

Step 2 configure the SQL Server database configuration.
By default, when the program executes this configuration for the first time, it will automatically create a specific database instance of SQL Server 2005 Express (which is included by default when Visual Studio 2005 is installed. The database instance is stored in the app_data folder under the root directory of the application, and its name is aspnetdb. MDF, which contains data tables that store user configuration information and other objects related to user configuration by default.
Therefore, if you use SQL Server 2005, this step can be omitted. If not, see http://www.itgao.com/html/2007-05/45481.html

Step 3: implement the program.
Some code of the. ASPX page:

<Form ID = "form1" runat = "server">
<Div>
<Fieldset style = "width: 300px">
<Legend class = "maintitle"> achieve personalized user configuration for anonymous users </legend>
<Br/>
<Table border = "0" width = "90%" class = "commontext" align = "center" cellpadding = "5">
<Tr>
<TD>
User name: </TD>
<TD>
<Asp: textbox id = "txtname" runat = "server"> </ASP: textbox> </TD>
</Tr> # P # paging title # e #
<Tr>
<TD>
Country: </TD>
<TD>
<Asp: textbox id = "txtcity" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD>
Zip code: </TD>
<TD>
<Asp: textbox id = "txtpostalcode" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center">
<Asp: button id = "btnsubmit" runat = "server" text = "Submit" onclick = "btnsubmit_click"/>
</TD>
</Tr>
</Table>
</Fieldset>
</Div>
</Form>

Background. CS file code:

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
// Display User Configuration Information
Displayprofileinfo ();
}
}
Protected void btnsubmit_click (Object sender, eventargs E)
{
// Save the user configuration information to the profile attribute
Profile. Name = txtname. text;
Profile. Address. City = txtcity. text;
Profile. Address. postalcode = txtpostalcode. text;
// Display User Configuration Information
Displayprofileinfo ();
}
Private void displayprofileinfo ()
{
// Obtain data from the profile attribute and assign it to the server control
Txtname. Text = profile. Name;
Txtcity. Text = profile. Address. City;
Txtpostalcode. Text = profile. Address. postalcode;
}
}

Running result:
The application stores user input usernames and country information in the database, and automatically generates a unique ID number for the user. The client indicates that the user is usually stored on the client computer as cookies, the validity period is about 60 days. In this way, the application easily stores the user's personal settings and provides a prerequisite for personalized services of the program. # P # paging title # e #

In the next section, we will learn ASP. NET 2.0 personalized service web components.

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.