0

I have an edit button that enables some form elements go from disabled to enabled and then be edited. I have it working correctly except for some reason, in the button_click event when I try to reference the Textbox.Text value from inside updateButton_Click(), it still has the old value instead of the value that had been edited by the user. Any ideas?

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();
}

}

4

1 回答 1

0

try using isPostBack property of page in pageload event

protected void Page_Load(object sender, EventArgs e)
  {
    if(!Page.IsPostBack){
    Page_PreRender(sender, e);
  }
}
于 2013-10-22T04:01:46.223 回答