您的位置:首页 > 编程语言 > ASP

重新过一遍ASP.NET 2.0(C#)(7) - Profile(存储用户配置测试)

2007-06-27 16:42 609 查看
介绍
ASP.NET 2.0 中的存储用户配置功能使您可以定义并存储要在整个应用程序中使用的基于用户的设置。而且,在用户未登录时,可以将这些设置存储在匿名配置文件中,然后在将来某个时间将其迁移到登录用户的配置文件中。

关键
1、配置<system.web>元素下的<profile>元素;如果需要支持匿名的话则还需要配置<system.web>元素下的<anonymousIdentification>元素。示例如下,仅为说明

<profile enabled="true" defaultProvider="SqlProfileProvider" inherits="CustomProfile">
<providers>
<add name="SqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="SqlConnectionString"
applicationName="/" />
</providers>
<properties>
<add name="Name" />
<add name="Color" type="System.Drawing.Color" />
<group name="Group">
<add name="Collection" type="System.Collections.ArrayList" />
<add name="Price" type="int" defaultValue="100" />
</group>
</properties>
</profile>

<anonymousIdentification
enabled="true"
cookieName=".VS2005_ANONYMOUS"
cookieTimeout="1440"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All"
cookieless="UseCookies" />

各属性详细说明参看MSDN,索引处查找“profile 元素”和“anonymousIdentification 元素”

注意:
<profile>元素的inherits属性指定自定义类,该类要继承自ProfileBase

Profile是自动保存的,但是某些复杂类型可能无法自动保存,此时需要设置<profile>元素的automaticSaveEnabled设置为false,要保存的话则调用 Profile 上的 Save 方法即可。要动态取消Profile的自动保存功能的话则需要在 global.asax 中加一个Profile_ProfileAutoSaving事件,示例如下,仅为说明

void Profile_ProfileAutoSaving(Object sender, ProfileAutoSaveEventArgs e)
protected void Page_Load(object sender, EventArgs e)
void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Web.Profile;

public class CustomProfile : ProfileBase

web.config

<profile enabled="true" defaultProvider="SqlProfileProvider" inherits="CustomProfile">
<providers>
<add name="SqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="SqlConnectionString"
applicationName="/" />
</providers>
<properties>
<add name="Name" />
<add name="Color" type="System.Drawing.Color" />
<group name="Group">
<add name="Collection" type="System.Collections.ArrayList" />
<add name="Price" type="int" defaultValue="100" />
</group>
</properties>
</profile>

Profile/Test.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Label ID="lbl" runat="Server" />
</asp:Content>

Profile/Test.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Profile_Test : System.Web.UI.Page

用“abc”这个用户登录后的运行结果
Name:abc
Color:Color [AliceBlue]
商品有:冰棍
商品有:瓜子
价格:999999
自定义类名字:abc
自定义类姓名:True

注:需要用aspnet_regsql配置数据库

OK
[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: