Get / set user-defined object to profile : Introduction « Profile « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ComplexTypes" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width: 99px">
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 99px">
                    Street:</td>
                <td>
                    <asp:TextBox ID="txtStreet" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 99px">
                    City:</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 99px">
                    Zip Code:</td>
                <td>
                    <asp:TextBox ID="txtZip" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 99px">
                    State:</td>
                <td>
                    <asp:TextBox ID="txtState" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 99px">
                    Country:</td>
                <td>
                    <asp:TextBox ID="txtCountry" runat="server" ></asp:TextBox></td>
            </tr>
        </table>
    
    </div>
        <br />
        <asp:Button ID="cmdGet" runat="server" OnClick="cmdGet_Click" Text="Get" />
        <asp:Button ID="cmdSave" runat="server" OnClick="cmdSave_Click" Text="Save" />
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ComplexTypes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
      LoadProfile();
    }
  protected void cmdSave_Click(object sender, EventArgs e)
  {
    Profile.Address = new Address(txtName.Text, txtStreet.Text, txtCity.Text, txtZip.Text, txtState.Text, txtCountry.Text);
  }
  protected void cmdGet_Click(object sender, EventArgs e)
  {
    LoadProfile();
  }

  private void LoadProfile()
  {
    txtName.Text = Profile.Address.Name;
    txtStreet.Text = Profile.Address.Street;
    txtCity.Text = Profile.Address.City;
    txtZip.Text = Profile.Address.ZipCode;
    txtState.Text = Profile.Address.State;
    txtCountry.Text = Profile.Address.Country;
  }
}

[Serializable()]
 class Address
{
  private string name;
  public string Name
  {
    get { return name; }
    set { name = value; }
  }

  private string street;
  public string Street
  {
    get { return street; }
    set { street = value; }
  }

  private string city;
  public string City
  {
    get { return city; }
    set { city = value; }
  }

  private string zipCode;
  public string ZipCode
  {
    get { return zipCode; }
    set { zipCode = value; }
  }

  private string state;
  public string State
  {
    get { return state; }
    set { state = value; }
  }

  private string country;
  public string Country
  {
    get { return country; }
    set { country = value; }
  }

  public Address(string name, string street, string city,
    string zipCode, string state, string country)
  {
    Name = name;
    Street = street;
    City = city;
    ZipCode = zipCode;
    State = state;
    Country = country;
  }

  public Address() { }
}
File: Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <profile>
      <properties>
        <add name="address" type="Address" serializeAs="Binary"/>

      </properties>
    </profile>
  </system.web>
</configuration>








15.5.Introduction
15.5.1.Using Profiles
15.5.2.Get / set profile data defined in Web.config
15.5.3.Get / set user-defined object to profile
15.5.4.Inheriting a Profile from a Custom Class
15.5.5.Creating Complex Profile Properties
15.5.6.Making Personalization Properties Read-Only
15.5.7.Defining default values for personalization properties
15.5.8.Working with the automaticSaveEnabled attribute