Click here to Skip to main content
Click here to Skip to main content
Go to top

Creating a DotNetNuke® Module - For Absolute Beginners!

, 21 Jan 2007
Rate this:
Please Sign up or sign in to vote.
How to create a DotNetNuke® module using Visual Studio Express - for DotNetNuke version 4.

Introduction

This article explains in detail the process of creating a DotNetNuke module.

Requirements

To use this tutorial, you need:

  1. Visual Studio Express (download here)
  2. SQL Server Express (download here)
  3. DotNetNuke Starter Kit 4.x (download here)

    Note: When you download it, it may change the file extension to ".zip". After downloading, change this to ".vsi", and then you can double-click on the file to install the templates.

Note: Do not attempt to create modules on a production installation of DotNetNuke; trust me, you will invariably screw up the installation and will have to restore the database or re-install.

Setup

  1. Install Visual Studio Express if you haven't already done so (download here).
  2. Install SQL Server Express if you haven't already done so (download here).
  3. Follow the directions here to install the DotNetNuke Starter Kit and to create a DotNetNuke website.

image002.jpg

Very important: Click once on the root directory in the Solution Explorer window. Doing so will ensure that the starter module code is created in the proper place.

image003.jpg

Select File from the toolbar, then New File:

image004.jpg

On the next menu, click once on DotNetNuke Module (under My Templates), select "Visual Basic" from the Language dropdown, type "GuestBook." in the Name box, and click Add.

image005.jpg

I interrupt this tutorial for an important warning:

Note: If the files DataProvider.vb, GuestBookController.vb, GuestBookInfo.vb, and SqlDataProvider.vb in the "ModuleName" directory are under "DesktopModules/App_Code" then they are in the wrong place.

Click on the "ModuleName" folder and drag it under the "App_Code" directory that is directly under the main root of the website. You will also have to move the "DesktopModules/ModuleName" folder and drag the "ModuleName" folder so it is under the "DesktopModules" folder that is directly under the main root of the website.

This is the correct way it should look:

image007.jpg

image008.jpg

Now back to the tutorial:

A very helpful help page comes up and instructs you to rename /App_Code/ModuleName to /App_Code/GuestBook, and rename /DesktopModules/ModuleName to /DesktopModules/GuestBook.

image009.jpg

Make the changes by right-clicking the folder name in the Solutions Explorer and selecting Rename from the menu. You have to do this twice. Once for the folder under "App_Code", and then for the folder under "DesktopModules".

image010.jpg

image011.jpg

Now, from the toolbar, select Build, then Build Web Site:

image017.jpg

The website should build without errors.

image018.jpg

In the Solution Explorer, right-click on "Default.aspx" and select Set As Start Page:

image019.jpg

From the toolbar, select Debug, then Start Without Debugging.

(You may wonder why I am not covering debugging. While creating this tutorial, I ran into permission problems on my own machine. They are easily fixed but I wanted to avoid any unneeded complexity. If you need help debugging, see this post.)

image020.jpg

image021.jpg

The website should now come up.

image022.gif

Click Login:

image023.jpg

Log in as "host". The password (if you haven't already changed it) is "dnnhost":

image024.jpg

Click on the HOST menu and select SQL

The form will display.

Switch back to Visual Studio and in the Solution Explorer. Double-click on "01.00.00.SqlDataProvider" in the "DestopModules/GuestBook" directory so that it shows up in the main window.

Click once on the page, then from the toolbar select EDIT then SELECT ALL (Ctrl +A)

Then EDIT and COPY (Ctrl +C).

Switch back to the DotNetNuke website and paste (Ctrl +V) the script you copied into the window, click the "Run as Script" box and click EXECUTE.

From the HOST menu select "Module Definitions"

Click the small down arrow in the upper left hand corner of the Module Definitions menu and select "Create New Module"

Select "GuestBook.dnn" in the Module Manifest drop-down and click Install

Under the Page Functions menu, click Add:

image031.jpg

In the Page Details menu:

  • Enter "Guest Book" for Page Name.
  • Enter "Guest Book" for Page Title.
  • Enter "Guest Book" for Description.
  • Click the View Page box next to All Users.

Then, click Update:

image032.jpg

From the Module drop-down, select "GuestBook":

image033.jpg

Then, click Add:

image034.jpg

If you get the "Object reference not set to an instance of an object" error, just click on the "Guest Book" link:

(However, if you are running DotNetNuke on a machine that is lower than a Pentium III with 512 KB RAM, then DotNetNuke will regularly throw this error).

image036.jpg

The module should now appear:

image037.jpg

The Guest Book Module

We will walk through the construction of the Guest Book module in these steps:

  • Data Access Layer (DAL) - We will create the tables, the stored procedures, and alter code in the "SqlDataProvider.vb" and "DataProvider.vb" files.
  • Business Logic Layer (BLL) - We will alter code in the "GuestBookController.vb" and "GuestBookInfo.vb" files.
  • Presentation Layer (UI) - We will alter code in the "ViewGuestBook.ascx", and in the other files in the "DesktopModules/GuestBook" directory.

image038.jpg

Data Access Layer (DAL)

To build the Data Access Layer, we will:

And that's it! It's actually not a lot of code to add to the "SqlDataProvider.vb" and "DataProvider.vb" files. Creating the tables and stored procedures is standard database programming that you are probably already used to.

image039.gif

From: "DotNetNuke Module Developers' Guide"

Copyright © 2003-2005 Perpetual Motion Interactive Systems, Inc. All Rights Reserved.

Connect To The Database

If you have created your DotNetNuke website using SQL Server 2005 Express, your database connection should already be set up for you. If you set it up using SQL Server 2000 or SQL Server 2005, then you may have to set it up.

To set up a connection to the SQL Server database, from the toolbar, select View, then Server Explorer.

image040.jpg

In the Server Explorer, right-click on Data Connections, and select "Add Connection":

image041.jpg

Fill in the connection information, and click OK.

image042.jpg

Delete the Sample Database Objects

You now need to delete the table and the stored procedures that the template created so that you can make new ones (that happen to use the same names) that the guestbook will use.

The connection will now show up in the Server Explorer. Click the plus next to the connection to expand it. Next, click the plus icon next to Tables to expand it.

Right click on the table "YourCompany_GuestBook" and select "Delete" and delete the table.

Note: At this point, the module will no longer work in your DotNetNuke web site. It will not work again until you have completed all the steps in this tutorial.

image043.jpg

Click the plus icon next to Store Procedures to expand it. One at a time, right-click on the following stored procedures and select "Delete" to delete them. Remember, you have to delete them one at a time.

  • YourCompany_AddGuestBook
  • YourCompany_DeleteGuestBook
  • YourCompany_GetGuestBook
  • YourCompany_GetGuestBooks
  • YourCompany_UpdateGuestBook

image044.jpg

Create The Table

Log into the DotNetNuke website as Host (if you are not already logged in as Host), and from the Host menu, select SQL.

image045.jpg

Paste the following script in the box:

CREATE TABLE [dbo].[YourCompany_GuestBook](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ModuleID] [int] NULL,
[Name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Email] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Message] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DateEntered] [datetime] NULL,
CONSTRAINT [PK_YourCompany_GuestBook] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Do not select the "Run as Script" box, and click Execute.

(Note: This is a SQL 2005 script. It will not work in SQL 2000. For SQL 2000, you will have to create the table manually as explained in the next two steps.)

image046.jpg

Optionally, you can create the table in the table designer in Visual Web Developer. In the Server Explorer, right-click on Tables and select Add New Table:

image047.jpg

Design the table in the table designer using the graphics below. Make the "ID" column an "identity" column and also set it as the primary key. Save it as "YourCompany_GuestBook".

image048.jpg

Create the Stored Procedures

Log into the DotNetNuke website as Host (if you are not already logged in as Host), and from the Host menu, select SQL.

image045.jpg

Paste the following script in the box:

CREATE PROCEDURE {databaseOwner}
   [{objectQualifier}YourCompany_GuestBook_Delete] 
(
@ID int
)
AS
DELETE FROM {objectQualifier}YourCompany_GuestBook
WHERE (ID = @ID)
RETURN
GO
CREATE PROCEDURE {databaseOwner}
   [{objectQualifier}YourCompany_GuestBook_GetAll] 
(
@ModuleID int 
)
AS
SELECT ID, ModuleID, Name, Email, Message, DateEntered
FROM {objectQualifier}YourCompany_GuestBook
WHERE (ModuleID = @ModuleID)
order by DateEntered DESC
RETURN
GO
CREATE PROCEDURE {databaseOwner}
   [{objectQualifier}YourCompany_GuestBook_Insert] 
(
@ModuleID int,
@Name nvarchar(50),
@Email nvarchar(50),
@Message nvarchar(250)
)
AS
INSERT INTO {objectQualifier}YourCompany_GuestBook
(ModuleID, Name, Email, Message, DateEntered)
VALUES (@ModuleID,@Name,@Email,@Message,getdate())
RETURN
GO
CREATE PROCEDURE {databaseOwner}
   [{objectQualifier}YourCompany_GuestBook_Update] 
(
@ID int,
@Name nvarchar(50),
@Email nvarchar(50), 
@Message nvarchar(250),
@DateEntered datetime
)
AS
UPDATE {objectQualifier}YourCompany_GuestBook
SET Name = @Name, Email = @Email, 
    Message = @Message, DateEntered = @DateEntered
WHERE (ID = @ID)
RETURN
GO

Select the Run as Script box, and click Execute:

image049.jpg

Verify that the stored procedures have been created. Switch back to Visual Studio, and select View from the toolbar and Server Explorer:

image050.jpg

On the "Server Explorer" window, right-click on Stored Procedures and select Refresh:

