定义和使用ASP.NET Profile(附例)

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属性中.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值