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;
    }
4

1 回答 1

1

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

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Load the data from database
    } else {
        // Validate the data and save to database
    }
}
于 2012-11-23T02:19:09.043 回答