鍍金池/ 教程/ C#/ ASP.NET個性化
ASP.NET調(diào)試
ASP.NET Web Services
ASP.NET緩存
ASP.NET多線程
ASP.NET面板控件
ASP.NET數(shù)據(jù)綁定
ASP.NET數(shù)據(jù)源
ASP.NET個性化
ASP.Net教程
ASP.NET Ajax控件
ASP.NET生命周期
ASP.NET HTML服務(wù)器
ASP.NET簡介
ASP.NET驗證器
ASP.NET多視圖
ASP.NET網(wǎng)站配置
ASP.NET錯誤管理
ASP.NET自定義控件
ASP.NET LINQ
ASP.NET AdRotator控件
ASP.NET客戶端
ASP.NET文件上傳
ASP.NET服務(wù)器控件
ASP.NET開發(fā)環(huán)境配置
ASP.NET管理狀態(tài)
ASP.NET服務(wù)端
ASP.NET數(shù)據(jù)庫訪問(Access)
ASP.NET基本控件
ASP.NET安全
ASP.NET指令
ASP.NET事件處理
ASP.NET第一個程序
ASP.NET日歷控件

ASP.NET個性化

網(wǎng)站設(shè)計是為用戶提供重復(fù)訪問的個性化使得網(wǎng)站能夠記住用戶身份和其他信息細(xì)節(jié),并為每個用戶呈現(xiàn)個人化的環(huán)境。

ASP.NET提供個性化網(wǎng)站來為特定客戶的喜好和偏好地提供服務(wù)。

了解配置文件

ASP.NET個性化服務(wù)基于用戶配置文件。 用戶配置文件定義了該網(wǎng)站所需用戶的信息種類。 例如,姓名,年齡,地址,出生日期和電話號碼。

此信息在應(yīng)用程序的web.config文件中定義,ASP.NET運行時讀取并使用它。這項工作是由個性化提供程序來完成的。

從用戶數(shù)據(jù)中獲取的用戶配置文件存儲在由ASP.NET創(chuàng)建的默認(rèn)數(shù)據(jù)庫中。 您可以創(chuàng)建自己的數(shù)據(jù)庫來存儲配置文件。配置文件數(shù)據(jù)定義存儲在配置文件web.config中。

示例

下面創(chuàng)建一個ASP.Net空網(wǎng)站示例項目:Personalization ,我們希望應(yīng)用程序記住用戶詳細(xì)信息,如姓名,地址,出生日期等。在web.config文件中的<system.web>元素節(jié)點下添加配置文件詳細(xì)信息。

<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"/>
      </group>

   </properties>
</profile>

</system.web>
</configuration>

web.config文件中定義配置文件時,配置文件可以通過當(dāng)前HttpContext中的Profile屬性使用,也可以通過頁面使用。

按照配置文件中的定義添加文本框以接受用戶輸入,并添加一個用于提交數(shù)據(jù)的按鈕:

更新Page_load事件方法以顯示配置文件信息:

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.Calendar1.SelectedDate = pc.Birthday;
            }
        }
    }

提交按鈕編寫以下處理程序,將用戶數(shù)據(jù)保存到配置文件中:

    protected void Button1_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.Birthday = this.Calendar1.SelectedDate;

            pc.Save();
        }
    }
`

當(dāng)頁面首次執(zhí)行時,用戶需要輸入信息。 但是,下次用戶的詳細(xì)信息會自動加載。

add元素的屬性

除了已經(jīng)使用的名稱和類型屬性之外,還有<add>元素的其他屬性。下表說明了其中的一些屬性:

編號 屬性 描述
1 name 屬性的名稱。
2 type 默認(rèn)情況下,類型是字符串,但它允許任何完全限定的類名作為數(shù)據(jù)類型。
3 serializeAs 序列化此值時使用的格式。
4 readOnly 只讀配置文件值不能更改,默認(rèn)情況下該屬性為false。
5 defaultValue 如果配置文件不存在或沒有信息,則使用默認(rèn)值。
6 allowAnonymous 一個布爾值,指示此屬性是否可以與匿名配置文件一起使用。
7 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"/>