一部のフォーム要素を無効から有効にしてから編集できるようにする編集ボタンがあります。updateButton_Click() 内から Textbox.Text 値を参照しようとすると、なんらかの理由を除いて正しく機能しています。ユーザーが編集した値ではなく、古い値が残っています。何か案は?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
public partial class UserControls_OrderDetailsAdmin : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Page_PreRender(sender, e);
}
// edit mode by default is false
private bool editMode = false;
// set up the form
protected void Page_PreRender(object sender, EventArgs e)
{
// check if we must display order details
if (Session["AdminOrderID"] != null)
{
//fill constituent controls with data
PopulateControls();
// set edit mode
SetEditMode(editMode);
}
else
// Hide
this.Visible = false;
}
// populate the form with data
private void PopulateControls()
{
if (Session["AdminOrderID"] != null)
{
// obtain order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// obtain order info
OrderInfo orderInfo = OrdersAccess.GetInfo(orderId);
// populate labels and text boxes with order info
orderIdLabel.Text = "Displaying Order #" + orderId;
totalAmountLabel.Text = String.Format("{0:c}", orderInfo.GrandTotal);
dateCreatedTextBox.Text = orderInfo.DateCreated;
dateShippedTextBox.Text = orderInfo.DateShipped;
statusLabel.Text = orderInfo.Status.ToString(); ;
statusLabel.Enabled = false;
completedCheck.Checked = orderInfo.Completed;
canceledCheck.Checked = orderInfo.Canceled;
commentsTextBox.Text = orderInfo.Comments;
// by default the Edit button is enabled, and the
// Update and Cancel buttons are disabled
editButton.Enabled = true;
updateButton.Enabled = false;
cancelButton.Enabled = false;
// Decide which one of the other three buttons
// should be enabled and which should be disabled
if (canceledCheck.Checked || completedCheck.Checked)
{
// if the order was canceled or completed ...
markVerifiedButton.Enabled = false;
markCompletedButton.Enabled = false;
markCanceledButton.Enabled = false;
}
else if (verifiedCheck.Checked)
{
// if the order was not canceled but is verified ...
markVerifiedButton.Enabled = false;
markCompletedButton.Enabled = true;
markCanceledButton.Enabled = true;
}
else
{
// if the order was not canceled and is not verified ...
markVerifiedButton.Enabled = true;
markCompletedButton.Enabled = false;
markCanceledButton.Enabled = true;
}
// fill the data grid with order details
grid.DataSource = OrdersAccess.GetDetails(orderId);
grid.DataBind();
}
}
// enable or disable edit mode
private void SetEditMode(bool enable)
{
dateCreatedTextBox.Enabled = false;
dateShippedTextBox.Enabled = enable;
verifiedCheck.Enabled = enable;
completedCheck.Enabled = enable;
canceledCheck.Enabled = enable;
commentsTextBox.Enabled = enable;
customerNameTextBox.Enabled = enable;
}
protected void editButton_Click(object sender, EventArgs e)
{
editMode = true;
}
protected void updateButton_Click(object sender, EventArgs e)
{
//SetEditMode(false);
//System.Threading.Thread.SpinWait(500);
// Store the new order details in an OrderInfo object
OrderInfo orderInfo = new OrderInfo();
string orderId = Session["AdminOrderID"].ToString();
orderInfo.OrderID = Int32.Parse(orderId);
orderInfo.DateCreated = dateCreatedTextBox.Text;
orderInfo.DateShipped = dateShippedTextBox.Text;
orderInfo.Verified = verifiedCheck.Checked;
orderInfo.Completed = completedCheck.Checked;
orderInfo.Canceled = canceledCheck.Checked;
orderInfo.Comments = commentsTextBox.Text;
orderInfo.CustomerName = customerNameTextBox.Text;
// try to update the order
try
{
// Update the order
OrdersAccess.Update(orderInfo);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
}
// Exit edit mode
// SetEditMode(false);
// Update the form
SetEditMode(false);
PopulateControls();
}
protected void cancelButton_Click(object sender, EventArgs e)
{
// don't need to do anything, editMode will be set to false by default
}
protected void markVerifiedButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark order as verified
OrdersAccess.MarkVerified(orderId);
// update the form
PopulateControls();
}
protected void markCompletedButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark the order as completed
OrdersAccess.MarkCompleted(orderId);
// update the form
PopulateControls();
}
protected void markCanceledButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark the order as canceled
OrdersAccess.MarkCanceled(orderId);
// update the form
PopulateControls();
}
}