image051.jpg

Scroll down the list of stored procedures and verify that these stored procedures have been created:

  • YourCompany_GuestBook_Delete
  • YourCompany_GuestBook_GetAll
  • YourCompany_GuestBook_Insert
  • YourCompany_GuestBook_Update

image052.jpg

Alter the "SqlDataProvider.vb" file. In Visual Studio, select View from the toolbar and "Solution Explorer":

image053.jpg

In the "Solution Explorer" window, expand the "GuestBook" directory under the "App_code" folder and double-click on the "SqlDataprovider.vb" file:

image054.jpg

Replace all the code with:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Framework.Providers

Namespace YourCompany.Modules.GuestBook

Public Class SqlDataProvider Inherits DataProvider

    Private Const ProviderType As String = "data"
    Private Const ModuleQualifier As String = ""
    Private _providerConfiguration As _
      ProviderConfiguration = _
      ProviderConfiguration.GetProviderConfiguration(ProviderType)
    Private _connectionString As String
    Private _providerPath As String
    Private _objectQualifier As String
    Private _databaseOwner As String

    ' <summary>
    ' Constructs new SqlDataProvider instance
    ' </summary>
    Public Sub New()
        MyBase.New()
        'Read the configuration specific information for this provider
        Dim objProvider As Provider = CType(_providerConfiguration._
            Providers(_providerConfiguration.DefaultProvider), Provider)

        'Read the attributes for this provider
        If ((objProvider.Attributes("connectionStringName") <> "") _
                AndAlso (System.Configuration.ConfigurationManager._
                AppSettings(objProvider.Attributes(_
                "connectionStringName")) <> "")) Then
            connectionString = System.Configuration._
                ConfigurationManager.AppSettings(_
                objProvider.Attributes("connectionStringName"))
        Else
            _connectionString = _
              objProvider.Attributes("connectionString")
        End If

        _providerPath = objProvider.Attributes("providerPath")
        _objectQualifier = objProvider.Attributes("objectQualifier")

        If ((_objectQualifier <> "") _
               AndAlso (_objectQualifier.EndsWith("_") _
               = False)) Then
            _objectQualifier = (_objectQualifier + "_")
        End If

        _databaseOwner = objProvider.Attributes("databaseOwner")

        If ((_databaseOwner <> "") _
                   AndAlso (_databaseOwner.EndsWith(_
                   ".") = False)) Then
        _databaseOwner = (_databaseOwner + ".")
        End If
    End Sub

    ' <summary>
    ' Gets and sets the connection string
    ' </summary>
    Public ReadOnly Property ConnectionString() As String
        Get
            Return _connectionString
        End Get
    End Property
    
    ' <summary>
    ' Gets and sets the Provider path
    ' </summary>
    Public ReadOnly Property ProviderPath() As String
        Get
            Return _providerPath
        End Get
    End Property

    ' <summary>
    ' Gets and sets the Object qualifier
    ' </summary>
    Public ReadOnly Property ObjectQualifier() As String
        Get
            Return _objectQualifier
        End Get
    End Property

    ' <summary>
    ' Gets and sets the database ownere
    ' </summary>
    Public ReadOnly Property DatabaseOwner() As String
        Get
            Return _databaseOwner
        End Get
    End Property

    ' --------------------------------------------------
    ' <summary>
    ' Gets the fully qualified name
    ' of the stored procedure
    ' </summary>
    ' <param name="name">The name
    ' of the stored procedure</param>
    ' <returns>The fully qualified name</returns>
    ' --------------------------------------------------
    Private Function GetFullyQualifiedName(ByVal _
                 name As String) As String
        Return (DatabaseOwner _
            + (ObjectQualifier _
            + (ModuleQualifier + name)))
    End Function
    
    ' --------------------------------------------------
    ' <summary>
    ' Gets the value for the field or DbNull
    ' if field has "null" value
    ' </summary>
    ' <param name="Field">The field to evaluate</param>
    ' <returns></returns>
    ' --------------------------------------------------
    Private Function GetNull(ByVal Field As Object) As Object
        Return Null.GetNull(Field, DBNull.Value)
    End Function

    Public Overrides Sub YourCompany_GuestBook_Insert(ByVal _
           ModuleId As Integer, ByVal Name As String, _
           ByVal Email As String, ByVal Message As String)
        SqlHelper.ExecuteNonQuery(ConnectionString, _
          GetFullyQualifiedName("YourCompany_GuestBook_Insert"), _
          ModuleId, Name, Email, Message)
    End Sub
    
    Public Overrides Sub _
           YourCompany_GuestBook_Delete(ByVal ID As Integer)
        SqlHelper.ExecuteNonQuery(ConnectionString, _
           GetFullyQualifiedName("YourCompany_GuestBook_Delete"), ID)
    End Sub
    
    Public Overrides Function YourCompany_GuestBook_GetAll _
           (ByVal ModuleId As Integer) As IDataReader
        Return CType(SqlHelper.ExecuteReader(ConnectionString, _
               GetFullyQualifiedName("YourCompany_GuestBook_GetAll"), _
               ModuleId), IDataReader)
    End Function
    
    Public Overrides Sub YourCompany_GuestBook_Update(ByVal ID _
           As Integer, ByVal Name As String, ByVal Email As _
           String, ByVal Message As String, _
           ByVal DateEntered As DateTime)
        SqlHelper.ExecuteNonQuery(ConnectionString, _
           GetFullyQualifiedName("YourCompany_GuestBook_Update"), _
           ID, Name, Email, Message, DateEntered)
    End Sub
