我想填充ddl(它的工作原理),當我選擇'FirstName'時,文本框被填充進行編輯。 當我瀏覽程序時,(drp_Customer.SelectedItem.Value)總是'23',這是我數據庫中最後一個'Id'。 該檔案填充,但23的細節..無法在select語句中獲取正確的值
我想我需要確保ddl的細節匹配。
這是我的代碼。 這只是粗略的,因爲我試圖向某人展示它是如何工作的。
using (CustomerDataContext obj = new CustomerDataContext())
{
// Get fields for drp_Customer
// ===========================
var allCustomers = from c in obj.Customers
orderby c.FirstName
select new
{
c.FirstName,
c.CustomerId
};
foreach (var item in allCustomers)
{
drp_Customer.Items.Add(item.FirstName);
drp_Customer.SelectedItem.Value = item.CustomerId.ToString();
}
drp_Customer.DataBind();
if (drp_Customer.SelectedItem.Text == " -- Select Customer -- ")
{
lbl_message.Text = "Please select a customer to update";
}
else if (drp_Customer.SelectedItem.Text != " -- Select Customer -- ")
{
Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32
(drp_Customer.SelectedItem.Value));
if (myCust != null)
{
txt_FirstName.Text = myCust.FirstName;
txt_Surname.Text = myCust.Surname;
txt_HouseNumber.Text = myCust.HouseNumberName;
txt_Address.Text = myCust.Address;
txt_Town.Text = myCust.Town;
txt_Telephone.Text = myCust.Telephone;
txt_Postcode.Text = myCust.Postcode;
}
}