All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET Remoting体系结构(五)
MSSQL关于日期的查询_[SQL,Server教程]
HTML网页制作基础教程(3):常用标记讲解_[Html教程]
支付宝接口(刚完成,应该是目前最好的了)_.net资料_编程技术
网页技巧:如何在网页上实现音乐播放?_JavaScript技术_编程技术
PHP技巧:PHP脚本编程中的文件系统函数库_php资料_编程技术
建立Apache+PHP3+MySQL驱动的动态网站_php资料_编程技术
HTML教程-收集的常用的HTML标签(4)_[Html教程]
Delphi菜单动态合并实例
防止用户上传产生无效文件源码_[Asp.Net教程]
Reading number is top 10 pictures
The girl of like self-time
The service WaLiangGe Chinese aircraft carrier1
BingBingFan apple dew point photo gallery4
影评-疯子,我爱你
西游日记2
Chinese paper-cut grilles art appreciation1
China's ambassador to Libya embassy was shock, and the glass is broken in
Other people's teacher VS my teacher
The real super beauty1
29 the belle stars after bath figure4
Download software ranking
Rio big adventure
White deer villiage
SQL2000 For 4IN1
C#COM编程指南
matrix3
I for your crazy
尖东毒玫瑰B
金山office2007
Unix video tutorial7
The Bermuda triangle2
归海一刀 published in(发表于) 2014/1/30 1:12:18 Edit(编辑)
温故知新ASP.NET,2.0(C#)(7),-,Profile(存储用户配置)_[Asp.Net教程]

温故知新ASP.NET,2.0(C#)(7),-,Profile(存储用户配置)_[Asp.Net教程]

温故知新ASP.NET 2.0(C#)(7) - Profile(存储用户配置)_[Asp.Net教程]

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



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


connectionStringName="SqlConnectionString"
applicationName="/" />










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


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


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


Profile是自动保存的,但是某些复杂类型可能无法自动保存,此时需要设置元素的automaticSaveEnabled设置为false,要保存的话则调用 Profile 上的 Save 方法即可。要动态取消Profile的自动保存功能的话则需要在 global.asax 中加一个Profile_ProfileAutoSaving事件,示例如下,仅为说明
void Profile_ProfileAutoSaving(Object sender, ProfileAutoSaveEventArgs e)
{
if ((e.Context.Items["CancelProfileAutoSave"] != null) && ((bool)e.Context.Items["CancelProfileAutoSave"] == true))
e.ContinueWithProfileAutoSave = false;
}
在需要取消Profile的自动保存功能的页的代码处如下写
protected void Page_Load(object sender, EventArgs e)
{
Context.Items["CancelProfileAutoSave"] = true;
}


2、通过ProfileManager执行相关任务,如搜索有关所有配置文件、经过身份验证用户的配置文件及匿名用户的配置文件的统计信息,确定在给定时间段内尚未修改的配置文件的数量,根据配置文件的上一次修改日期删除单个配置文件及多个配置文件等


3、将匿名配置文件迁移到经过身份验证的配置文件
在global.asax加一个Profile_MigrateAnonymous事件处理,示例如下,仅为说明
void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
{
// 获得匿名配置
ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);


// 从匿名配置中取值并赋值给经过身份验证的配置
if (anonProfile.Color != System.Drawing.Color.Empty)
{
Profile.Color = anonProfile.Color;
}

// 从数据库中删除匿名配置
ProfileManager.DeleteProfile(pe.AnonymousID);

// 清除与某个会话关联的匿名 Cookie 或标识符
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}


示例
App_Code/CustomProfile.cs
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;


/**////


/// CustomProfile 的摘要说明
///

public class CustomProfile : ProfileBase
{
private string _customName;


public string CustomName
{
get { return _customName; }
set { _customName = value; }
}


private bool _customSex;


public bool CustomSex
{
get { return _customSex; }
set { _customSex = value; }
}
}


web.config


connectionStringName="SqlConnectionString"
applicationName="/" />










Profile/Test.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Profile_Test" Title="存储用户配置测试" %>





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
{
protected void Page_Load(object sender, EventArgs e)
{
// 一看就懂
Profile.Name = User.Identity.Name;
Profile.Color = System.Drawing.Color.AliceBlue;
Profile.Group.Collection.Clear();
Profile.Group.Collection.Add("冰棍");
Profile.Group.Collection.Add("瓜子");
Profile.Group.Price = 999999;


Profile.CustomName = User.Identity.Name;
Profile.CustomSex = true;


lbl.Text = "Name:" + Profile.Name + "
";
lbl.Text += "Color:" + Profile.Color.ToString() + "
";
foreach (string s in Profile.Group.Collection)
{
lbl.Text += "商品有:" + s + "
";
}
lbl.Text += "价格:" + Profile.Group.Price + "
";


lbl.Text += "自定义类名字:" + Profile.CustomName + "
";
lbl.Text += "自定义类姓名:" + Profile.CustomSex;
}
}


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



注:需要用aspnet_regsql配置数据库

[源码下载]


作者:webabcd







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.