下拉列表不接受SelectedValue

时间:2013-07-14 13:05:44

标签: c# asp.net drop-down-menu ado.net

我有一个奇怪的问题。我用这段代码填写下拉列表

   MySqlCommand cmdAreaPassive = new MySqlCommand();
    cmdAreaPassive.CommandText = "Select BolgeID, BolgeAdi, AktiPasif ";
    cmdAreaPassive.CommandText += "From tbl_bolgeler ";
    cmdAreaPassive.CommandText += "Where AktiPasif = 1 ";
    cmdAreaPassive.CommandText += "Order By BolgeAdi ASC ";
    cmdAreaPassive.Connection = con;

    MySqlDataAdapter drAreaPassive = new MySqlDataAdapter(cmdAreaPassive);
    DataSet dsAreaPassive = new DataSet();
    drAreaPassive.Fill(dsAreaPassive);

    ddlAreaDelete.DataSource = dsAreaPassive;
    ddlAreaDelete.DataTextField = "BolgeAdi";
    ddlAreaDelete.DataValueField = "BolgeID";
    ddlAreaDelete.DataBind();

    Response.Redirect(Request.RawUrl);

我尝试使用此代码删除项目

        MySqlCommand cmdDelete = new MySqlCommand();
        cmdDelete.CommandText = "Update tbl_bolgeler ";
        cmdDelete.CommandText += "Set AktiPasif = @AktiPasif ";
        cmdDelete.CommandText += "Where BolgeID = @BolgeID ";
        cmdDelete.Connection = con;

        cmdDelete.Parameters.AddWithValue("@BolgeID", ddlAreaDelete.SelectedItem.Value);
        cmdDelete.Parameters.AddWithValue("@AktiPasif", "0");

        cmdDelete.ExecuteNonQuery();

一切正常,直到选择对象。它必须采用选定的值,但它需要选择索引。我尝试SelectedItem.Value和SelectedValue但它们都不起作用。抱歉我的英文不好,谢谢。

1 个答案:

答案 0 :(得分:0)

我想,我解决了这个问题

我的代码就像那样

  cmdDelete.Parameters.AddWithValue("@BolgeID", ddlAreaDelete.SelectedItem.Value);

我偶然这样

        int selectedIndex = ddlAreaDelete.SelectedIndex;
        if (selectedIndex >= 0)
        {
         cmdDelete.Parameters.AddWithValue("@BolgeID",ddlAreaDelete.Items[selectedIndex].Value);
        }

但我仍然不知道是什么原因