End Class
End Namespace

You will notice that you will see errors such as:

"'YourCompany_GuestBook_Insert' cannot be declared 
  'Overrides' because it does not override a sub in a base class"

This is because the "DataProvider.vb" must indicate the methods that the "SqlDataProvider.vb" overrides. When we place those methods in the "DataProvider.vb" file, the errors will go away.

image055.jpg

Alter the "DataProvider.vb" file. In the Solution Explorer, in the "GuestBook" directory under the "App_code" folder, double-click on the "DataProvider.vb" file.

image056.jpg

Replace all the code with:

Imports System
Imports DotNetNuke
Imports System.Data
Imports DotNetNuke.Framework
Namespace YourCompany.Modules.GuestBook

Public MustInherit Class DataProvider
    ' singleton reference to the instantiated object
    Private Shared objProvider As DataProvider = Nothing

    ' constructor
    Shared Sub New()
    CreateProvider()
    End Sub

    ' dynamically create provider
    Private Shared Sub CreateProvider()
    objProvider = CType(Reflection.CreateObject("data", _
                  "YourCompany.Modules.GuestBook", _
                  ""), DataProvider)
    End Sub

    ' return the provider
    Public Shared Function Instance() As DataProvider
        Return objProvider
    End Function

    Public MustOverride Sub YourCompany_GuestBook_Insert(ByVal _
           ModuleId As Integer, ByVal Name As String, _
           ByVal Email As String, ByVal Message As String)
    Public MustOverride Function _
           YourCompany_GuestBook_GetAll(ByVal ModuleId _
           As Integer) As IDataReader
    Public MustOverride Sub YourCompany_GuestBook_Update(ByVal _
           ID As Integer, ByVal Name As String, _
           ByVal Email As String, ByVal Message _
           As String, ByVal DateEntered As DateTime)
    Public MustOverride Sub _
        YourCompany_GuestBook_Delete(ByVal ID As Integer)
End Class
End Namespace

Switch back to the "SqlDataprovider.vb" file and notice all the error messages are gone. Now would be a good time to save (Ctrl + Shift + S).

image057.jpg

Review

  • Data Access Layer (DAL) (Done)
  • Business Logic Layer (BLL)
  • Presentation Layer (UI)

We are done with the Data Access Layer. We will now program the Business Logic Layer. The Business Logic Layer will get done fast because it is only two files: a simple class file to hold our data, and a "controller" file that will fill the class file with data as well as handle inserting and deleting data.

The Business Logic Layer (BLL)

To build the Business Logic Layer, we will:

  • Alter "GuestBookInfo.vb"
  • Alter "GuestBookController.vb"

And that's it! This is the step that is usually the hardest for beginners to understand but is really not that complicated. However, ASP.NET 2.0 does provide a major reduction of code over the ASP.NET 1.1 version.

Alter the "GuestBookInfo.vb" file

In Visual Studio, select View from the toolbar and the "Solution Explorer":

image053.jpg

In the Solution Explorer window, expand the "GuestBook" directory under the "App_code" folder, and double-click on the "GuestBookInfo.vb" file:

image058.jpg

Replace every single line of code in the file with this code:

Imports System
Imports System.Configuration
Imports System.Data
Namespace YourCompany.Modules.GuestBook

Public Class GuestBookInfo
    Private _ModuleId As Integer
    Private _ID As Integer
    Private _Name As String
    Private _Email As String
    Private _Message As String
    Private _DateEntered As DateTime

    ' initialization
    Public Sub New()
      MyBase.New()
    End Sub

    ' <summary>
    ' Gets and sets the Module Id
    ' </summary>
    Public Property ModuleId() As Integer
     Get
      Return _ModuleId
     End Get
    Set(ByVal value As Integer)
      _ModuleId = value
     End Set
    End Property
    
    ' <summary>
    ' Gets and sets the Item ID
    ' </summary>
    Public Property ID() As Integer
     Get
      Return _ID
     End Get
     Set(ByVal value As Integer)
      _ID = value
     End Set
    End Property

    ' <summary>
    ' gets and sets the Name
    ' </summary>
    Public Property Name() As String
     Get
      Return _Name
     End Get
     Set(ByVal value As String)
      _Name = value
     End Set
    End Property

    ' <summary>
    ' Gets and sets the Email
    ' </summary>
    Public Property Email() As String
     Get
      Return _Email
     End Get
     Set(ByVal value As String)
      _Email = value
     End Set
    End Property

    ' <summary>
    ' Gets and sets the Message
    ' </summary>
    Public Property Message() As String
     Get
      Return _Message
     End Get
     Set(ByVal value As String)
      _Message = value
     End Set
    End Property

    ' <summary>
    ' Gets and sets the DateEntered
    ' </summary>
    Public Property DateEntered() As DateTime
     Get
      Return _DateEntered
     End Get
     Set(ByVal value As DateTime)
      _DateEntered = value
     End Set
    End Property
