[英]Adding a control to a class and storing to session doesnt retrieve all control attributes
我動態創建了幾個儀表控件,將它們存儲到一個類中,然后將它們添加到列表和會話變量中,以供以后檢索。 當我嘗試從會話狀態加載儀表時,未設置某些控件屬性,例如,“ 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();
}
}
}
行為是半預期的-不要這樣做。
在會話狀態下存儲不可序列化的對象通常不是一個好主意。 它主要在過程提供程序的情況下起作用,而在使用SQL提供程序時完全失敗。
另外,當下一個請求從會話狀態中取出時,存儲期望頁面對象存在的控件將無法工作,因為舊的Page(或父控件)對象與先前的請求一起被破壞了。 由於控件的父級已銷毀,因此控件本身也可能已取消初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.