我正在尝试填充一个 ddl(它有效),当我选择“名字”时,会填充文本框以进行编辑。当我逐步执行程序时,(drp_Customer.SelectedItem.Value)始终是“23”,这是我的数据库中的最后一个“Id”。文件被填充,但包含 23 的详细信息..
我想我需要确保 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;
}
}