End Class
End Namespace

image059.jpg

Alter the "GuestBookController.vb" file

In Visual Studio, select View from the toolbar and the "Solution Explorer".

In the Solution Explorer window, expand the "GuestBook" directory under the "App_code" folder, and double-click on the "GuestBookController.vb" file:

image060.jpg

Replace every single line of code in the file with this code:

Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.ComponentModel
Imports System.Data
Imports System.Xml
Imports System.Web
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Search

Namespace YourCompany.Modules.GuestBook
 Public Class GuestBookController

  <DataObjectMethod(DataObjectMethodType.Insert)> _
  Public Shared Sub GuestBook_Insert(_
         ByVal objTest As GuestBookInfo)
    DataProvider.Instance.YourCompany_GuestBook_Insert(_
      objTest.ModuleId, objTest.Name, _
      objTest.Email, objTest.Message)
  End Sub

  <DataObjectMethod(DataObjectMethodType.Delete)> _
  Public Shared Sub GuestBook_Delete(ByVal _
                objTest As GuestBookInfo)
    DataProvider.Instance._
      YourCompany_GuestBook_Delete(objTest.ID)
  End Sub

  <DataObjectMethod(DataObjectMethodType.Select)> _
  Public Shared Function GuestBook_GetAll(_
         ByVal ModuleId As Integer) _
         As List(Of GuestBookInfo)
    Return CBO.FillCollection(Of GuestBookInfo)_
           (DataProvider.Instance()._
           YourCompany_GuestBook_GetAll(ModuleId))
  End Function

  <DataObjectMethod(DataObjectMethodType.Update)> _
  Public Shared Sub GuestBook_Update(ByVal _
                    objTest As GuestBookInfo)
    DataProvider.Instance._
        YourCompany_GuestBook_Update(objTest.ID, _
        objTest.Name, objTest.Email, _
        objTest.Message, objTest.DateEntered)
  End Sub

 End Class
End Namespace

image061.jpg

Review

  • Data Access Layer (DAL) (Done)
  • Business Logic Layer (BLL) (Done)
  • Presentation Layer (UI)

We are done with the Business Logic Layer.

What did we just do?

The "GuestBookInfo.vb" was created. This is just a simple class file. It will hold the data.

image062.jpg

The "GuestBookController.vb" was created. This class has four methods:

  • GuestBook_Insert
    • Inserts items into the database. You pass a "GuestBookInfo" object to this method. The method then opens up the object and passes the individual parameters (module ID, name, email, message) to the "YourCompany_GuestBook_Insert" method in the DataProvider.vb file.
  • GuestBook_Delete
    • Deletes items from the database. You pass a "GuestBookInfo" object to this method. The method then opens up the object and passes the individual parameter (ID) to the "YourCompany_GuestBook_Delete" method in the DataProvider.vb file.
  • GuestBook_GetAll
    • Gets a recordset from the database. You pass a "ModuleId" parameter to this method. The method calls the "YourCompany_GuestBook_GetAll" method in the DataProvider.vb file and returns a "GuestBookInfo" object filled with data.
  • GuestBook_Update
    • Updates the database. You pass a "GuestBookInfo" object to this method. The method then opens up the object and passes the individual parameters (ID, module ID, name, email, message, date entered) to the "YourCompany_GuestBook_Update" method in the DataProvider.vb file.

image063.jpg

A Lot Of Stuff Was Deleted

You may notice that a lot of code was deleted out of the "GuestBookController.vb" file. Some of this were the optional interfaces that handle exporting data and searching. These are interfaces that you will want to read about and implement in your modules. However, they are not required, and they are left out for simplicity.

A lot of code was eliminated by using code that is exclusive to ASP.NET 2.0 and incompatible with ASP.NET 1.1. ASP.NET 2.0 really saves you a lot of work and requires less code.

The Presentation Layer (UI)

From this point on, the files that need to be altered reside in the ".../DesktopModules/GuestBook" directory.

To build the Presentation Layer, we will:

  • Alter the "localization" (.resx) files.
  • Alter "controls" and their "code-behind" files:
    • EditGuestBook.ascx
    • Settings.ascx
    • ViewGuestBook.ascx

And that's it! The module will then be complete.

image064.jpg

Alter the Localization Files

