ASP.NET Tutorial/Profile/Custom Data

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Creating a class to use as a personalization type

File: ShoppingCart.vb
Imports Microsoft.VisualBasic
<Serializable()> _
Public Class ShoppingCart
    Private PID As String
    Private CompanyProductName As String
    Private Number As Integer
    Private Price As Decimal
    Private DateAdded As DateTime
    Public Property ProductID() As String
        Get
            Return PID
        End Get
        Set(ByVal value As String)
            PID = value
        End Set
    End Property
    Public Property ProductName() As String
        Get
            Return CompanyProductName
        End Get
        Set(ByVal value As String)
            CompanyProductName = value
        End Set
    End Property
    Public Property NumberSelected() As Integer
        Get
            Return Number
        End Get
        Set(ByVal value As Integer)
            Number = value
        End Set
    End Property
    Public Property ItemPrice() As Decimal
        Get
            Return Price
        End Get
        Set(ByVal value As Decimal)
            Price = value
        End Set
    End Property
    Public Property DateItemAdded() As DateTime
        Get
            Return DateAdded
        End Get
        Set(ByVal value As DateTime)
            DateAdded = value
        End Set
    End Property
End Class

File: ShoppingCart.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;
[Serializable]
public class ShoppingCart
{
    private string PID;
    private string CompanyProductName;
    private int Number;
    private decimal Price;
    private DateTime DateAdded;
    public ShoppingCart() { }
    public string ProductID
    {
        get { return PID; }
        set { PID = value; }
    }
    public string ProductName
    {
        get { return CompanyProductName; }
        set { CompanyProductName = value; }
    }
    public int NumberSelected
    {
        get { return Number; }
        set { Number = value; }
    }
    public decimal ItemPrice
    {
        get { return Price; }
        set { Price = value; }
    }
    public DateTime DateItemAdded
    {
        get { return DateAdded; }
        set { DateAdded = value; }
    }
}
File: Web.config
<configuration>
  <system.web>
     <profile>
        <properties>
           <add name="FirstName" type="System.String" />
           <add name="LastName" type="System.String" />
           <add name="LastVisited" type="System.DateTime" />
           <add name="Age" type="System.Integer" />
           <add name="Member" type="System.Boolean" />
           <add name="Cart" type="ShoppingCart" serializeAs="Binary" />
        </properties>
    </profile>
    <authentication mode="Windows" />
  </system.web>
</configuration>