Step 12 - Registering Business Objects on the Order Edit Form

In this step you register the Orders, OrderDetail, Shipper, and Employee business objects with the Customer Orders form. These objects will be used to retrieve and manipulate data on this form.

  1. In the Solution Explorer, right-click the OrderEdit.aspx file and select View Code from the shortcut menu.

  2. 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:

    Imports 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:

    Imports Acme.OrderSystem.BusinessCF
    

  3. 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 Class OrderEdit
    	Inherits mmBusinessWebPage
     
    	Protected Order As Order
    	Protected OrderDetail As OrderDetail
    	Protected Shipper As Shipper
    	Protected Employee As Employee
    	Protected OrderID As Integer

  4. In the Web Form's Page_Load method, add the following code that instantiates the business objects and registers them with the form:

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
    	' Instantiate the Order, OrderDetail, Employee and Shipper objects
    	Me.Order = CType(Me.RegisterBizObj(New Order), Order)
    	Me.OrderDetail = CType(Me.RegisterBizObj(New OrderDetail), OrderDetail)
    	Me.Order.RegisterChildBizObj(Me.OrderDetail)
    	Me.Shipper = CType(Me.RegisterBizObj(New Shipper), Shipper)
    	Me.Employee = CType(Me.RegisterBizObj(New Employee), Employee)
     
    	' Retrieve the Order ID passed by the Customer Orders page 
    	' from the query string variable
    	Dim orderIDString As String = Request.QueryString("orderID")
    	If Not String.IsNullOrEmpty(orderIDString) Then
    		Me.OrderID = Int32.Parse(orderIDString)
    	End If
     
    	If Not Me.IsPostBack Then
    		' Retrieve the specified order and detail
    		Me.Order.GetOrderByOrderID(Me.OrderID)
    		Me.StoreEntityInSession(Me.Order.Entity, "CurrentOrder")
    		Me.OrderDetail.GetOrderDetail(Me.OrderID)
    		Me.StoreEntityListInSession(Me.OrderDetail.EntityList, "CurrentOrderDetail")
     
    		' Retrieve the Shippers and Employees
    		Me.Shipper.GetAllEntities()
    		Me.Employee.GetAllEntities()
    	End If
    End Sub

    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 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.

  5. 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).

  6. 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>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Protected Sub btnCancel_Click(sender As Object, e As EventArgsHandles btnCancel.Click
    	Response.Redirect("CustomerOrders.aspx")
    End Sub

  7. 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>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnSave_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnSave.Click
     
    	Dim orderEntity As OrderEntity = Me.GetEntityFromSession(Of OrderEntity)("CurrentOrder")
     
    	If Me.Save(Of OrderEntity)(Me.Order, orderEntity) = mmSaveDataResult.RulesPassed Then
    		Response.Redirect("CustomerOrders.aspx")
    	End If
     
    End Sub

    This code retrieves the previously saved Orders 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


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/25/18
Comment or report problem with topic