Desperately seeking assistance! I have spent countless hours researching this issue but have hit a dead end. My dilemma involves using the Google Interactive Charts API within an UpdatePanel to dynamically update data based on dropdown selection changes. However, whenever the UpdatePanel refreshes, the chart suddenly disappears. I've confirmed that JavaScript executes within the updated panel by testing a simple alert, so the problem seems specific to how the Google javascript functions after the update. Could it be that the setOnLoadCallback fails to call drawChart post-UpdatePanel refresh? I'm uncertain if this is the root cause or how to rectify it.
The following code resides in a Public Sub called during Page_Load and when the selected value of the dropdownlist changes:
Dim strOutput As New StringBuilder
strOutput.Append("google.load('visualization', '1.0', { 'packages': ['corechart'] });")
strOutput.Append("google.setOnLoadCallback(drawChart);")
strOutput.Append("function drawChart() {")
strOutput.Append("var data = new google.visualization.DataTable();")
strOutput.Append("data.addColumn('string', 'Domain');")
strOutput.Append("data.addColumn('number', 'Value');")
strOutput.Append("data.addRows([")
Dim sqlSelect As New SqlClient.SqlCommand
sqlSelect.CommandText = String.Format("spAnalytics_ViewsByDomain")
sqlSelect.CommandType = CommandType.StoredProcedure
Dim dtChartData As DataTable = clsDB.FillDT(sqlSelect)
Dim commaCounter As Integer = 1
For Each row As DataRow In dtChartData.Rows
If ddlStat.SelectedValue = "hits" Then
strOutput.Append(String.Format("['{0}',{1}]", row.Item("domain"), row.Item("hit_count")))
ElseIf ddlStat.SelectedValue = "sessions" Then
strOutput.Append(String.Format("['{0}',{1}]", row.Item("domain"), row.Item("session_count")))
End If
If commaCounter <> dtChartData.Rows.Count Then strOutput.Append(",")
commaCounter = commaCounter + 1
Next
strOutput.Append("]);")
strOutput.Append("var options = { 'title': 'Views by domain',")
strOutput.Append("'width': 500,")
strOutput.Append("'height': 300")
strOutput.Append("};")
strOutput.Append("var chart = new google.visualization.PieChart(document.getElementById('chart_div'));")
strOutput.Append("chart.draw(data, options);")
strOutput.Append("}")
ScriptManager.RegisterStartupScript(litDomains, GetType(String), "blahblah", strOutput.ToString, True)