下拉列表框始终选择第一个值

时间:2012-09-04 10:28:25

标签: c# asp.net html webforms

我正在使用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);

    }

任何想法是什么问题?仅供参考.. :))

1 个答案:

答案 0 :(得分:0)

如果绑定数据引发问题并且添加项目不是,则表示正在调用populateList,并且当下拉列表再次绑定时,之前的选择将被清除。在populateList方法中设置一个断点,看看当你从下拉列表发回帖子时它是否会被击中。