How to: Add a User Control with a Presenter

You can use the View-Presenter pattern to separate the responsibilities for the visual display and the event handling behavior into different classes. Separation of these concerns leads to simplified classes, allows reuse of presentation logic, and helps you test the presentation logic without a visual user interface.
This topic describes how to use the Add User Control (with presenter) recipe to generate a user control with presenter.


The Add User Control (with presenter) recipe assumes that you have an existing Web client solution. For information about how to create a Web client solution, see How to: Create a Web Client Solution.
The recipe applies to a Web site root node or to a folder in a Web site that contains a Web.config file (typically, this is a Web folder for a business module).
The recipe uses the module name as a prefix for the generated user control class. This means you can only use the recipe to add user controls to modules that have names that are valid identifiers (for example, the module name does not include a period, such as Reports.Module). If you use the recipe to add a user control to a module with a name that is not a valid identifier, the generated code will not compile.

The Add Business Module recipe requires you to use a valid identifier for the module name. If you rename a business module after you create it, select a name that is a valid identifier.


The following procedure describes how to use the recipe to create a user control with presenter.

To use the Add User Control (with presenter) recipe to create a user control with presenter
  • In Solution Explorer, right-click a Web site or a folder in a Web site, point to Web* Client Factory, and then click Add User Control (with presenter) (C#)* or Add User Control (with presenter) (Visual Basic), as shown in Figure 1.
Figure 1
The Add User Control (with presenter) recipe menu.

If you are running the recipe in a Web Application project, you will see only the recipe menu items corresponding to the project’s language.

The recipe starts the Add User Control (with presenter) wizard, as illustrated in Figure 2.
Figure 2
The Add User Control (with presenter) recipe wizard.
  • In the User control name box, type the name for the user control.
  • Select the module. The recipe will create the presenter class and user control interface in this project.
  • (Optional) If you want to see a summary of the recipe actions and suggested next steps after the recipe completes, select the Show documentation after recipe completes check box.
  • Click Finish. The recipe generates the user control with presenter.


You will have the following elements in your solution, as shown in Figure 3:
  • A user control interface definition in the Views folder of the module project
  • A presenter class in the Views folder of the module project
  • A user control implementation in the module's Web site folder
Figure 3
Solution with the user control View1 in the business module Module1.

If you have a test project for the business module that contains the new user control, the recipe will create a test fixture class for the presenter in the Views folder of the test project.

Next Steps

The following are typical tasks that you perform after you create a user control with presenter:
  • Design the UI of your user control. Define styles and the layout of the user control.
  • Implement your user control. Add controls to the user control and forward user events to the presenter.
  • Define the user control interface. The user control should expose its state through its public interface. Typically, user control interfaces have properties such as Customers or SelectedAccount.
  • Create unit tests and implement the presenter. Handle user events and update the user control state. Interact with the module controller to execute business and navigation logic.
  • Consume the user control. Add the user control to your pages.

Last edited Nov 20, 2007 at 2:25 PM by ejadib, version 3


No comments yet.