Localization allows you to create labels that can have their text changed (for example, to change text from English to Spanish) by simply changing a "resource" file that has a ".resx" extension.

Double click on "EditGuestBook.ascx.resx" to open it.

image065.jpg

Change the content so it matches the picture below. The order does not matter. Save and close the file when done.

image066.jpg

Double click on "Settings.ascx.resx" to open it.

image067.jpg

Change the content so it matches the picture below. The order does not matter. Save and close the file when done.

image068.jpg

Double click on "ViewGuestBook.ascx.resx" to open it.

image069.jpg

Change the content so it matches the picture below. The order does not matter. Save and close the file when done.

image070.jpg

Alter the Controls

The module consists of three controls (and their "code-behind" files):

  • EditGuestBook.ascx
    • EditGuestBook.ascx.vb
  • Settings.ascx
    • Settings.ascx.vb
  • ViewGuestBook.ascx
    • ViewGuestBook.ascx.vb

image071.jpg

(A screen shot from the module definition that was created in the DotNetNuke site in the early step of the tutorial.)

Right-click on "EditGuestBook.ascx" and select "View Markup":

image072.jpg

Replace all the code with this code (save and close the file when done):

<%@ Control language="VB" 
     Inherits="YourCompany.Modules.GuestBook.EditGuestBook" 
     CodeFile="EditGuestBook.ascx.vb" 
     AutoEventWireup="true"%>
<%@ Register TagPrefix="dnn" TagName="Label" 
     Src="~/controls/LabelControl.ascx" %>
<dnn:label id="lblContent" runat="server" 
     controlname="lblContent" suffix=":">
</dnn:label>

<asp:ObjectDataSource ID="ObjectDataSource_Tasks" 
     runat="server" 
     DataObjectTypeName="YourCompany.Modules.
                         GuestBook.GuestBookInfo"
DeleteMethod="GuestBook_Delete" 
    InsertMethod="GuestBook_Insert" 
    OldValuesParameterFormatString="original_{0}"
    OnInit="Page_Load" SelectMethod="GuestBook_GetAll" 
    TypeName="YourCompany.Modules.GuestBook.GuestBookController"
    UpdateMethod="GuestBook_Update">
<SelectParameters>
  <asp:Parameter DefaultValue="00" Name="ModuleId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AllowPaging="True" AutoGenerateColumns="False" 
    DataSourceID="ObjectDataSource_Tasks" 
    DataKeyNames="ID">
<Columns>

<asp:CommandField ShowDeleteButton="True" 
        ShowEditButton="True" />
<asp:BoundField DataField="ID" 
        HeaderText="ID" Visible="False" />
<asp:BoundField DataField="ModuleID" 
        HeaderText="ModuleID" Visible="False" />
<asp:BoundField DataField="Name" 
        HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Message" 
        HeaderText="Message" SortExpression="Message" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField ApplyFormatInEditMode="True" 
        DataField="DateEntered" DataFormatString="{0:d}"
HeaderText="Date" HtmlEncode="False" 
        SortExpression="DateEntered" />
</Columns>
</asp:GridView>

Right-click on "Settings.ascx" and select "View Markup":

image073.jpg

Replace all the code with this code (save and close the file when done):

<%@ Control Language="VB" AutoEventWireup="false" 
       CodeFile="Settings.ascx.vb" 
       Inherits="YourCompany.Modules.GuestBook.Settings" %>
<%@ Register TagPrefix="dnn" TagName="Label" 
       Src="~/controls/LabelControl.ascx" %>

<dnn:label id="lblshowform" runat="server" 
       controlname="txtshowform" suffix=":">
</dnn:label>

<br />

<asp:DropDownList ID="DropDownList1" runat="server" 
       AutoPostBack="True" 
       nSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem Selected="True">Yes</asp:ListItem>
  <asp:ListItem>No</asp:ListItem>
</asp:DropDownList>

Right-click on "ViewGuestBook.ascx" and select "View Markup":

image074.jpg

Replace all the code with this code (save and close the file when done):

<%@ Control Language="VB" 
     Inherits="YourCompany.Modules.GuestBook.ViewGuestBook" 
     CodeFile="ViewGuestBook.ascx.vb"
     AutoEventWireup="true" %>
<%@ Register TagPrefix="dnn" TagName="Label" 
     Src="~/controls/LabelControl.ascx" %>
<asp:ObjectDataSource ID="ObjectDataSource_Tasks" 
     runat="server" 
     DataObjectTypeName="YourCompany.Modules.
                         GuestBook.GuestBookInfo"
     DeleteMethod="GuestBook_Delete" 
     InsertMethod="GuestBook_Insert" 
     OldValuesParameterFormatString="original_{0}"
     SelectMethod="GuestBook_GetAll" 
     TypeName="YourCompany.Modules.
               GuestBook.GuestBookController"
     UpdateMethod="GuestBook_Update" 
     OnInit="Page_Load">
