<%@ 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>