-1

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

   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 但它们都不起作用。对不起我的英语不好,谢谢。

4

1 回答 1

0

我想,我解决了这个问题

我的代码是这样的

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

我碰巧就是这样

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

但我还是不知道是什么原因

于 2013-07-14T13:39:05.000 回答