更新表单保留原始值



我有一个表单,里面填充了从数据库中检索的值。该表单有一个提交按钮,单击该按钮后应更新数据库中的相应字段。

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

我的猜测是,一旦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;
    }

如果在Page_Load函数中加载了数据,请确保检查IsPostBack以确保在用户提交页面时不会重置数据。

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

相关内容

最新更新