Combobox'OnSelectedIndexChanged'没有为某些值触发

时间:2014-02-27 13:09:46

标签: c# asp.net

我在更新面板中有一个asp组合框,其中autopost back设置为true 在页面加载我填写组合框

public DataTable getProductDetails()
{
    MasterAllocationDB dataHandler = new MasterAllocationDB();
    DataTable dataBoxType = null;
    DataRow row = null;
    try
    {
        dataBoxType = dataHandler.GetBoxType();
        if (dataBoxType != null && dataBoxType.Rows.Count > 0)
        {
            row = dataBoxType.NewRow();
            row["Product"] = "--Select--";
            dataBoxType.Rows.InsertAt(row,0);
            row =  dataBoxType.NewRow();
            row["Product"] = "Other";
            dataBoxType.Rows.InsertAt(row, dataBoxType.Rows.Count);
        }
    }
    catch (Exception ex)
    {
        LogHandler.LogMessageToFile(ex, LogMode.Fatal);
    }

    return dataBoxType;
}

我还有一个onselectedindexchanged事件绑定

protected void productComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string json = null;
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            var s = this.productComboBox.SelectedValue;
            try
            {
                int totalNoOfItems = this.productComboBox.Items.Count;

                // Make sure that Index are between "select a value" and "Other"
                if (this.productComboBox.SelectedIndex > 0 && this.productComboBox.SelectedIndex < totalNoOfItems - 1) 
                {
                    //code here
                }
                json = new JavaScriptSerializer().Serialize(rows);


            }
            catch (Exception ex)
            {
                LogHandler.LogMessageToFile(ex, LogMode.Fatal);
            }

        }

正如您所看到的,除了从DB获取的行之外,我还添加了2行。数据行正确绑定,因为我可以在输出的组合框值中看到相同的行

跑完后我注意到每当我选择“其他”时,组合框内的文字就会被改变 “--select--”并且控件永远不会到达onselectedindexchanged

它可以很好地适用于所有其他情况。可能是什么原因?

1 个答案:

答案 0 :(得分:0)

好的,我把它整理出来了 问题是我只为组合框设置了文本而不是选项的值。

将代码更改为

  row["Product"] = "--Select--";
  row["ProductID"] = "0";

  row["Product"] = "Other";
  row["ProductID"] = "10";

ProductId给出了组合框的值 快乐的编码