0

我正在使用 c# asp.net 和 c#,我尝试使用下拉列表框过滤 amchart。但是无论我选择什么,我都会得到第一个元素的值,我尝试了!ispostback,但没有运气,以下是前端代码,

    <%@ 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>

以下是我的代码,

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);
    }

}

请帮我解决这个问题,我尽我所能,但这个列表框总是返回第一个值:(非常感谢.. :)


编辑:

如果我执行以下操作而不是数据绑定,我可以检索所选项目的值。

    MySqlDataReader readIsp = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    while (readIsp.Read())
    {
        string ispVal = readIsp.GetString(0);
        ispVal = ispVal.Trim();
        DropDownListISP.Items.Add(ispVal);

    }

知道问题是什么吗?仅供参考.. :)

4

1 回答 1

0

If binding the data is raising the issue and adding the items is not, than it means that populateList is being called, and when dropdown is binded again, previous selection is washed out. Put a break point in the populateList method and see if it gets hit when you do a post back from dropdown.

于 2012-09-04T11:16:16.587 回答