Step 15: Enhancing Web Form Navigation

In this step you will change the Customer Orders form so it automatically displays all orders for the current customer when navigating back to the page from the Order Edit form.

  1. If your Web application is currently running, shut it down.

  2. In the Solution Explorer, right-click CustomerOrders.aspx and select View Code from the shortcut menu.

  3. In the code-behind file, add the first line of code shown below to the btnGetOrders_Click method:

    /// <summary>
    /// Retrieve all orders for the specified customer
    /// </summary>
    /// <param name="customerID">Customer ID</param>
    protected void btnGetOrders_Click(object sender, EventArgs e)
    {
    	// Save the customer ID
    	Session["CustomerID"] = this.txtCustomerID.Text;
     
    	this.GetCustomerOrders(this.txtCustomerID.Text);
    }

    This code saves the current customer ID in a session variable.

  4. Next, add the following if statement to the bottom of the Page_Load method:

    protected void Page_Load(object sender, EventArgs e)
    {
    	// Instantiate the Order object and register it with the form
    	this.Order = (Order)this.RegisterBizObj(new Order());
     
    	// If a Customer ID is currently stored in a session variable
    	// and the form is not being posted back, use the ID to 
    	// retrieve orders for the specified customer
    	if (!IsPostBack)
    	{
    		string CustomerID = (string)Session["CustomerID"];
    		if (CustomerID != null)
    		{
    			this.txtCustomerID.Text = CustomerID;
    			this.GetCustomerOrders(CustomerID);
    		}
    	}
    }

    This code checks the value of IsPostBack. If it's false, it means the user is navigating to the form. When IsPostBack is true, it indicates the form is being posted back to the server (which happens when the user clicks the Get Orders button).

    If the form is not being posted back, this code checks if there is a customer ID stored in the CustomerID session variable. This is the session variable that gets set when the user clicks the Get Orders button. If the session variable exists, the customer ID is stored back in the Customer ID text box and is then used to retrieve all orders for the customer.

  5. Now you're ready to test the code. To run the Web application, click the Start button on the Visual Studio toolbar. This displays the Customer Order form with no orders retrieved yet:

    Behind the scenes, IsPostBack is false so our new code in the Page_Load method is executed. However, CustomerID is null, so the form doesn't retrieve orders.

  6. Enter ROMEY in the Customer ID text box, and then click the Get Orders button. The Web Form displays all orders for customer ROMEY:

    Behind the scenes, IsPostback is true, so the new code does not fire. The code in the btnGetOrders_Click method fires, saving ROMEY to the CustomerID session variable.

  7. Select order 10281 which navigates to the Order Edit form.

  8. In the Order Edit form, click the Cancel button. This navigates back to the Customer Orders form which should look like this:

    Behind the scenes, the Customer Orders form reinstantiates on the server, but this time IsPostBack is true, firing our new code in the Form_Load method. The ROMEY customer ID is retrieved from the CustomerID session variable and the orders are retrieved.

See also:
Step 16: Enhancing the Order Detail GridView


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