鍍金池/ 教程/ C#/ 個性化
調(diào)試
管理狀態(tài)
Panel 控件
Web 服務(wù)
語言集成查詢
數(shù)據(jù)源
基礎(chǔ)控件
廣告輪轉(zhuǎn)器
服務(wù)器端
服務(wù)器控件
ADO.NET
多線程
HTML 服務(wù)器
生命周期
Ajax 控制
客戶端
異常處理
環(huán)境設(shè)置
部署
個性化
驗證器
多視圖
日歷
文件上傳
ASP.NET - 數(shù)據(jù)綁定
數(shù)據(jù)庫存取
實例
自定義控件
簡介
配置
數(shù)據(jù)緩存
安全性
指令
事件處理

個性化

網(wǎng)站是為用戶的重復(fù)訪問而設(shè)計的。個性化允許一個網(wǎng)站記住用戶標識和其他信息細節(jié),并且它給每個用戶提供了一個個人的環(huán)境。

ASP.NET 為滿足特性客戶的品味和喜好而個性化一個網(wǎng)站提供服務(wù)。

理解特征文件

ASP.NET 個性化服務(wù)基于用戶的特征文件。用戶特征文件定義了該網(wǎng)站需要用戶的信息。例如,名字,年齡,地址,出生日期和手機號碼。

這個信息被定義在應(yīng)用程序的 web.config 文件中并且 ASP.NET 運行時間閱讀并使用它。這個工作由個性化提供者所完成。

用戶數(shù)據(jù)所含有的用戶特征文件被存儲在默認的 ASP.NET 創(chuàng)建的數(shù)據(jù)庫中。你可以創(chuàng)建你自己的數(shù)據(jù)庫來存儲特征文件。特征文件數(shù)據(jù)定義被存儲在配置文件 web.config 中。

例子

讓我們創(chuàng)建一個樣本網(wǎng)站,那里我們想要我們的應(yīng)用程序記住用戶細節(jié),像名字,地址,出生日期等。在 web.config 文件中用 元素添加特征文件細節(jié)。

<configuration>
<system.web>

<profile>
   <properties>
      <add name="Name" type ="String"/>
      <add name="Birthday" type ="System.DateTime"/>

      <group name="Address">
         <add name="Street"/>
         <add name="City"/>
         <add name="State"/>
         <add name="Zipcode"/>
      </group>

   </properties>
</profile>

</system.web>
</configuration>

當(dāng)特征文件在 web.config 文件中被定義時,特征文件可以通過在當(dāng)前的 HttpContext 中找到的 Profile 屬性使用并且通過頁面獲得。

添加 text box 來獲取在特征文件中定義的用戶輸入,添加一個 button 來提交數(shù)據(jù):

http://wiki.jikexueyuan.com/project/asp-net/images/personalization.jpg" alt="1" />

更新 Page_load 來展示特征文件信息:

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;

public partial class _Default : System.Web.UI.Page 
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!this.IsPostBack)
      {
         ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

         if (pc != null)
         {
            this.txtname.Text = pc.Name;
            this.txtaddr.Text = pc.Address.Street;
            this.txtcity.Text = pc.Address.City;
            this.txtstate.Text = pc.Address.State;
            this.txtzip.Text = pc.Address.Zipcode;
            this.Calendar1.SelectedDate = pc.Birthday;
         }
      }
   }
}

為提交按鈕寫以下的句柄,將用戶數(shù)據(jù)存入特征文件中:

protected void btnsubmit_Click(object sender, EventArgs e)
{
   ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

   if (pc != null)
   {
      pc.Name = this.txtname.Text;
      pc.Address.Street = this.txtaddr.Text;
      pc.Address.City = this.txtcity.Text;
      pc.Address.State = this.txtstate.Text;
      pc.Address.Zipcode = this.txtzip.Text;
      pc.Birthday = this.Calendar1.SelectedDate;

      pc.Save();
   }
}

當(dāng)頁面第一次執(zhí)行時,用戶需要輸入信息。但是,下一次用戶的細節(jié)將被自動加載。

元素的屬性

除了我們已經(jīng)使用過的名字和類型屬性,元素還有其它屬性。以下的表格展示了這些屬性中的一些:

屬性 描述
name 屬性的名字。
type 類型默認是 string 但是它允許任何完全的類名稱作為數(shù)據(jù)類型。
serializeAS 當(dāng)序列化這個值時使用的格式。
readOnly 只讀的特征文件值不能被改變,這個屬性默認是 false。
defaultValue 一個默認的值,如果特征文件不存在或者沒有信息的話它被使用。
allowAnonymous 一個指示這個屬性是否能和匿名文件使用的布爾值。
Provider 應(yīng)該被用來管理這個屬性的特征文件提供者。

匿名個性化

匿名個性化允許用戶在標識它們自己之前個性化網(wǎng)站。例如,Amazon.com 允許用戶在登錄前在購物車中添加物品。為了啟用此功能,web.config 文件可以被配置成以下:

<anonymousIdentification enabled ="true" cookieName=".ASPXANONYMOUSUSER"
   cookieTimeout="120000" cookiePath="/" cookieRequiresSSL="false"
   cookieSlidingExpiration="true" cookieprotection="Encryption"
   coolieless="UseDeviceProfile"/>
上一篇:服務(wù)器控件下一篇:Web 服務(wù)