<SelectParameters>
  <asp:Parameter DefaultValue="00" 
          Name="ModuleId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server" 
       DataSourceID="ObjectDataSource_Tasks"
       AutoGenerateColumns="False" 
       AllowPaging="True" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="Name" 
       HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Message" 
       HeaderText="Message" 
       SortExpression="Message" />
<asp:BoundField ApplyFormatInEditMode="True" 
       DataField="DateEntered" DataFormatString="{0:d}"
HeaderText="Date" 
       SortExpression="DateEntered" 
       HtmlEncode="False" />
</Columns>
<EmptyDataTemplate>
There are no entries.
</EmptyDataTemplate>
</asp:GridView>
<br />
<center>
<dnn:Label ID="lblAddMessage" runat="server" 
       ControlName="lblAddMessage" Suffix=":">
</dnn:Label>
</center>
<br />
<asp:FormView ID="FormView1" runat="server" 
      DataSourceID="ObjectDataSource_Tasks" 
      DefaultMode="Insert" HorizontalAlign="Center">
<InsertItemTemplate>
<table cellpadding="2" cellspacing="5" 
          style="width: 50%" align="center">
<tr>
<td align="right" style="width: 4px">
<asp:Label ID="Label1" 
   runat="server" Text="Name"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="NameTextBox" runat="server" 
     Text='<%# Bind("Name") %>' 
     Width="264px"></asp:TextBox></td>
</tr>
<tr>
<td align="right" style="width: 4px; height: 23px">
<asp:Label ID="Label3" runat="server" 
         Text="Email"></asp:Label></td>
<td style="width: 100px; height: 23px">
<asp:TextBox ID="EmailTextBox" runat="server" 
        Text='<%# Bind("Email") %>' 
        Width="264px"></asp:TextBox></td>
</tr>
<tr>
<td align="right" style="width: 4px; height: 21px">
<asp:Label ID="Label2" runat="server" 
        Text="Message"></asp:Label></td>
<td style="width: 100px; height: 21px">
<asp:TextBox ID="MessageTextBox" 
        runat="server" EnableViewState="False" 
        MaxLength="250" Rows="2" 
        Text='<%# Bind("Message") %>' 
        TextMode="MultiLine" 
        Width="264px"></asp:TextBox></td>
</tr>
<tr>
<td align="right" colspan="2" style="height: 21px">
<asp:Button ID="InsertButton" runat="server" 
        Text="Submit" CommandName="Insert" /></td>
</tr>
</table>
<br />

</InsertItemTemplate>
</asp:FormView>

Right-click on "EditGuestBook.ascx" and select "View Code":

image075.jpg

Replace all the code with this code (save and close the file when done.):

Imports DotNetNuke
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports DotNetNuke.Entities.Modules

Namespace YourCompany.Modules.GuestBook
Partial Class EditGuestBook Inherits PortalModuleBase
    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs)
        Try
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Protected Sub SetModuleId(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls._
              ObjectDataSourceSelectingEventArgs) _
              Handles ObjectDataSource_Tasks.Selecting
        e.InputParameters("ModuleId") = ModuleId.ToString
    End Sub
End Class
End Namespace

Right-click on "Settings.ascx" and select "View Code":

image076.jpg

Replace all the code with this code (save and close the file when done):

Imports System
Imports System.Web.UI
Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Namespace YourCompany.Modules.GuestBook

Partial Class Settings Inherits ModuleSettingsBase
    Public Overrides Sub LoadSettings()
        Try
            If (Page.IsPostBack = False) Then
                If (Not (CType(TabModuleSettings(_
                       "showform"), _
                       String)) Is Nothing) Then
                    Me.DropDownList1.SelectedValue_
                       = CType(TabModuleSettings(_
                       "showform"), String)
                End If
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal_
           sender As Object, ByVal e As EventArgs)
        Dim objModules As ModuleController = _
                 New ModuleController

        If (Me.DropDownList1.SelectedValue = "Yes") Then
            objModules.UpdateTabModuleSetting(_
                 TabModuleId, "showform", "Yes")
        Else
            objModules.UpdateTabModuleSetting(_
               TabModuleId, "showform", "No")
        End If
    End Sub
End Class
End Namespace

Right-click on "ViewGuestBook.ascx" and select "View Code":

Replace all the code with this code (save and close the file when done):

image077.jpg

Imports DotNetNuke
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports DotNetNuke.Entities.Modules
Namespace YourCompany.Modules.GuestBook

