Step 12: Registering Business Objects on the Order Edit Form
In this step you register the Order, OrderDetail, Shipper, and Employee business objects with the Customer Orders form. These objects will be used to retrieve and manipulate data on this form.
- In the Solution Explorer, right-click the OrderEdit.aspx file and select View Code from the shortcut menu.
- If you added a reference to the Order System Business Objects EF project, at the top of the source code file, add a reference to the following namespace:
using Acme.OrderSystem.BusinessEF;If you added a reference to the Order System Business Objects EF Code First project, add a reference to the following namespace:
using Acme.OrderSystem.BusinessCF; - At the top of the class definition, add the following protected properties that will be used to store references to the business objects and the current Order ID.
public partial class OrderEdit : mmBusinessWebPage { protected Order Order; protected OrderDetail OrderDetail; protected Shipper Shipper; protected Employee Employee; protected int OrderID;
- In the Web Form's Page_Load method, add the following code that instantiates the business objects and registers them with the form (you can get rid of the comment automatically placed in the method by Visual Studio):
protected void Page_Load(object sender, EventArgs e) { // Instantiate the Order, OrderDetail, Employee and Shipper objects this.Order = (Order)this.RegisterBizObj(new Order()); this.OrderDetail = (OrderDetail)this.RegisterBizObj(new OrderDetail()); this.Order.RegisterChildBizObj(this.OrderDetail); this.Shipper = (Shipper)this.RegisterBizObj(new Shipper()); this.Employee = (Employee)this.RegisterBizObj(new Employee()); // Retrieve the Order ID passed by the Customer Orders page // from the query string variable string orderIDString = Request.QueryString["orderID"]; if (!string.IsNullOrEmpty(orderIDString)) { this.OrderID = int.Parse(orderIDString); } if (!this.IsPostBack) { // Retrieve the specified order and detail this.Order.GetOrderByOrderID(this.OrderID); this.StoreEntityInSession(this.Order.Entity, "CurrentOrder"); this.OrderDetail.GetOrderDetail(this.OrderID); this.StoreEntityListInSession(this.OrderDetail.EntityList, "CurrentOrderDetail"); // Retrieve the Shippers and Employees this.Shipper.GetAllEntities(); this.Employee.GetAllEntities(); } }
Here's an explanation of this code:
- The first part of this code instantiates and registers the business objects with the Web Form.
- The order ID passed by the Customer Orders form (when a user clicks an Order ID hyperlink) is retrieved from the query string variable.
- The order is retrieved by passing the order ID to the Order object's GetOrderByOrderID method. The Order entity list is saved in an ASP.NET session variable named CurrentOrder so it can be retrieved when the user saves changes to the order.
- The order detail is retrieved by passing the order ID to the OrderDetail object's GetOrderDetail method. The OrderDetail entity list is saved in an ASP.NET session variable named CurrentOrderDetail so it can be retrieved when the user saves changes to the order detail.
Note: Although we are using the Session object in this example to save user state, you can also use the ASP.NET Cache object instead when you have a user ID that can be used to uniquely identify each user. See the MM .NET Help topic Persisting User State for details. - The last few lines of code retrieve all Shippers and Employees. Since the user can't change this information on this Web page, we don't need to persist the Shipper and Employee entity lists.
- Next, go to the Solution Explorer and double-click OrderEdit.aspx which opens the Web Form in design mode (if the form is already open in design mode, you can simply click the OrderEdit.aspx tab instead).
- At run time, when the user clicks the Cancel button, we need to redirect to the Customer Orders page. To add code that makes this happen, double-click the Cancel button. This displays the OrderEdit.aspx.cs source code file, adding a new btnCancel_Click event handler method. Add the following code to this method:
/// <summary> /// Redirects the user to the Customer Orders page /// </summary> protected void btnCancel_Click(object sender, EventArgs e) { Response.Redirect("CustomerOrders.aspx"); }
- At run time, when the user clicks the Save button, we need to save any changes to the data and redirect to the Customer Orders page. To add code that makes this happen, go back to the Order Edit form in design mode and double-click the Save button. This adds a new btnSave_Click event handler to the code-behind file. Add the following code to this method:
/// <summary> /// Save the Order header data /// </summary> protected void btnSave_Click(object sender, EventArgs e) { OrderEntity orderEntity = this.GetEntityFromSession<OrderEntity>("CurrentOrder"); if (this.Save<OrderEntity>(this.Order, orderEntity) == mmSaveDataResult.RulesPassed) { Response.Redirect("CustomerOrders.aspx"); } }
This code retrieves the previously saved Order entity from the CurrentOrder session variable and passes it back to the form's Save method along with a reference to the Order business object. This form-level method calls the business object's SaveEntity method and checks the result afterwards. If any errors occurred, these are displayed. Otherwise, the user is redirected back to the Customer Orders page.
What's happening behind the scenes when this code is executed? When the SaveEntity method is executed, the Order business object raises a Saving event. All Web controls that are bound to the Order business object respond to the event by copying their values back into the entity. Afterwards, the Order business object saves the entity.
Now that you have registered the business objects with the Web Form, and added some of the updating logic, it's time to bind the Web controls to their respective business objects.
See also:
Step 13: Binding the Order Edit Form Controls
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/25/18
Comment or report problem with topic
