2012-11-23 74 views
0

我有一个填充了从数据库检索的值的表单。该表单有一个提交按钮,应该在点击数据库时更新数据库中的相应字段。更新表格保留原始值

问题是无论我做什么值都不会在数据库中更新。我正在使用一个存储过程,并检查了两次,它工作正常,我也尝试了硬编码插入值的技巧 - 也可以。

我的猜测是,一旦该字段从Page_Load上的数据库填充,即使我改变它的值,它的值仍然存在。如何解决此问题,以便我可以使用相同的表单来检索和更新数据?

protected void Page_Load(object sender, EventArgs e) 
    { 
     lblMsg.Visible = false; 

     string bookingid = Request.QueryString["bookingid"]; 
     Booking b = BookingAccess.GetBooking(Int32.Parse(bookingid)); 
     if (b != null) 
     { 
      var start_date_formatted = ((DateTime)b.StartDate).ToString("dd-MM-yyyy"); 
      var end_date_formatted = ((DateTime)b.EndDate).ToString("dd-MM-yyyy"); 

      pet_name.Text = b.PetName; 
      species.Text = b.Species; 
      start_date.Text = start_date_formatted; 
      end_date.Text = end_date_formatted; 
     } 


    } 

    protected void btnEditBooking_Click(object sender, EventArgs e) 
    { 
     string bookingid_raw = Request.QueryString["bookingid"]; 
     int bookingid = int.Parse(bookingid_raw); 

     var start_date_formatted = DateTime.ParseExact(start_date.Text, "dd-MM-yyyy", null); 
     var end_date_formatted = DateTime.ParseExact(end_date.Text, "dd-MM-yyyy", null); 

     string pet_name = "a"; 
     string species = "b"; 
     lblMsg.Visible = true; 
     string msg = BookingAccess.UpdateBooking(bookingid, pet_name, species, start_date_formatted, end_date_formatted); 
     if (msg == null) 
      lblMsg.Text = "Updated booking details successfully!"; 
     else 
      lblMsg.Text = "Error -> " + msg; 
    } 
+2

请添加您的代码。并确保你的邮件没有填充字段。 – MikeSmithDev

回答

1

如果你有在Page_Load功能将数据装载,确保您的IsPostBack检查,以确保当用户提交页面不重新设置数据。

private void Page_Load() 
{ 
    if (!IsPostBack) 
    { 
     // Load the data from database 
    } else { 
     // Validate the data and save to database 
    } 
}