我动态创建了几个仪表控件,将它们存储到一个类中,然后将它们添加到列表和会话变量中以供稍后检索。当我尝试从会话状态加载仪表时,未设置某些控制属性,例如“gauge.Gauge.Value”引发了“System.NullReferenceException”类型的异常。我的课看起来像这样:
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; }
}
下面是我声明控件的方式,在底部,您可以看到我将它们添加到列表的位置、列表和会话。任何想法为什么会发生这种情况?我需要做一些特别的事情来在会话中存储控件吗?
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);
我尝试在此调用中检索它们,它确实获取了值,但并非所有属性都是
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();
}
}
}