Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [278]

By Root 5371 0
and FormatString. This choice of bindable properties is arbitrary, however.

You define a pair of DataXxxField properties—one for Value and one for FormatString. These string properties contain the name of the data source fields mapped to the Value and FormatString. In particular, DataValueField indicates that the field mapped to Value and DataTextField specifies the field linked to FormatString. Once again, note that the names used here are arbitrary.

public virtual string DataValueField

{

get

{

var o = ViewState["DataValueField"] as String;

return o ?? String.Empty;

}

set { ViewState["DataValueField"] = value; }

}

public virtual string DataTextField

{

get

{

var o = ViewState["DataTextField"] as String;

return o ?? String.Empty;

}

set { ViewState["DataTextField"] = value; }

}

As you can see, both properties use the ViewState as the storage medium and are set to the empty string by default. Other popular data-bound properties available on the GaugeBar class are DataSource, DataSourceID, and DataMember, all of which are inherited from parent classes.

The GaugeBar’s Data Item Object


After the GaugeBar control is bound to some external data, you need to track and cache any bound data. For this purpose, you need a data item object. As mentioned, a data item object is a custom class with as many public properties as there are bindable properties in the control’s interface. The data item class for the GaugeBar control is named GaugeBarDataItem (again, an arbitrary name) and is defined as follows:

public class GaugeBarDataItem : IStateManager

{

private string _text;

private float _value;

private bool _marked;

public GaugeBarDataItem()

{

}

public GaugeBarDataItem(float value, string text)

{

_text = text;

_value = value;

}

public string Text

{

get { return _text; }

set { _text = value; }

}

public float Value

{

get { return _value; }

set { _value = value; }

}

public bool IsTrackingViewState

{

get { return _marked; }

}

public void LoadViewState(object state)

{

if (state != null)

{

Pair p = (Pair)state;

_value = (float)p.First;

_text = (string)p.Second;

}

}

public object SaveViewState()

{

return new Pair(_value, _text);

}

public void TrackViewState()

{

_marked = true;

}

}

The class has two public properties—Text and Value—persisted through local members. More interestingly, the class also implements the IStateManager interface, which provides a standard interface to save any valuable contents to the view state across postbacks.

The SaveViewState method returns a Pair object (a sort of simplified array of two elements) filled with the current values of the Text and Value properties. The Pair object returned by SaveViewState becomes the input argument of LoadViewState, which unpacks the Pair object and initializes the Text and Value properties.

The GaugeBar control needs to expose a read-only property of type GaugeBarDataItem. You can use any name for this variable—I’m using DataItem here. More important than the name of the property is its implementation. Take a look at the following code:

private GaugeBarDataItem _dataItem;

...

private GaugeBarDataItem DataItem

{

get

{

if (_dataItem == null)

{

_dataItem = new GaugeBarDataItem();

if (base.IsTrackingViewState)

_dataItem.TrackViewState();

}

return _dataItem;

}

}

Unlike other control properties that are persisted directly in the ViewState collection object, the DataItem property uses a private member (_dataItem) to persist its value. A private member, though, is not persistent and doesn’t survive postbacks. For this reason, in the get accessor of the property you need to check _dataItem for nullness and create a new instance if it is null.

The code contained in the get accessor of a property runs whenever that property is invoked. As you’ll see in a moment, the preceding code ensures that no access to DataItem results in a null object exception and that the state of the object is restored correctly after each postback.

Data Item and View State


Most of the control properties

Return Main Page Previous Page Next Page

®Online Book Reader