.Web.UI;
namespace CustomPeople
{
[TypeConverter(typeof(AddressConverter))]
public class Address : IStateManager
{
private String street = null;
private String city = null;
private String state = null;
private String zip = null;
public Address()
:
this(String.Empty, String.Empty,
String.Empty, String.Empty)
{ }
public Address(string street, string city,
string state, string zip)
{
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
[NotifyParentProperty(true)]
public String Street
{
get { return ViewState["Street"] != null ? (string)ViewState["Street"] : String.Empty; }
set { ViewState["Street"] = value; }
}
[
Category("Behavior"),
DefaultValue(""),
Description("城市"),
NotifyParentProperty(true),
]
public String City
{
get { return ViewState["City"] != null ? (string)ViewState["City"] : String.Empty; }
set { ViewState["City"] = value; }
}
[
Category("Behavior"),
DefaultValue(""),
Description("国籍"),
NotifyParentProperty(true),
]
public String State
{
get { return ViewState["State"] != null ? (string)ViewState["State"] : String.Empty; }
set { ViewState["State"] = value; }
}
[
Category("Behavior"),
DefaultValue(""),
Description("邮编"),
NotifyParentProperty(true)
]
public String Zip
{
get { return ViewState["Zip"] != null ? (string)ViewState["Zip"] : String.Empty; }
set { ViewState["Zip"] = value; }
}
方法#region 方法
public override string ToString()
{
return ToString(CultureInfo.CurrentCulture);
}
public virtual string ToString(CultureInfo culture)
{
return TypeDescriptor.GetConverter(typeof(Address)).ConvertToString(null, culture, this);
}
#endregion
自定义状态管理#region 自定义状态管理
private bool _isTrackingViewState;
private StateBag _viewState;
protected StateBag ViewState
{
get
{
if (_viewState == null)
{
_viewState = new StateBag(false);
if (_isTrackingViewState) ((IStateManager)_viewState).TrackViewState();
}
return _viewState;
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _isTrackingViewState;
}
}
void IStateManager.LoadViewState(object savedState)
{
if (savedState != null)
{
((IStateManager)ViewState).LoadViewState(savedState);
}
}
object IStateManager.SaveViewState()
{
object savedState = null;
if (_viewState != null)
{
savedState =
((IStateManager)_viewState).SaveViewState();
}
return savedState;
}
void IStateManager.TrackViewState()
{
_isTrackingViewState = true;
if (_viewState != null)
{
((IStateManager)_viewState).TrackViewState();
}
}
internal void SetDirty()
{
_viewState.SetDirty(true);
}
#endregion
}
}本来不想解释什么,怕大家嫌我罗嗦,但是要记得ViewState的为StateBag类型
3.2第二步,维护自定义属性的状态,(请注意,此时的CustomAddress就是People的一个属性,所以我们就叫自定义属性的状态维护)
Code
自定义视图状态#region 自定义视图状态
[Description("地址集合")]
[DesignerSerializat