簡體   English   中英

為什么我的下拉列表顯示舊值而不是null?

[英]Why is my drop down list showing old values instead of null?

我有一個C#.NET Web程序,其中包含制造商和汽車型號的下拉列表。 每當您單擊制造商時,都應為您提供他們的模型。 只要有問題的制造商有許多型號,這些型號的清單就可以裝訂好。 在模型數據庫表中單擊沒有模型的制造商后,“模型”下拉列表仍保留先前制造商的值,而不是綁定空值並清除沒有模型的制造商的下拉列表選項。 有問題的函數如下所示:

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綁定到下拉列表? 任何幫助將不勝感激,並給予正確答案。 謝謝。

使用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();             
}

如何使它變得更好:(一位嬰兒大師):

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
}

使用此代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM