Creating Custom Modules - Creating Controls Manually in Visual Studio
(Page 3 of 7 )
When using a Class Library project as a starting point for your private assembly, you cannot add a Web User Control to your project by selecting Add | New Item from the project menu. Because of this we will have to add our controls manually.
| An optional way to create the user controls needed is to create a Web User Control inside the DotNetNuke project and then drag the control to your PA project to make modifications. |
Creating the View Control
The View control is what a non-administrator sees when you add the module to your portal. In other words, this is the public interface for your module.
Let's walk through the steps needed to create this control.
- Making sure that your private assembly project is highlighted, select Add New Item from the Project menu.
- Select Text File from the list of available templates and change the name to ShopList.ascx.
- Click Open to create the file.
- Click on the HTML tab and add the following directive to the top of the page:
<%@ Control language="vb" AutoEventWireup="false" Inherits="EganEnterprises.CoffeeShopListing.ShopList" CodeBehind="ShopList.ascx.vb"%>
Directives can be located anywhere within the file, but it is standard practice to place them at the beginning of the file. This directive sets the language to VB.NET and specifies the class and code-behind file that we will inherit from.
- Click the save icon on the toolbar to save the page.
- In the Solution Explorer right-click on the ShopList.ascx file and select View Code.
This will create a code-behind file for the Web User Control that we just created. The code-behind file follows the format of a normal Web User Control that inherits from System.Web.UserControl. This control, though based on Web.UserControl, will instead inherit from a class in DotNetNuke. Change the code-behind file to look like the code that follows. Here is the code-behind page in its entirety minus the Web Form Designer Generated Code:
Imports DotNetNuke
Imports DotNetNuke.Security.Roles
Namespace EganEnterprises.CoffeeShopListing
Public MustInherit Class ShopList
Inherits Entities.Modules.PortalModuleBase
Implements Entities.Modules.IActionable
Implements Entities.Modules.IPortable
Implements Entities.Modules.ISearchable
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Public ReadOnly Property ModuleActions() As _
DotNetNuke.Entities.Modules.Actions.ModuleActionCollection _
Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
Get
Dim Actions As New _
Entities.Modules.Actions.ModuleActionCollection
Actions.Add(GetNextActionID, _
Localization.GetString( _
Entities.Modules.Actions.ModuleActionType.AddContent, _
LocalResourceFile), _
Entities.Modules.Actions.ModuleActionType.AddContent, _
"", _
"", _
EditUrl(), _
False, _
Security.SecurityAccessLevel.Edit, _
True, _
False)
Return Actions
End Get
End Property
Public Function ExportModule(ByVal ModuleID As Integer) _
As String Implements _ DotNetNuke.Entities.Modules.IPortable.ExportModule
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Function
Public Sub ImportModule(ByVal ModuleID As Integer, _
ByVal Content As String, _
ByVal Version As String, _
ByVal UserID As Integer) _
Implements DotNetNuke.Entities.Modules.IPortable.ImportModule
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Sub
Public Function GetSearchItems( _
ByVal ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) _
As DotNetNuke.Services.Search.SearchItemInfoCollection _
Implements DotNetNuke.Entities.Modules.ISearchable.GetSearchItems
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Function
End Class
End Namespace
Let's break up the code listing above so that we can better understand what is happening in this section. The first thing that we do is add an Imports statement for DotNetNuke and DotNetNuke.Security.Roles so that we may access their methods without using the fully qualified names.
Imports DotNetNuke
Imports DotNetNuke.Security.Roles
Namespace EganEnterprises.CoffeeShopListing
Next, we add the namespace to the class and set it to inherit from Entities.Modules .PortalModuleBase. This is the base class for all module controls in DotNetNuke. Using the base class is what gives our controls consistency and implements the basic module behavior like the module menu and header. This class also gives us access to useful items such as User ID, Portal ID, and Module ID among others.
This section then finishes up by implementing three different interfaces. These interfaces allow us to add enhanced functionality to our module. We will only be implementing the IActionable interface in this file. The others will only be placed in this file to allow the framework to see, using reflection, whether the module implements the interfaces. The actual implementation for the other interfaces occurs in the controller class that we will create later.
Public MustInherit Class ShopList
Inherits Entities.Modules.PortalModuleBase
Implements Entities.Modules.IActionable
Implements Entities.Modules.IPortable
Implements Entities.Modules.ISearchable
Since we will be implementing the IActionable interface in this file, we will now look at the IActionableModuleActions properties that need to be implemented.
The core framework creates certain menu items automatically. These include the movement, module settings, and so on. You can manually add functionality to the menu by implementing this interface.

To add an action menu item to the module actions menu, we need to create an instance of a ModuleActionCollection. This is done in the ModuleActions property declaration.
Public ReadOnly Property ModuleActions() As _
DotNetNuke.Entities.Modules.Actions.ModuleActionCollection _ Implements
DotNetNuke.Entities.Modules.IActionable.ModuleActions
Get
Dim Actions As New _
Entities.Modules.Actions.ModuleActionCollection
We then use the Add method of this object to add and item to the menu.
Actions.Add(GetNextActionID, _
Localization.GetString( _
Entities.Modules.Actions.ModuleActionType.AddContent, _
LocalResourceFile), _
Entities.Modules.Actions.ModuleActionType.AddContent, _
"", _
"", _
EditUrl(), _
False, _
Security.SecurityAccessLevel.Edit, _
True, _
False)
Return Actions
End Get
End Property
The parameters of the Actions.Add method are: You will notice that the second parameter of the Add method asks for a title. This is the text that will show up on the menu item you create. In our code you will notice that instead of using a string, we use the Localization.GetString method to get the text from a local resource file.
| Parameter | Type | Description |
|---|
| ID | Integer | The GetNextActionID function (found in |
| | the ActionsBase.vb file) will retrieve the |
| | next available ID for your |
| | ModuleActionCollection. This works like |
| | an auto-increment field, adding one to the |
| | previous action ID. |
| Title | String | The title is what is displayed in the context |
| | menu form your module. |
| CmdName | String | If you want your menu item to call clientside code (JavaScript), then this is where you will place the name of the command. This is used for the delete action on the |
| | context menu. When the delete item is |
| | selected, a message asks you to confirm your choice before executing the command. For the menu items we are adding we will leave this blank. |
| CmdArg | String | This allows you to add additional arguments for the command. |
| Icon | String | This allows you to set a custom icon to appear next to your menu option. |
| URL | String | This is where the browser will be redirected |
| | to when your menu item is clicked. You can use a standard URL or use the EditURL |
| | function to direct it to another module. The |
| | EditURL function finds the module |
| | associated with your view module by looking at the key passed in. You will notice that the first example below passes in "Options" and the second one passes nothing. This is because the default key is "Edit". These keys are entered in the Module Definition. We will learn how to add |
| | these manually later. |
| ClientScript | String | As the name implies, this is where you would add the client-side script to be run when this item is selected. This is paired with the CmdName attribute above. We are |
| | leaving this blank for your actions. |
| UseActionEvent | Boolean | This determines if the user will receive |
| | notification when a script is being executed. |
| Secure | SecurityAccess Level | This is an Enum that determines the access level for this menu item. |
| Visible | Boolean | Determines whether this item will be visible. |
| New Window | Boolean | Determines whether information will be |
| | presented in a new window. |
Actions.Add(GetNextActionID, _
Localization.GetString( _
Entities.Modules.Actions.ModuleActionType.AddContent, _
LocalResourceFile), _
Entities.Modules.Actions.ModuleActionType.AddContent, _
"", _
"", _
EditUrl(), _
False, _
Security.SecurityAccessLevel.Edit, _
True, _
False)
Localization is one of the many things that DotNetNuke 3.0 has brought us. This allows you to set the language seen on most sections of your portal to the language of your choice. Localization is somewhat beyond the scope of this chapter, but we will at least implement it for the actions menu.
To add a localization file, we first need to create a folder to place it in. Right-click on the EganEnterprises.CoffeeShopListing project in the Solution Explorer and select Add | New Folder. Name the folder App_LocalResources. This is where we will place our localization file. To add the file, right-click on the App_LocalResources folder and select Add | Add New Item from the menu. Select Assembly Resource File from the options and name it ShopList.ascx.resx. Click on Open when you are done.

Under the name section add the resource key AddContent.Action and give it a value of Add Coffee Shop. The action menu we implemented using the IActionable interface earlier uses this key to place Add Coffee Shop on the context menu.
| To learn more about how to implement localization in your DotNetNuke modules, please see the DotNetNuke Localization white paper (\DotNetNuke\Documentation\Public\DotNetNuke Localization.doc). |
Now we can move on to the other interfaces. As we stated earlier, these interfaces only need us to add the shell of the implemented functions into this file. These will only be placed in this file to allow the framework to see, using reflection, if the module implements the interfaces. We will write the code to implement these interfaces in the CoffeeShopListingController class later.
Public Function ExportModule(ByVal ModuleID As Integer) _
As String Implements _ DotNetNuke.Entities.Modules.IPortable.ExportModule
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Function
Public Sub ImportModule(ByVal ModuleID As Integer, _
ByVal Content As String, _
ByVal Version As String, _
ByVal UserID As Integer) _
Implements
DotNetNuke.Entities.Modules.IPortable.ImportModule
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Sub
Public Function GetSearchItems( _
ByVal ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) _
As DotNetNuke.Services.Search.SearchItemInfoCollection _
Implements
DotNetNuke.Entities.Modules.ISearchable.GetSearchItems
' included as a stub only so that the core
'knows this module Implements Entities.Modules.IPortable
End Function
That is all the code we need at this time to set up our view module. Open up the display portion of the control in Visual Studio, and by using Table | Insert | Table on Visual Studio's main menu, add an HTML table to the form. Add the following text to the table:

We add the table and text because we will be testing our modules to make sure that everything is in order before moving on the more advanced coding. Again, setting test points in your development allows you to pinpoint errors that may have been introduced into your code. Once we finish the setup for the Edit and Settings controls we will test the module to make sure we have not missed anything.
Next: Module Edit Control >>
More Visual Basic.NET Articles
More By PACKT Publishing
|
This article is excerpted from chapter seven of the book Building Websites with VB.NET and DotNetNuke 3.0, written by Daniel N. Egan (PACKT Publishing, 2005; ISBN: 1904811272). Check it out today at your favorite bookstore. Buy this book now.
|
|