Lifes
Lifes

Reputation: 1317

Creating a C# Module in DNN

I want to embed a C# class in a module so that I can call the functions using buttons and click events. I have no idea how to do this. I've managed to write the class I want to use, but where do I put the code? I created a module in DNN and got this:

<%@ Control Language="C#" ClassName="MailingSystem" Inherits="DotNetNuke.Entities.Modules.PortalModuleBase" %>
<h1>Congratulations</h1>
<p>You have successfully created your module.  You can edit the source of the module control by selecting the View Source Action from the Action Menu.</p>

<script runat="server">

</script>

I can't put my code in here, I get all sorts of errors about namespaces not allowed, can't import classes with "Using", and so on. So what am I supposed to do? My class is working, I just need to wrap it in a module and put it on a DNN page.

Upvotes: 0

Views: 2522

Answers (4)

lukejkw
lukejkw

Reputation: 1134

If you don't want want to go the whole module template route. Do the following.

  1. Create an webusercontrol (.ascx)
  2. Go to the code behind file (.ascx.cs) and change the class to inherit from DotNetNuke.Entities.Modules.PortalModuleBase (your will need to add DotNetNuke.dll as reference)
  3. Add what ever controls you want to the ascx and attach any event handlers. I prefer to do this in the page init method

In ASCX:

    <asp:Button ID="btnButton" Text="Click me" runat="server" />

In Code Behind:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnButton.Click += btnButton_Click;
        // OR
        btnButton.Click += (sender, e)=> { // Button clicked! Do something };

    }

    protected void btnButton_Click(object sender, EventArgs e)
    {
         // Your button has been clicked, Do something

    }
  1. Compile code

  2. Get [yourprojectname].dll file from the bin folder of your project and copy it into DNN's bin folder. Then, copy your module control ascx into a dedicated folder in DNN's DesktopModules Folder

Example Path: DesktopModule > YourProjectName > [YourASCXName].ascx

  1. Login to DNN, go to Host>Extensions and click add extension. Go through the wizard making sure to set your extension type to Module (there are many different types of extensions in DNN).

  2. Once added, you will be be taken back to the module extensions page. Scroll down and find your module extension. Click edit, go to module definitions and add a module definition with a meaningful name.

Example: YourProjectNameMainView

  1. Then, add your ASCX file as a view to that module extension. Click save and you are done with setup

You should be able to drop your (VERY BASIC) module on a page and use it!

Upvotes: 0

Maciej
Maciej

Reputation: 11

You may want to do something like this:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        /// code goes here
    }
</script>

Upvotes: 1

Mohammad Dehghani
Mohammad Dehghani

Reputation: 11

simply you can double click on design part of the page , then the page load section will be appear in the page and you can put your c# code there.

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104168

It is better to start with a DotNetNuke Module Template, like this one. It isn't as easy as creating an aspx page.

Upvotes: 1

Related Questions