2012-07-02 27 views
0

我动态创建几个仪表控件将它们存储到一个类,然后将它们后来添加到列表和会话变量进行检索。当我尝试从会话状态加载测量仪时,某些控制属性未设置,例如'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(); 
      } 
     } 
    } 

回答

1

行为是半市场预期 - 只是没有做到这一点。

在会话状态下存储不可序列化的对象通常不是上帝的想法。它主要适用于进程内提供程序,并且在使用SQL提供程序时完全失败。

在这种预期的页面对象是各地的时候,因为旧的页面(或父控件)对象与以前的请求一起被毁坏的下一个请求从会话状态取出行不通除了存储控制。由于其父母被摧毁,控制也可能已经自行解除初始化。

+0

谢谢Alexa的,你可以推荐用于存储我的仪表控件,而无需使用会话变量更好的办法? – CodeMan5000

+1

@LarryBargers,只需存储你需要的值。请注意,如果您创建控件正确的WebForms引擎将保留后背上之间性能的照顾... –

+2

我想阿列克谢意味着,如果你创建的Page_Load之前,你的控制,并采取ViewState中的优势,你不应该担心页面回发之间存储值。 – TheGeekYouNeed