The purpose of this article is to demonstrate the required steps for creating a sample DotNetNuke module that uses OpenAccess for its DAL.
CREATING THE DATABASE
First thing before we implement the sample module is to create the database that will be used for storing the module data. It contains only one table called “TelerikSampleProduct” which will laterprovide the entities used in our database CRUD operations.
SETTING UP THE MODEL
Open the Dnn solution and create a new project of type “ClassLibrary” and name it “TelerikOpenAccessSampleModel”. Enable it to use OpenAccess and reverse map the “DnnOpenAccessIntegration” database that was created in the previous step. That creates a “TelerikSampleProduct” class with the following fields: “id, price, description, category, moduleId and userId”. Then add a project reference for the newly created project to the Dnn website project in the solution.
CREATE THE MODULE
In the “Desktop Modules” folder of the Dnn website create a new folder named “OpenAccessModule”.

Then create a new web user control in it and name it “View.ascx”


When the “View.ascx” page opens, locate the “Inherits” line on the top and change it like the following:
|
Copy Code |
|
Inherits="DotNetNuke.Modules.OpenAccessModule.View" |
In the code-behind file replace all code with the following code:
| C# |
Copy Code |
|
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using TelerikOpenAccessSampleModel; using DotNetNuke; using DotNetNuke.Security;
namespace DotNetNuke.Modules.OpenAccessModule { public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase { protected void Page_Load(object sender, EventArgs e) {
} } |
| VB .NET |
Copy Code |
|
Imports Microsoft.VisualBasic Imports System Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.HtmlControls Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports TelerikOpenAccessSampleModel Imports DotNetNuke Imports DotNetNuke.Security Namespace DotNetNuke.Modules.OpenAccessModule Partial Public Class View Inherits DotNetNuke.Entities.Modules.PortalModuleBase Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If (PortalSecurity.IsInRole("Registered Users") OrElse PortalSecurity.IsInRole("Administrators")) Then LinkButton1.Enabled = True Else LinkButton1.Text = "You must be logged in to add a Listing" LinkButton1.Enabled = False End If End Sub |
Switch to the designer - “View.ascx” page and from the toolbox drag an OpenAccessDataSource control.
From the smart tag choose “Configure Data Source”.

In the second step of the dialog that pops up click “Filter” and set it up like the following:

NOTE: This instructs the OpenAccessDataSource to filter the results by moduleId. We will set the values of this field later in a code-behind function.
SETTING UP THE UI
Drag a “GridView” control from the toolbox and set the OpenAccessDataSource that was created as the data source for the grid.

Check the options Enable Paging, Enable Sorting, Enable Editing, and Enable Deleting on the smart tags list of GridView.
Next click the “Edit columns” link from the “GridView” options.
Select the ID column in the Selected Fields section, and under the BoundField properties section, change Visible to False.

The same should be done with the UserId and ModuleId fields.
The grid should now have the look as shown below:

The next step is to place a “FormView” control from the toolbox on the design surface. It should also have the OpenAccessDataSource set as a data source.

After accomplishing that, click the edit templates link from the smart tag of the “FormView” and select “InsertItemTemplate” from the Display combo-box.

Switch to source view and replace the InsertItemTemplate section with the following code:
|
Copy Code |
|
<InsertItemTemplate> Category: <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# Eval("Category") %>' SelectedValue='<%# Bind("Category") %>' EnableViewState="False"> <asp:ListItem>Home</asp:ListItem> <asp:ListItem>Office</asp:ListItem> <asp:ListItem>Electronics</asp:ListItem> <asp:ListItem>Misc.</asp:ListItem> </asp:DropDownList> Price: $ <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' Width="56px" CausesValidation="True" EnableViewState="False"></asp:TextBox><br /> <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="Price must be greater than 0" MaximumValue="99999" MinimumValue="1"></asp:RangeValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="A price is required"></asp:RequiredFieldValidator><br /> Description:<br /> <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' MaxLength="499" TextMode="MultiLine" Width="286px" EnableViewState="False"></asp:TextBox><br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click"></asp:LinkButton> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" OnClick="InsertCancelButton_Click"></asp:LinkButton> </InsertItemTemplate> |
Switch to design view, select Edit Templates and then select InsertItem Template and the form will resemble the image bellow:

In the properties of the “FormView” set the DefaultMode property to Insert and the Visable to False.

Last step in the designer part of the implementation is to drag a “LinkButton” control from the toolbox and drop it between the “GridView” and “FormView”. Set the Text property of the “LinkButton” to be “Add My Listing”.
CREATING THE CODE BEHIND
Subscribe for the “Inserted” event of the OpenAccessDataSource. That can be accomplished by double clicking on the corresponding event in the event view in the Visual Studio Designer. When doing so, the necessary code for the method that “listens” for the event – “OpenAccessDataSource1_Inserted” is created in the code behind. The body of the method should look like this:
| C# |
Copy Code |
|
protected void OpenAccessDataSource1_Inserted(object sender, Telerik.OpenAccess.OpenAccessDataSourceChangedEventArgs e) { this.GridView1.DataBind(); } |
| VB .NET |
Copy Code |
|
Protected Sub OpenAccessDataSource1_Inserted(ByVal sender As Object, ByVal e As Telerik.OpenAccess.OpenAccessDataSourceChangedEventArgs) Handles OpenAccessDataSource1.Inserted
Me.GridView1.DataBind() End Sub |
This method instructs the grid to refresh itself after a record has been inserted.
Next event that needs to be subscribed to is the “Inserting” event of the datasource. The code for the body of the method generated is the following:
| C# |
Copy Code |
|
protected void OpenAccessDataSource1_Inserting(object sender, Telerik.OpenAccess.OpenAccessDataSourceChangingEventArgs e) { TelerikSampleProduct telerikProduct = (TelerikSampleProduct)e.Entity; telerikProduct.UserID = Entities.Users.UserController.GetCurrentUserInfo().UserID; telerikProduct.ModuleId = ModuleId; } |
| VB .NET |
Copy Code |
|
Protected Sub OpenAccessDataSource1_Inserting(ByVal sender As Object, ByVal e As OpenAccessDataSourceChangingEventArgs) Handles OpenAccessDataSource1.Inserting
Dim telerikProduct As TelerikSampleProduct = CType(e.Entity, TelerikSampleProduct) telerikProduct.UserID = Entities.Users.UserController.GetCurrentUserInfo().UserID telerikProduct.ModuleId = ModuleId End Sub |
This method sets the UserId and ModuleId properties of the “TelerikSampleProduct” object to be inserted to the current ModuleId and UserId that are set by DotNetNuke for every module or user.
After the “Inserting” comes the implementation of the method listening for the “Selecting” event:
| C# |
Copy Code |
|
protected void OpenAccessDataSource1_Selecting(object sender, Telerik.OpenAccess.OpenAccessDataSourceSelectingEventArgs e) { e.DataSource.WhereParameters["ModuleId"].DefaultValue = ModuleId.ToString(); } |
| VB .NET |
Copy Code |
|
Protected Sub OpenAccessDataSource1_Selecting(ByVal sender As Object, ByVal e As Telerik.OpenAccess.OpenAccessDataSourceSelectingEventArgs) Handles OpenAccessDataSource1.Selecting e.DataSource.WhereParameters("ModuleId").DefaultValue = ModuleId.ToString() End Sub |
Code behind for the click event of the “Add My Listing” button:
| C# |
Copy Code |
|
protected void LinkButton1_Click(object sender, EventArgs e) { this.FormView1.Visible = true; GridView1.DataBind(); } |
| VB .NET |
Copy Code |
|
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Me.FormView1.Visible = True GridView1.DataBind() End Sub |
The following is the event listener for the RowDataBound event of the “GridView”:
| C# |
Copy Code |
|
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((e.Row.RowType == DataControlRowType.DataRow)) { TelerikSampleProduct telerikProduct = ((TelerikSampleProduct)(e.Row.DataItem));
if ((PortalSecurity.IsInRole("Administrators")) || (Entities.Users.UserController.GetCurrentUserInfo().UserID == (int)telerikProduct.UserID)) { e.Row.Cells[0].Enabled = true; } else { e.Row.Cells[0].Text = " "; } } } |
| VB .NET |
Copy Code |
|
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If (e.Row.RowType = DataControlRowType.DataRow) Then Dim telerikProduct As TelerikSampleProduct = (CType(e.Row.DataItem, TelerikSampleProduct)) If (PortalSecurity.IsInRole("Administrators")) OrElse (Entities.Users.UserController.GetCurrentUserInfo().UserID = CInt(Fix(telerikProduct.UserID))) Then e.Row.Cells(0).Enabled = True Else e.Row.Cells(0).Text = " " End If End If End Sub |
this method casts "e" which is an instance of the object that contains the data for the current row, as a TelerikSampleProduct object. It then compares the UserID to the UserID of the current user. If the UserID matches the current user or the current user is an administrator it enables the first column on the GridView (this allows a user to edit the row).
Aditional methods for the code behind:
| C# |
Copy Code |
|
protected void InsertButton_Click(object sender, EventArgs e) { this.FormView1.Visible = false; LinkButton1.Text = "Update Successful - Add Another Listing"; this.GridView1.DataBind(); }
protected void InsertCancelButton_Click(object sender, EventArgs e) { this.FormView1.Visible = false; this.GridView1.DataBind(); } |
| VB .NET |
Copy Code |
|
Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As EventArgs) Me.FormView1.Visible = False LinkButton1.Text = "Update Successful - Add Another Listing" Me.GridView1.DataBind() End Sub Protected Sub InsertCancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Me.FormView1.Visible = False Me.GridView1.DataBind() End Sub |
Alter the Page_Load method to look as the following:
| C# |
Copy Code |
|
protected void Page_Load(object sender, EventArgs e) {
if ((PortalSecurity.IsInRole("Registered Users") || PortalSecurity.IsInRole("Administrators"))) { LinkButton1.Enabled = true; } else { LinkButton1.Text = "You must be logged in to add a Listing"; LinkButton1.Enabled = false; } } |
| VB .NET |
Copy Code |
|
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If (PortalSecurity.IsInRole("Registered Users") OrElse PortalSecurity.IsInRole("Administrators")) Then LinkButton1.Enabled = True Else LinkButton1.Text = "You must be logged in to add a Listing" LinkButton1.Enabled = False End If End Sub |
(This code determines if the user is logged in and displays the “Add Listing” link if they are).
CREATING THE MODULE DEFINITION
While logged into your DotNetNuke site as "host", in the web browser, from the menu bar select "Host". Then select "Module Definitions".

Click “Create New Module” from the bottom of the page:

In the new form insert “OpenAccessModule” for both required fields – “Name” and “Friendly Name” and click “Next”.

Fill in the remaining fields with some common values and click “Next”:

Then locate your new module in the page and click on the
“Edit” button:

Then locate the “Add Definition” button, click it, fill it with the values corresponding to the newly created module and then click on the “CreateDefinintion” button:

Click the “Add Module Control” and fill the new form in the following way and click “Update”:

The module is ready for use and to use it as part of the Home-Page or any other Dnn page just select it from the “Module” drop down menu.
