定义和使用ASP.NET Profile(附例)
2010-04-10 13:57
169 查看
1、双击一个数据源控件,将要存储profile的数据库作为数据源加入控件(这个步骤没什么用,只是为了方便大家用控件建立连接,这样不容易出错),建立好之后将design页面的数据源控件删除,你会发现在web.config里还是有一条连接语句,不要删除,我们下面将用到它,如: <connectionStrings>
<add name="aspnetdbConnectionString" connectionString="Data Source=JAMESSHENG;Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
2、定义Profile,在源码中加入
<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider">
<providers>
<clear/>
<add name="sqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString" applicationName="ProfileSample" description="Sample for asp.net Profile and Profile Service"/>
</providers>
<properties>
<add name="BackGroudColor" type="System.String" allowAnonymous="true"/>
</properties>
</profile>
<anonymousIdentification enabled="true"/>
//defaultprovider指定默认配置文件提供程序的名称
automaticSaveEnabled指定用户配置文件是否在 ASP.NET 页执行结束时自动保存。如果为true,则用户配置文件在 ASP.NET 页执行结束时自动保存。
providers(可选元素)定义配置文件提供程序的集合,它当中包含<add> :name = defaultProvider的值;type = System.Web.Profile.SqlProfileProvider是使用sql;connectionStringName = aspnetdbConnectionString使用上面连接中定义的连接字符串;
properties(必选的元素)定义用户配置文件属性和属性组的集合,它当中包含<add>:name=“BackGroudColor”定义Profile中的属性名称;type="System.String"字符串类型;allowAnonymous="true"定义是否保存匿名用户配置,如果在此处定义true,则在后面必须加入<anonymousIdentification>配置,设置启用匿名用户配置.此时web.config的配置已经完成了
下面有一段小例子:完成一个普通的用户个性化配置的过程
(1)在页面添加一个dropdownlist,添加三个值分别为:红 red,黄 yellow,绿 green,将Autopostback勾上,添加SelectedIndexChanged事件, 加入代码styleContainer.Style.Add(HtmlTextWriterStyle.BackgroundColor, DropDownList1.SelectedValue);
此处的styleContainer刚建好页面中没有,在源码的div中加入ID=“styleContainer”即可,设置div背景色为dropdownlist选择项.
(2)添加Button,添加Click事件,加入代码 this.Profile.BackGroudColor = DropDownList1.SelectedValue;保存dropdownlist选择项到Profile的BackGroudColor属性(我们上面刚定义好的)
(3)在Page_load中加入
if (!Page.IsPostBack)//如果不是回调,而是首次读取
{
if (this.Profile.BackGroudColor != null)
{
DropDownList1.SelectedValue = this.Profile.BackGroudColor;//读取数据库中Profile的BackGroudColor属性
styleContainer.Style.Add(HtmlTextWriterStyle.BackgroundColor, this.Profile.BackGroudColor);//将styleContainer设置背景色
}
}
此时我们就有了一个可以保存匿名用户的个性化设置页面了,打开数据库aspnetdb中Profile数据,你会发现数据中多了BackGroudColor,我们已经将值写入了数据库
ProfileManager.DeleteProfile(string name)//删除一个Profile;
注:Profile中inherits属性很重要,可选的 String 属性。
包含从ProfileBase抽象类派生的自定义类型的类型引用。ASP.NET 动态地生成一个从该类型继承的 ProfileCommon 类,并将该类放在当前HttpContext的Profile属性中.换言之,就是你可以定义一个继承自ProfileBase类,在类中你可以添加一些属性,就像 <add name="BackGroudColor" type="System.String" allowAnonymous="true"/>效果相似,只是有两个好处,一个是写在后置代码中,可以更方便我们操作,另一个是我们可以写一个类,不管在哪个项目里,只需要在inherits中引用一下该类,就可以将该类里所有的属性加入Profile属性中.
<add name="aspnetdbConnectionString" connectionString="Data Source=JAMESSHENG;Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
2、定义Profile,在源码中加入
<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider">
<providers>
<clear/>
<add name="sqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString" applicationName="ProfileSample" description="Sample for asp.net Profile and Profile Service"/>
</providers>
<properties>
<add name="BackGroudColor" type="System.String" allowAnonymous="true"/>
</properties>
</profile>
<anonymousIdentification enabled="true"/>
//defaultprovider指定默认配置文件提供程序的名称
automaticSaveEnabled指定用户配置文件是否在 ASP.NET 页执行结束时自动保存。如果为true,则用户配置文件在 ASP.NET 页执行结束时自动保存。
providers(可选元素)定义配置文件提供程序的集合,它当中包含<add> :name = defaultProvider的值;type = System.Web.Profile.SqlProfileProvider是使用sql;connectionStringName = aspnetdbConnectionString使用上面连接中定义的连接字符串;
properties(必选的元素)定义用户配置文件属性和属性组的集合,它当中包含<add>:name=“BackGroudColor”定义Profile中的属性名称;type="System.String"字符串类型;allowAnonymous="true"定义是否保存匿名用户配置,如果在此处定义true,则在后面必须加入<anonymousIdentification>配置,设置启用匿名用户配置.此时web.config的配置已经完成了
下面有一段小例子:完成一个普通的用户个性化配置的过程
(1)在页面添加一个dropdownlist,添加三个值分别为:红 red,黄 yellow,绿 green,将Autopostback勾上,添加SelectedIndexChanged事件, 加入代码styleContainer.Style.Add(HtmlTextWriterStyle.BackgroundColor, DropDownList1.SelectedValue);
此处的styleContainer刚建好页面中没有,在源码的div中加入ID=“styleContainer”即可,设置div背景色为dropdownlist选择项.
(2)添加Button,添加Click事件,加入代码 this.Profile.BackGroudColor = DropDownList1.SelectedValue;保存dropdownlist选择项到Profile的BackGroudColor属性(我们上面刚定义好的)
(3)在Page_load中加入
if (!Page.IsPostBack)//如果不是回调,而是首次读取
{
if (this.Profile.BackGroudColor != null)
{
DropDownList1.SelectedValue = this.Profile.BackGroudColor;//读取数据库中Profile的BackGroudColor属性
styleContainer.Style.Add(HtmlTextWriterStyle.BackgroundColor, this.Profile.BackGroudColor);//将styleContainer设置背景色
}
}
此时我们就有了一个可以保存匿名用户的个性化设置页面了,打开数据库aspnetdb中Profile数据,你会发现数据中多了BackGroudColor,我们已经将值写入了数据库
ProfileManager.DeleteProfile(string name)//删除一个Profile;
注:Profile中inherits属性很重要,可选的 String 属性。
包含从ProfileBase抽象类派生的自定义类型的类型引用。ASP.NET 动态地生成一个从该类型继承的 ProfileCommon 类,并将该类放在当前HttpContext的Profile属性中.换言之,就是你可以定义一个继承自ProfileBase类,在类中你可以添加一些属性,就像 <add name="BackGroudColor" type="System.String" allowAnonymous="true"/>效果相似,只是有两个好处,一个是写在后置代码中,可以更方便我们操作,另一个是我们可以写一个类,不管在哪个项目里,只需要在inherits中引用一下该类,就可以将该类里所有的属性加入Profile属性中.
相关文章推荐
- Asp.Net Ajax 学习笔记15 如何使用ASP.NET Profile
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译]
- 使用ASP.NET 2.0 Profile存储用户信息
- 请问一下如何在asp.net mvc 下使用profile
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 如何使用ASP.NET Profile
- 自己定义的httpHandlers无法使用Session(ASP.net 1.1)
- ASP.NET 2.0会员管理(membership)中使用profile
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用Asp.net Ajax时出现'Sys'未定义错误的解决办法
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译] Level 200
- ASP.NET--profile的简单使用
- 使用ASP.NET 2.0 Profile存储用户信息[翻译]
- 使用ASP.NET 2.0 Profile存储用户信息_1
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- Asp.net如何使用access数据库做profile实现按照区域显示相应语言功能
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译]
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200