Partial Class ViewGuestBook
  Inherits Entities.Modules.PortalModuleBase
  Implements Entities.Modules.IActionable

    Public ReadOnly Property ModuleActions() _
          As Entities.Modules.Actions._
          ModuleActionCollection Implements _
          Entities.Modules.IActionable.ModuleActions
        Get
            Dim Actions As New Entities.Modules_
                  .Actions.ModuleActionCollection
            Actions.Add(GetNextActionID, _
                  Localization.GetString(_
                  Entities.Modules.Actions._
                  ModuleActionType.EditContent, _
                  LocalResourceFile), _
                  Entities.Modules.Actions._
                  ModuleActionType.EditContent, _
                  "", "", EditUrl(), False, _
                  Security.SecurityAccessLevel.Edit,_
                  True, False)
            Return Actions
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender _
              As Object, ByVal e As System.EventArgs)
        Try
            Dim objModules As ModuleController_
                       = New ModuleController
            If Not Page.IsPostBack Then
                If (Not (CType(Settings("showform"), _
                         String)) Is Nothing) Then
                    If (CType(Settings("showform"), _
                              String) = "No") Then
                        ' Do not allow messages to be added
                        FormView1.Visible = False
                        lblAddMessage.Visible = False
                    End If
                End If
            Else
                Me.GridView1.DataBind()
            End If
        Catch ex As Exception
            Exceptions.ProcessModuleLoadException(Me, ex)
        End Try
    End Sub

    Protected Sub NewItem(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls._
              FormViewInsertEventArgs) _
              Handles FormView1.ItemInserting
        e.Values.Item("ID") = 0
        e.Values.Item("ModuleId") = ModuleId.ToString()
        e.Values.Item("DateEntered") = _
              DateTime.Now.ToShortDateString
    End Sub

    Protected Sub SetModuleID(ByVal sender As _
              Object, ByVal e As System.Web.UI._
              WebControls.ObjectDataSourceSelectingEventArgs) _
              Handles ObjectDataSource_Tasks.Selecting
    e.InputParameters("ModuleId") = ModuleId.ToString
    End Sub
End Class
End Namespace

Review

  • Data Access Layer (DAL) (Done)
  • Business Logic Layer (BLL) (Done)
  • Presentation Layer (UI) (Done)

Build The Site

From the toolbar, select Build, then "Build Web Site":

image078.jpg

The site should build with no errors:

image079.jpg

From the toolbar, select Debug, then Start Without Debugging:

image021.jpg

The website will come up. Click "Guest Book" on the menu bar:

image080.jpg

The module will now show:

image081.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

defwebserver
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
LightSwitchHelpWebsite.com

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMauliK.b20-Nov-12 1:41 
QuestionDOTNETNUKE PinmemberRozzan10-Sep-12 21:07 
AnswerRe: DOTNETNUKE Pinmemberdefwebserver11-Sep-12 3:11 
Questionplease send the above code for c# PinmemberAjay108918-Jun-12 21:17 
QuestionI get the error "The type or namespace name '[X]' could not be found" after adding the new module PinmemberDanielCarbonell31-Jan-12 0:45 
GeneralHelp me PinmemberPhamMinhLanh29-Mar-10 17:17 
GeneralRe: Help me Pinmemberdefwebserver29-Mar-10 17:22 
GeneralLocalize FormView control PinmemberBlumen16-May-09 22:43 
GeneralRe: Localize FormView control Pinmemberdefwebserver17-May-09 1:51 
GeneralMiGS PinmemberBlumen22-Nov-08 18:52 
General'Context' is not member of 'Isattis.Modules.BookBuy.ViewBookBuy' Pinmemberriyahiyan1-Mar-08 23:52 
GeneralRe: 'Context' is not member of 'Isattis.Modules.BookBuy.ViewBookBuy' PinmemberMichelleMcCabe22-Sep-08 7:51 
GeneralRe: Creating table Pinmemberdefwebserver22-Sep-07 6:54 
GeneralRe: Creating table Pinmemberprofessorpc22-Sep-07 8:13 
GeneralProperty 'ConnectionString' is 'ReadOnly' Pinmemberdenniswong28815-Sep-07 23:40 
GeneralRe: Property 'ConnectionString' is 'ReadOnly' Pinmemberdefwebserver22-Sep-07 6:55 
GeneralRe: Property 'ConnectionString' is 'ReadOnly' PinmemberJermond Love20-Jan-09 11:34 
QuestionHelp me .. Pinmemberjavarabbi26-Jun-07 2:19 
AnswerRe: Help me .. Pinmemberdefwebserver26-Jun-07 2:55 
QuestionWhy use the Starter Kit? Pinmemberxsoftdev1-Feb-07 22:46 
GeneralWay to go Michael! PinmemberPhebous23-Jan-07 7:26 
QuestionItem has already been added. Key in dictionary: 'ID' Key being added: 'ID' PinmembermousaSoutari198230-Nov-06 12:46 
AnswerRe: Item has already been added. Key in dictionary: 'ID' Key being added: 'ID' Pinmemberdefwebserver30-Nov-06 14:35 
GeneralDotNetNuke Starter Kit Install Error Pinmemberstapes23-Nov-06 10:46 
GeneralRe: DotNetNuke Starter Kit Install Error Pinmemberdefwebserver23-Nov-06 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 21 Jan 2007
Article Copyright 2006 by defwebserver
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid