当我从ddl中选择文本框时,我有填充文本框的代码。 这很有用,并且在我的页面加载方法中。
protected void Page_Load(object sender, EventArgs e)
{
//drp_Customer.ClearSelection();
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
};
drp_Customer.DataTextField = "FirstName";
drp_Customer.DataValueField = "CustomerId";
drp_Customer.DataSource = allCustomers.ToList();
drp_Customer.DataBind();
if (drp_Customer.SelectedItem.Text == " -- Select Customer -- ")
{
lbl_message.Text = "Please select a customer to update";
}
else
{
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;
}
}
}
}
我在页面上有一个更新按钮,用于更新,但由于此代码有效,因此已停止工作。
protected void btn_Update_Click(object sender, EventArgs e)
{
using (CustomerDataContext obj = new CustomerDataContext())
{
Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32(drp_Customer.SelectedValue));
// Check If any are empty
// ======================
if (myCust != null)
{
myCust.FirstName = txt_FirstName.Text;
myCust.Surname = txt_Surname.Text;
myCust.HouseNumberName = txt_HouseNumber.Text;
myCust.Address = txt_Address.Text;
myCust.Town = txt_Town.Text;
myCust.Telephone = txt_Telephone.Text;
myCust.Postcode = txt_Postcode.Text;
}
obj.SubmitChanges();
}
}
我正在查询同一个customerId,当我单步执行代码时 // myCust.FirstName = txt_FirstName.Text; 当我改变它时不会改变。
页面是否需要刷新以实现更改? 或者我是否需要以某种方式在页面加载中使用它?
答案 0 :(得分:0)
您似乎正在重新加载Page_Load
中的所有控件,尝试在!Page.IsPostback
方法中添加Page_Load
。像这样:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
//Your code goes here.
}
}