Learn More





DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.

Creating Testable Modules - The LinksView Control

This article is cross-posted from my personal blog.

In previous blog posts in this series on creating testable modules I have shown how the various layers of a DotNetNuke module can be built and tested independently.  In this blog I will create the LinksView UserControl and demonstrate in the browser that everything does indeed work as expected.

The LinksView UserControl

Lets first take a look at the code file for the UserControl.  As we would expect the UserControl has to implement the ILinksView interface.  As we are writing a DotNetNuke Module it must also implement the IModuleControl interface.  In DotNetNuke 5.0 the IModuleControl interface replaces the PortalModuleBase class as the contract which all module controls must implement.

Listing 1 – The LinskView UserControl’s Code File

   1:  public partial class LinksView : ModuleUserControlBase, ILinksView
   2:  {
   3:      LinksViewPresenter presenter;
   5:      public LinksView()
   6:      {
   7:          //Instantiate associated Presenter
   8:          presenter = new LinksViewPresenter(this, 
   9:                      new LinksRepository(new DataService()));
  10:      }
  12:      public int ModuleId
  13:      {
  14:          get { return this.ModuleContext.ModuleId; }
  15:      }
  17:      public List Links
  18:      {
  19:          set
  20:          {
  21:              grdLinks.DataSource = value;
  22:              grdLinks.DataBind();
  23:          }
  24:      }
  26:      protected override void OnLoad(EventArgs e)
  27:      {
  28:          base.OnLoad(e);
  29:          presenter.OnViewLoaded();
  30:      }
  31:  }


In this case as we are creating a UserControl we can inherit from the new ModuleUserControlBase.  The View has to create an instance of the LinksViewPresenter, which in turn requires an instance of LinksRepository and an instance of DataService, and this is all wired up in the Constructor. 

The ModuleId property, which the presenter uses, wraps the ModuleId property of the IModuleControl’s ModuleContext property, while the Links property wraps the DataSource property of a DataGrid control on the ascx page.  Finally, the view’s OnLoad method calls the presenter’s OnViewLoaded method to make everything work.  This is similar to how we wired up the concrete View control in our HelloWorldMVP application.

Listing 2 - The LinskView UserControl’s ASCX File

   1:  <asp:DataGrid ID="grdLinks" runat="server" AutoGenerateColumns="false" 
   2:                  GridLines="None" ShowHeader="false">
   3:      <Columns>
   4:          <asp:TemplateColumn>
   5:              <ItemTemplate>
   6:                  <asp:HyperLink 
   7:                      ID="lnkLink" runat="server" CssClass="CommandButton" 
   8:                      NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"LinkUrl") %>' 
   9:                      Text='<%# DataBinder.Eval(Container.DataItem,"LinkText") %>'
  10:                      ToolTip='<%# DataBinder.Eval(Container.DataItem,"Title")%>' />
  11:              ItemTemplate>
  12:          asp:TemplateColumn>
  13:      Columns>
  14:  asp:DataGrid>


Listing 2 shows the ascx file for the UserControl.  Here we just set up the binding to the various properties of our Link objects.

Registering the LinksMVP Module

We are now ready to Register the Links MVP module.  The process has changed a little in DotNetNuke 5.0. In Host/Module Definitions (or Host/Extensions) select the Create New action.  On the first page of the Create new Extension Wizard fill out the Package level properties (Figure 1) – Note:  if you launched the Wizard from the Module Definitions page (Rather than Extensions) you will not see the Select Extension Type drop-down.

Figure 1 – Creating the New Module Definition – Page 1

The next Wizard page will allow you to set the Module properties. (Figure 2).

Figure 2 – Creating the New Module Definition – Page 2

Finally, on the last Wizard page, if you intend this module for distribution, you can add some information about you or your company (Figure 3).

Figure 3 – Creating the New Module Definition – Page 3

After selecting Next the Module will be registered.  You will need to add the Module Definition and the Module Control reference.  This is done in much the same way as it was prior to 5.0.

Setting up the Database

The last step is to create the database objects we will need.  We will do this by executing the same SQL we used in our DataService tests.

Listing 3 – The TSQL required to create the Links Table and GetLinks Procedure

   1:  CREATE TABLE {databaseOwner}[{objectQualifier}LinksMVP_Links](
   2:      [LinkId] [int] IDENTITY(1,1) NOT NULL,
   3:      [ModuleId] [int] NULL,
   4:      [LinkText] [nvarchar](250) NULL,
   5:      [LinkURL] [nvarchar](1000) NULL,
   6:      [Title] [nvarchar](250) NULL
   7:      CONSTRAINT [PK_{objectQualifier}LinksMVP_Links] PRIMARY KEY CLUSTERED ([LinkId] ASC)
   8:  )
  10:  CREATE PROCEDURE {databaseOwner}[{objectQualifier}LinksMVP_GetLinks]
  11:            @ModuleId int
  12:  AS
  13:      SELECT *
  14:      FROM {databaseOwner}{objectQualifier}LinksMVP_Links
  15:                     WHERE ModuleID = @ModuleID
  16:  GO

Add the Module to a Page

Once the Module is registered and the Database obejcts have been created we can add an instance of our new module to a test page.  As there is no data we will not see anything, but we can use SQL Server Management Studio to add some links to the database.

Figure 4 – Adding Data to the Links Table


After adding links to the table, we can refresh the page and we should see a list of the links (Figure 5)

Figure 5 – The LinksMVP Module with two Links


There are currently no comments, be the first to post one.

Comment Form

Only registered users may post comments.


2sic Daniel Mettler (124)
Aderson Oliveira (15)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (21)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (203)
Chris Paterra (55)
Clinton Patterson (28)
Cuong Dang (21)
Daniel Bartholomew (2)
Dave Buckner (2)
David Poindexter (3)
David Rodriguez (2)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (74)
Geoff Barlow (6)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (269)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Peter Donker (52)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Tony Henrich (3)
Torsten Weggen (2)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (37)
Will Strohl (163)
William Severance (5)
Try Evoq
For Free
Start Free Trial
a Demo
See Evoq Live
Need More Information?