繁体   English   中英

将控件添加到类并存储到会话不会检索所有控件属性

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM