我在C#中有一個事件,它使用來自不同數據庫的關聯值填充類文件(database.cs)中的下拉列表通過上面提到的類文件中包含的oracle存儲過程。這是我目前使用的代碼:'ColConfigSubsystem.Database.GetChromeMakeByYear(int)'的最佳重載方法匹配有一些無效參數
///<summary>
///Populates the model dropdownlist with available chrome data associated with
///the year selected
/// </summary>
private void PopulateChromeModel()
{
//define a counter
int itemCounter = 1;
//create a database object
Database cmake = new Database();
//call GetChromeMakeByYear to retrieve the available models according to
//the year
DataTable table = cmake.GetChromeMakeByYear(ddlVehicleYear.SelectedItem);
Trace.Write("populating models");
//create a flag showing whether an item should be selected. Preset it to false
bool selected = false;
//we may need to select, then later deselect, an item based on the model.
int selectedItemByModel = 0;
//preset the current selected code to ""
string currentSelectedCode = "";
//define a flag to indicate whether we've already selected an item. Preset it to false.
bool hasSelected = false;
//create a list item for the 0 position
ListItem firstItem = new ListItem("-- SELECT --", "");
//first see if there is a currently selected item. If so, set the current selected model.
//this is done because we have to clear all of the selected items before adding the new list.
//but we want to be able to select the model that is already selected.
if (ddlVehicleModel.SelectedIndex > 0)
{
currentSelectedCode = ddlVehicleModel.SelectedValue;
}
//clear any items from the list
ddlVehicleModel.Items.Clear();
//add the first item
ddlVehicleModel.Items.Add(firstItem);
//loop through the table and add items for each row.
foreach (DataRow row in table.Rows)
{
//get this record's chrome id
string id = row["CHROME_ID"].ToString();
//get this record's make
string make = row["CHROME_MAKE"].ToString();
//set a flag specifying whether the item should be selected based on year
bool selectBasedOnYear = false;
if (currentSelectedCode == id)
{
selected = true;
}
else
{
selected = false;
selectBasedOnYear = false;
}
Trace.Write(string.Format("{0}: {1}: {2}: {3}", id, make, selected, selectBasedOnYear));
//create a new list item for this model
ListItem newItem = new ListItem(id, make);
//if we have thrown either selected flag and we have not already selected an item,
//mark this option as selected
if ((selected || selectBasedOnYear) && !hasSelected)
{
Trace.Write("-- Either selected or selectedBasedOnYear was true, and hasSelected was false");
//first make sure the first item is deselected
firstItem.Selected = false;
Trace.Write("-- deselected the first item.");
//next deselect and items that were selected due to year. This allows the user selected
//region to override the default model for year.
if (selectBasedOnYear != null)
{
Trace.Write(string.Format(" -- deselecting item {0}, which was selected due to year", selectBasedOnYear));
ddlVehicleMake.Items[selectedItemByModel].Selected = false;
}
//select this item
newItem.Selected = true;
Trace.Write(" -- selected the current item");
//only throw the hasSelected flag if this was a user-selected region
if (selected)
{
hasSelected = true;
Trace.Write(" -- set hasSelected to true");
}
}
//add the model to the list
ddlVehicleMake.Items.Add(newItem);
itemCounter++;
}
//if there's no items selected and we have more than just the default item,
//default to the first item
if (ddlVehicleMake.Items.Count > 0 && !hasSelected && selectedItemByModel == 0)
{
ddlVehicleMake.SelectedIndex = 1;
}
}
的錯誤是在下面的行拋出:
//call GetChromeMakeByYear to retrieve the available models according to
//the year
DataTable table = cmake.GetChromeMakeByYear(ddlVehicleYear.SelectedItem);
我已經包括了我的在線評論和總結,以幫助這個事件的範圍。我不太確定錯誤的原因是什麼。
確定我沒有像你說的,它拋出另一個錯誤「無法將類型「System.Web.UI.WebControls.ListItem 'to'int'。我是否必須將其轉換爲int? – MaximusPrime
@MaximusPrime:嘗試'SelectedValue'而不是'SelectedItem' –
嗯......我嘗試使用SelectedValue,並且拋出了另一個錯誤'Can not convert type'string' 'int' – MaximusPrime