Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


ASP.net Timer wipes out charts

Posted on 2014-02-15
Medium Priority
Last Modified: 2014-02-17

I am using the following code to bind data to GridViews and then dynamically build charts off each GridView. I have just add a timer to the page that has no code at all on it's tick
event but it bizarrely wipes the charts clean. Why would this be?

 Sub oChart(ByVal oConnectionString As String, ByVal SQL As String, ByVal RowFields As String, ByVal DataField As String, ByVal sAggregate As String, _
               ByVal ColumnFields As String, ByVal oChartType As String, ByVal oRow As Integer, ByVal oColumn As Integer, ByVal sWidth As String, _
               ByVal sHeight As String, ByVal blnShow_Data_With_Chart As Boolean, blnFlip_Axes As Boolean)

        ' Initialize the Chart object    
        Dim chart1 As New Chart()

        If Me.lblCharts.Text = "Charts" Then
            Me.lblCharts.Text = chart1.ID
            Me.lblCharts.Text = Me.lblCharts.Text & chart1.ID
        End If
        'chart1.ID = "chart1"
        'chart1.ID = "chart1" & CStr(Me.Table1.Controls.Count)

        'Add to do code here
        If Not IsPostBack Then
            ' Initialize objects and elements
            Dim chartArea1 As New ChartArea()
            Dim legend1 As New Legend()
            'Dim series1 As New Series()
            'Dim series2 As New Series()
            'Dim series3 As New Series()
            Dim arrSeries(1000) As Series

            ' Set the Chart Properties
            chart1.Width = CInt(sWidth)
            chart1.Height = CInt(sHeight)

            chart1.Palette = ChartColorPalette.EarthTones
            chart1.Palette = ChartColorPalette.Excel
            chart1.BackColor = System.Drawing.Color.Gray
            chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss

            chart1.BorderlineColor = System.Drawing.Color.Gray
            chart1.BorderlineDashStyle = ChartDashStyle.Solid
            chart1.BorderlineWidth = 4 'MDB

            ' Set the ChartArea properties
            chartArea1.Name = "Default"
            chartArea1.BackColor = System.Drawing.Color.Gray

            'Enable AJAX features such as zooming and scrolling for the ChartArea
            'chartArea1.CursorX.UserEnabled = True
            'chartArea1.CursorY.UserEnabled = True

            ' Add the ChartArea to the Chart

            ' Set the Legend properties
            legend1.Name = "Default"
            legend1.Docking = Docking.Bottom
            legend1.LegendStyle = LegendStyle.Row
            legend1.Alignment = System.Drawing.StringAlignment.Center

            legend1.BackColor = System.Drawing.Color.Transparent
            legend1.BorderColor = System.Drawing.Color.Black
            legend1.BorderWidth = 1

            ' Add the Legend to the Chart

            ' Set the Series properties
            'series1.BorderColor = System.Drawing.Color.DarkSlateGray
            'series1.Name = "Series1"
            'series1.ChartType = SeriesChartType.SplineArea

            'series2.BorderColor = System.Drawing.Color.DarkSlateGray
            'series2.Name = "Series2"
            'series2.ChartType = SeriesChartType.Column

            'series3.Name = "Series3"
            'series3.BorderWidth = 3
            'series3.ChartType = SeriesChartType.Spline

            ' Add the Series to the Chart

            ' Add points to each series.
            Dim rnd As New Random()

            'For Each ser As Series In chart1.Series
            'For i As Integer = 0 To 12
            'ser.Points.AddY(rnd.[Next](5, 40))

            'chart1.DataSource = dt

            'GridView1.DataSource = dt
            Dim oGridView As New GridView
            'oGridView.DataSource = dt
            'First get the Aggregate function
            Dim oAggregate As AggregateFunction
            If sAggregate = "Count" Then
                oAggregate = AggregateFunction.Count
            ElseIf sAggregate = "Sum" Then
                oAggregate = AggregateFunction.Sum
            ElseIf sAggregate = "First" Then
                oAggregate = AggregateFunction.First
            ElseIf sAggregate = "Last" Then
                oAggregate = AggregateFunction.Last
            ElseIf sAggregate = "Average" Then
                oAggregate = AggregateFunction.Average
            ElseIf sAggregate = "Max" Then
                oAggregate = AggregateFunction.Max
            ElseIf sAggregate = "Min" Then
                oAggregate = AggregateFunction.Min
            ElseIf sAggregate = "Exists" Then
                oAggregate = AggregateFunction.Exists
            End If

            Dim dt As DataTable = SQL_Data_Table.GetSQLDataTable(oConnectionString, SQL)
            Dim pvt As New Pivot(dt)
            oGridView.DataSource = pvt.PivotData(RowFields, DataField, oAggregate, ColumnFields)
            If blnShow_Data_With_Chart = True Then
                oGridView.Visible = True
                oGridView.Visible = False
            End If

            If Me.lblChartGridViews.Text = "Chart GridViews" Then
                Me.lblChartGridViews.Text = oGridView.ID
                Me.lblChartGridViews.Text = Me.lblChartGridViews.Text & oGridView.ID
            End If
            'First add declare Update Panel then add GridView to it - finally add the update panel to the table cell

            Dim T As Table
            'T = Me.Controls("Table" & oRow)
            If oRow = 0 Then
                T = Me.Table1
            ElseIf oRow = 1 Then
                T = Me.Table2
            ElseIf oRow = 2 Then
                T = Me.Table3
            ElseIf oRow = 3 Then
                T = Me.Table4
            ElseIf oRow = 4 Then
                T = Me.Table5
            ElseIf oRow = 5 Then
                T = Me.Table6
            ElseIf oRow = 6 Then
                T = Me.Table7
            ElseIf oRow = 7 Then
                T = Me.Table8
            ElseIf oRow = 8 Then
                T = Me.Table9
            ElseIf oRow = 9 Then
                T = Me.Table10
            End If
            Dim up As New UpdatePanel
            'up.ID = "up" & CStr(Me.Table1.Controls.Count + Me.Table2.Controls.Count + _
            'Me.Table3.Controls.Count + Me.Table4.Controls.Count + Me.Table5.Controls.Count + _
            'Me.Table6.Controls.Count + Me.Table7.Controls.Count + Me.Table8.Controls.Count + _
            'Me.Table9.Controls.Count + Me.Table10.Controls.Count)

            Dim oCount As Integer = 0
            Dim oData As Double
            Dim strRowHeader As String
            Dim strColumnHeader As String
            'For Each ser As Series In chart1.Series
            'ser.Label = ser.Name

            chart1.BackColor = Drawing.Color.Navy
            chart1.BackGradientStyle = GradientStyle.LeftRight
            chartArea1.BackColor = Drawing.Color.DarkGreen
            chartArea1.BackGradientStyle = GradientStyle.LeftRight

            For r As Integer = 0 To oGridView.Rows.Count - 1

                oCount = 0
                strRowHeader = oGridView.Rows(r).Cells(0).Text
                arrSeries(r) = New Series
                arrSeries(r).BorderColor = System.Drawing.Color.MidnightBlue
                arrSeries(r).BorderWidth = 4 'Changes width of ine in line chart
                'arrSeries(r).BackImageTransparentColor = Drawing.Color.Aqua
                arrSeries(r).Name = strRowHeader

                If oChartType = "Chart_Line" Then
                    arrSeries(r).ChartType = SeriesChartType.Line
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkGreen
                ElseIf (oChartType = "Chart_Spline") Then
                    arrSeries(r).ChartType = SeriesChartType.Spline
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.MintCream
                ElseIf (oChartType = "Chart_Bar") Then
                    arrSeries(r).ChartType = SeriesChartType.Bar
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkRed
                ElseIf (oChartType = "Chart_StackedBar") Then
                    arrSeries(r).ChartType = SeriesChartType.StackedBar
                    chartArea1.BackColor = Drawing.Color.Orange
                    chart1.BackColor = Drawing.Color.DarkRed
                ElseIf (oChartType = "Chart_Column") Then
                    arrSeries(r).ChartType = SeriesChartType.Column
                    chart1.BackColor = Drawing.Color.Orange
                    chartArea1.BackColor = Drawing.Color.OrangeRed
                ElseIf (oChartType = "Chart_StackedColumn") Then
                    arrSeries(r).ChartType = SeriesChartType.StackedColumn
                    chart1.BackColor = Drawing.Color.DarkSlateGray
                    chartArea1.BackColor = Drawing.Color.MintCream
                ElseIf (oChartType = "Chart_Pie") Then
                    arrSeries(r).ChartType = SeriesChartType.Pie
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkSlateGray
                ElseIf (oChartType = "Chart_Doughnut") Then
                    arrSeries(r).ChartType = SeriesChartType.Doughnut
                    chart1.BackColor = Drawing.Color.DarkRed
                    chartArea1.BackColor = Drawing.Color.Chocolate
                ElseIf (oChartType = "Chart_Doughnut") Then
                    arrSeries(r).ChartType = SeriesChartType.Funnel
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.Silver
                ElseIf (oChartType = "Chart_Area") Then
                    arrSeries(r).ChartType = SeriesChartType.Area
                    chart1.BackColor = Drawing.Color.DarkGreen
                    chartArea1.BackColor = Drawing.Color.DarkTurquoise
                ElseIf (oChartType = "Chart_Funnel") Then
                    arrSeries(r).ChartType = SeriesChartType.Funnel
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.Silver
                ElseIf (oChartType = "Chart_Point") Then
                    arrSeries(r).ChartType = SeriesChartType.Point
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.White
                ElseIf (oChartType = "Chart_Polar") Then
                    arrSeries(r).ChartType = SeriesChartType.Polar
                    chart1.BackColor = Drawing.Color.DodgerBlue
                    chartArea1.BackColor = Drawing.Color.Red
                ElseIf (oChartType = "Chart_Radar") Then
                    arrSeries(r).ChartType = SeriesChartType.Radar
                    chart1.BackColor = Drawing.Color.Olive
                    chartArea1.BackColor = Drawing.Color.OliveDrab

                End If

                'arrSeries(r).ChartType = SeriesChartType.Spline

                'arrSeries(r).ChartType = SeriesChartType.Radar
                'chart1.BackColor = Drawing.Color.Olive
                'chartArea1.BackColor = Drawing.Color.OliveDrab

                'arrSeries(r).ChartType = SeriesChartType.Pyramid
                'arrSeries(r).ChartType = SeriesChartType.Doughnut
                'arrSeries(r).ChartType = SeriesChartType.Pie
                'arrSeries(r).ChartType = SeriesChartType.Bubble
                'arrSeries(r).ChartType = SeriesChartType.StackedArea100
                'arrSeries(r).ChartType = SeriesChartType.Funnel 'Goes with next one
                arrSeries(r).BackGradientStyle = GradientStyle.DiagonalLeft

                'arrSeries(r).ChartType = SeriesChartType.Spline
                'arrSeries(r).BackGradientStyle = GradientStyle.HorizontalCenter


                For c As Integer = 1 To oGridView.Rows(0).Cells.Count - 1
                    'only add a point if the item is numeric
                    If IsNumeric(oGridView.Rows(r).Cells(c).Text) Then

                        oData = CDbl(oGridView.Rows(r).Cells(c).Text)
                        'strRowHeader = oGridView.Rows(r).Cells(0).Text
                        strColumnHeader = oGridView.HeaderRow.Cells(c).Text
                        'ser.Points.Item(oCount).AxisLabel = strRowHeader & " " & strColumnHeader
                        arrSeries(r).Points.Item(oCount).AxisLabel = strColumnHeader
                        'ser.Points.Item(oCount).LabelAngle = 10
                        oCount = oCount + 1
                    End If

            'chartArea1.Area3DStyle.Rotation = 180

            chartArea1.AxisX.LabelStyle.Enabled = True
            chartArea1.AxisX.IsLabelAutoFit = True
            chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30

            'Only show GridView if specified

            'Preserve the chart's state during callbacks
            'chart1.CallbackStateContent = CallbackStateContent.All
        End If

        ' You MUST add the Chart to a form. By default, form1 is created on aspx pages.

    End Sub

Open in new window

Question by:Murray Brown
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 39863346
As mentioned in your other question, dynamic controls need to be recreated on every page postback. Timer is causing a postback to execute its tick event which then removes the charts because these are not being readded. Refer to links provided in other question.

Author Closing Comment

by:Murray Brown
ID: 39864606
Thanks very much

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question