I am using c# asp.net and c# where i try to filter an amchart using a dropdown listbox. But no matter what i select i get the value f the first element, I tried !ispostback, but no luck,
The following is the frontend code,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" EnableEventValidation = "false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>amCharts examples</title>
<link rel="stylesheet" href="http://localhost/style1.css" type="text/css">
<script src="http://localhost/amcharts.js" type="text/javascript"></script>
<script type="text/javascript">
var chart;
var chartData = JSON.parse('<%=sjson%>');
AmCharts.ready(function () {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "Ip";
chart.startDuration = 1;
//scrollbar definition
var chartScrollbar = new AmCharts.ChartScrollbar();
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.labelRotation = 90;
categoryAxis.gridPosition = "Count";
// value
// in case you don't want to change default settings of value axis,
// you don't need to create it, as one value axis is created automatically.
// GRAPH
var graph = new AmCharts.AmGraph();
graph.valueField = "Count";
graph.balloonText = "[[category]]: [[value]]";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 0.8;
//add scrollbar to graph
chartScrollbar.graph = graph;
chartScrollbar.scrollbarHeight = 40;
chartScrollbar.color = "#000000";
chartScrollbar.autoGridCount = true;
chart.addChartScrollbar(chartScrollbar);
chart.addGraph(graph);
chart.write("chartdiv");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="chartdiv" style="width: 100%; height: 400px;"></div>
<div>
<asp:DropDownList ID="DropDownListISP" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownListISP_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>
And following is my code behind,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data;
public partial class Default : System.Web.UI.Page
{
public string sjson;
string connStr = "server=localhost;Database=db_tav;Uid=root;Pwd=pass;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
populateList();
filterData("etisalat");
}
}
private void populateList() {
MySqlConnection connPopulate = new MySqlConnection(connStr);
connPopulate.Open();
MySqlCommand cmd = new MySqlCommand();
cmd = connPopulate.CreateCommand();
cmd.CommandText = "SELECT DISTINCT isp FROM tbl_correlateTest WHERE isp !=''";//
MySqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
DropDownListISP.DataSource = ddlValues;
DropDownListISP.DataValueField = "isp";
DropDownListISP.DataTextField = "isp";
DropDownListISP.DataBind();
connPopulate.Close();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
private void filterData(string isp) {
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT IP, conLvl FROM tbl_correlateTest WHERE isp LIKE ?ispVal order by ip asc";//
cmd.Prepare();
cmd.Parameters.Add("?ispVal", MySqlDbType.VarChar, 100).Value = "%" + isp + "%";
MySqlDataReader readIp = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList conRc = new ArrayList();
while (readIp.Read())
{
string ipVal = readIp.GetString(0);
string conLvlVal = readIp.GetString(1);
conRc.Add(new Confidence(ipVal, conLvlVal));
}
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
sjson = oSerializer.Serialize(conRc);
conn.Close();
}
protected void DropDownListISP_SelectedIndexChanged(object sender, EventArgs e)
{
string isp = DropDownListISP.SelectedValue.ToString();
isp = isp.Trim();
filterData(isp);
}
}
PLEASE help me on this one, i tried everything i can but this listbox always returns the first value 🙁 thank you very much.. 🙂
EDIT:
I can retrieve the selected item value if i perform the following instead of data bind.
MySqlDataReader readIsp = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (readIsp.Read())
{
string ispVal = readIsp.GetString(0);
ispVal = ispVal.Trim();
DropDownListISP.Items.Add(ispVal);
}
any idea what the issue is?? just for information.. 🙂
If binding the data is raising the issue and adding the items is not, than it means that
populateListis being called, and when dropdown is binded again, previous selection is washed out. Put a break point in thepopulateListmethod and see if it gets hit when you do a post back from dropdown.