为什么我的下拉列表显示旧值而不是null?

时间:2014-10-16 04:03:17

标签: c# asp.net

我有一个C#.NET网络程序,其中包含制造商和汽车模型的下拉列表。每当您点击制造商时,它应该为您提供他们的模型。只要有问题的制造商有许多型号,模型列表就可以很好地绑定。一旦我单击模型数据库表中没有模型的制造商,模型下拉列表仍然保留前一个制造商的值而不是绑定null并清除没有模型的制造商的下拉列表选项。 有问题的功能如下所示:

public void BindModels(int manufacturer)
{
    int numberOfModels;
    string strConnectionString =  ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
    SqlConnection conn = new SqlConnection(strConnectionString); // Connect to Carsales database
    conn.Open();                            // Select all models for a particular make
    string com = "SELECT ModelID, ModelName From VehiclesModels Where ManufacturerID = " + manufacturer + "  ";
    SqlDataAdapter adpt = new SqlDataAdapter(com, conn);// Convert the database string to an sqldata adapter
    DataTable dt = new DataTable();      // Create a data table for binding
    numberOfModels = adpt.Fill(dt);      // Determine number of models for this manufacturer before binding
    if (numberOfModels > 0)               // Fill the data table with the open Sql connection
    {                                    // If models exist for this manufacturer
        drpModel.DataSource = dt;        // dropdownlist data source is newly created table
        drpModel.DataTextField = "ModelName"; // relate database fields to dropdownlist fields
        drpModel.DataValueField = "ModelID";  // Model ID goes in the value field
        drpModel.DataBind();                  // Data bind to the dropdown list in the front end
        //hdnModelID.Value = "0";             // Indicate an unselected model exists
        if (numberOfModels == 1)              // If only one model (Special case)
        {
          BindGrid4BodyDetails(Convert.ToInt32(drpModel.SelectedValue)); // Bind the grid for body details for this model
        }
        hdnModelID.Value = drpModel.SelectedValue; // Indicate the only possible selection as the current ModelId value
    }
    else
    {                                // If no models exist for this manufacturer
        hdnModelID.Value = "-1";     // Indicate this via hdnModelID value
        drpModel.DataSource = null;  // Bind null to the models to indicate no models
        drpModel.DataTextField = "ModelName"; // relate database fields to dropdownlist fields
        drpModel.DataValueField = "ModelID";  // Model ID goes in the value field
        drpModel.DataBind();         // and clear any previous model data bound
    }
    conn.Close();             // Close the connection to the carsales database
}

我在else语句中做错了吗?为什么它没有将null绑定到下拉列表?任何帮助将不胜感激,正确的答案将得到回报。感谢。

3 个答案:

答案 0 :(得分:1)

使用drpModel.Items.Clear();会明确清除您的商品,但您不应该这样做。您应该能够始终将DataTable dt绑定到下拉列表,即使结果集中没有项目也是如此。在这种情况下,DropDownList控件将不包含任何项目。

将您的代码更改为以下内容:

public void BindModels(int manufacturer)
{
    int numberOfModels;
    string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
    SqlConnection conn = new SqlConnection(strConnectionString); 
    conn.Open();                            
    string com = "SELECT ModelID, ModelName From VehiclesModels Where ManufacturerID = " + manufacturer + "  ";
    SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
    DataTable dt = new DataTable();      
    numberOfModels = adpt.Fill(dt);      

    // set the DataTable as the DataSource, no items will be added to the DropDownList control if the DataTable has no records
    drpModel.DataSource = dt;               
    drpModel.DataTextField = "ModelName"; 
    drpModel.DataValueField = "ModelID";  
    drpModel.DataBind();                  

    if (numberOfModels == 1)              
    {
        BindGrid4BodyDetails(Convert.ToInt32(drpModel.SelectedValue)); 
    }
    hdnModelID.Value = drpModel.SelectedValue; 

    conn.Close();             
}

答案 1 :(得分:0)

如果能做到更好的话,怎么样:(一个来自Baby Master):

public void BindModels(int manufacturer)
{
    int numberOfModels;
    string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
    SqlConnection conn = new SqlConnection(strConnectionString);  // Connect to Carsales database
    conn.Open();                                   // Select all models for a particular make
    string com = "SELECT ModelID, ModelName From VehiclesModels Where ManufacturerID = " + manufacturer + "  ";
    SqlDataAdapter adpt = new SqlDataAdapter(com, conn); // Convert the database string to an sqldata adapter
    DataTable dt = new DataTable();            // Create a data table for binding
    numberOfModels = adpt.Fill(dt);// Determine number of models for this manufacturer before binding
                                       // Fill the data table with the open Sql connection
    drpModel.DataSource = dt;          // dropdownlist data source is newly created table
    drpModel.DataTextField = "ModelName"; // relate database fields to dropdownlist fields
    drpModel.DataValueField = "ModelID";  // Model ID goes in the value field
    drpModel.DataBind();                  // Data bind to the dropdown list in the front end
    switch (numberOfModels)
    {
        case 0: hdnModelID.Value = "-1";   // If no models exist for this manufacturer, Indicate this via hdnModelID value
                txtMessage.Text = "No models exist for this manufacturer"; // Give user a message.
                break;
        case 1 : BindGrid4BodyDetails(Convert.ToInt32(drpModel.SelectedValue)); // If only one model (Special case)
                                         // Bind the grid for body details for this model
                 hdnModelID.Value = drpModel.SelectedValue;  // Indicate the only possible selection as the current ModelId value
                 break;
        default : break;
    }
    conn.Close();                   // Close the connection to the carsales database
}

答案 2 :(得分:0)

使用此代码。

   if (dt.Rows.Count > 0)               
   {                                     
     drpModel.DataSource = dt;         
     drpModel.DataTextField = "ModelName";  
     drpModel.DataValueField = "ModelID";   
     drpModel.DataBind();  
   }