我在我的ASP.NET 4.0應用程序中使用ext.net 1.3控件。我的Web窗體上有幾個ComboBox控件。該頁面應該執行兩個任務,插入和更新。保存新記錄時沒有問題,但是當我嘗試用現有數據庫值填充ComboBox控件時,會彈出各種問題。最令人不安的是這一個:爲什麼ComboBox項目需要重新選擇才能獲得價值?
ComboBox顯示來自數據庫的文本,但它既沒有被填充,也沒有我能夠選擇ComboBox值成員。這是因爲它沒有填充。我已經編寫代碼來在頁面加載事件中填充ComboBox。
我使用此代碼來接從數據庫中的值,並顯示在組合框:
string Query = "SELECT CustName FROM CustomerMaster WHERE CustID = " + Session["CustomerID"];
var result = DB.Single<Customer>(Query);
CustomerComboBox.setValue = result.CustName;
此代碼成功地檢索ComboBox中客戶名稱和顯示。它沒有做的是它不是從組合框項目列表中選擇,也沒有填充組合框。
如果我嘗試使用檢索文本值的成員:
CustomerComboBox.SelectedItem.Value;
提示錯誤。
爲了使它工作,我需要再次點擊組合框使其填充,而不是從列表中手動選擇相同的客戶名稱來選擇值。
如何擺脫這個問題?
- 編輯 -
的代碼來填充ext.net組合框是這樣的:
public void FillExtComboList(string ParameterFlag, ComboBox DropDownName)
{
string Query = "";
using (TransactionScope transactionScope = new TransactionScope())
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cncustomer"].ConnectionString.ToString()))
{
con.Open();
SqlDataReader dr;
try
{
if (ParameterFlag == "Customer")
{
Query = "SELECT CustName FROM CustomerMaster";
}
//Check whether the Drop Down has existing items. If YES, empty it.
if (DropDownName.Items.Count > 0)
DropDownName.Items.Clear();
SqlCommand cmd = new SqlCommand(Query, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
Ext.Net.ListItem extLI = new Ext.Net.ListItem();
extLI.Text = dr[0].ToString();
DropDownName.Items.Add(extLI);
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
con.Close();
// RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
}
} /* End of SQL Connection */
transactionScope.Complete();
} /* End of Transaction Scope */
}
在頁加載事件,組合框控件填充有上述方法。
正如我所說的組合框填充在pageLoad的。用代碼編輯帖子。 – RKh 2012-03-14 12:39:05
請忽略DropDownName和CustomerComboBox的最後一行混淆。前者是參數,而後者是ComboBox的名稱。 – RKh 2012-03-15 04:53:47