I dynamically create several gauge controls store them to a class, and then add them to a list and session variable for retrieval later. When I attempt to load the gauges from the session state some of the control attributes are not set, as an example ‘gauge.Gauge.Value’ threw an exception of type ‘System.NullReferenceException’. My class looks like this:
public BoldGauge(ASPxGaugeControl gauge, string gaugeValue, string gaugeDataType, float gaugeMinValue, float gaugeMaxValue)
{
Gauge = gauge; // new ASPxGaugeControl();
GaugeValue = gaugeValue;
GaugeDataType = gaugeDataType;
GaugeMinValue = gaugeMinValue;
GaugeMaxValue = gaugeMaxValue;
}
public ASPxGaugeControl Gauge { get; set; }
public string GaugeValue { get; set; }
public string GaugeDataType { get; set; }
public float GaugeMinValue { get; set; }
public float GaugeMaxValue { get; set; }
}
Heres how I declare my controls, and toward the bottom you can see where I add them, to the list, and session. Any ideas why this is occuring? Do I need to do something special to store a control in session?
ASPxDockPanel dockPanel = CreateDockPanel(layoutName, tableCellId, controlType, controlData, controlName);
ASPxGaugeControl boldGaugeControl = new ASPxGaugeControl();
string dockZonePanelId = "dockPanel" + tableCellId[tableCellId.Length - 1] + layoutName.Replace(".xml", "");
//boldGaugeControl.ID = "gaugeControl" + dockZonePanelId;
boldGaugeControl.ID = "gaugeControl" +gaugeVal;
boldGaugeControl.ClientInstanceName = "gaugeControl" +gaugeVal; // +gaugeVal;// dockZonePanelId + layoutName.Replace(".xml", "");
boldGaugeControl.EnableClientSideAPI = true;
// Creates a new instance of the CircularGauge class and adds it
// to the gauge control's Gauges collection.
CircularGauge circularGauge = (CircularGauge)boldGaugeControl.AddGauge(GaugeType.Circular);
// Adds the default elements (a scale, background layer, needle and spindle cap).
circularGauge.AddDefaultElements();
// Changes the background layer's paint style.
ArcScaleBackgroundLayer background = circularGauge.BackgroundLayers[0];
background.ShapeType = BackgroundLayerShapeType.CircularFull_Style2;
// Customizes the scale's settings.
ArcScaleComponent scale = circularGauge.Scales[0];
scale.MinValue = minimumGaugeValue;
scale.MaxValue = maximumGaugeValue;
scale.Value = value;
scale.MajorTickCount = 6;
scale.MajorTickmark.FormatString = "{0:F0}";
scale.MajorTickmark.ShapeType = TickmarkShapeType.Circular_Style1_2;
scale.MajorTickmark.ShapeOffset = -9;
scale.MajorTickmark.AllowTickOverlap = true;
scale.MinorTickCount = 3;
scale.MinorTickmark.ShapeType = TickmarkShapeType.Circular_Style2_1;
scale.AppearanceTickmarkText.TextBrush = new SolidBrushObject(Color.Gray);
// Changes the needle's paint style.
ArcScaleNeedleComponent needle = circularGauge.Needles[0];
needle.ShapeType = NeedleShapeType.CircularFull_Style3;
// Adds the gauge control to the Page.
boldGaugeControl.Width = 150;
boldGaugeControl.Height = 150;
boldGaugeControl.AutoLayout = true;
boldGaugeControl.ControlStyle.BackColor = Color.Black;
boldGaugeControl.EnableClientSideAPI = true;
boldGaugeControl.ClientIDMode = ClientIDMode.Static;
boldGaugeControl.Value = "25";
dockPanel.Controls.Add(boldGaugeControl);
BoldGauge gauge = new BoldGauge(boldGaugeControl, gaugeValue, controlData, minimumGaugeValue, maximumGaugeValue);
boldGauges.Add(gauge);
Session.Add("GaugesList", boldGauges);
I attempt to retrieve them in this call, which does get the values, but not all attributes are
protected void UpdateGauges()
{
List<BoldGauge> boldGauges = (List<BoldGauge>)Session["GaugesList"];
if (boldGauges != null)
{
foreach (BoldGauge gauge in boldGauges)
{
ArcScaleComponent scale = GetGaugeScale(gauge.Gauge, 0, 0);
scale.BeginUpdate();
float newValue = new Random().Next(100);
scale.Value = newValue;
scale.EndUpdate();
}
}
}
Behavior is semi-expected – just don’t do that.
Storing non-serializable objects in session state is generally not a god idea. It mostly works in case of in-proc provider and totally fails when using SQL provider.
In addition storing controls that expect page object to be around will not work when taken out from session state on next request since old Page (or parent control) object is destroyed along with previous requests. The control is also could have de-initialized itself since its parent is destroyed.