views:

472

answers:

2

Hi,

I'm having troubles finding where I can put code-behind for my dnn pages.

For example:

MyPage.ascx already has

<%@ Control language="vb" CodeBehind="~/admin/Skins/skin.vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.UI.Skins.Skin" %>

which it needs in order to be cast to a skin.

However, I want to be able to add a VB function that is executed on Page_Load, so I made my own code-behind file. But I can't take out the current control (one referencing skin.vb) to put in my own, and you can't have more than one Control.

I also tried embedding the code in a tag, but I need to do some Imports which give me an error saying they must be declared at the beginning of the file etc etc...

Anyone know how to properly add code-behind for DNN pages?

A: 

You should be able to add a codebehind file that, itself, inherits from Skin.

However, I would suggest keeping your skin contained in the .ascx file itself (it is very uncommon for DNN skins to include code). To add the Imports, you can use @ Import directive in the page, instead of the Imports statement in your VB.

bdukes
+2  A: 

To keep the skin as self-contained as possible, I typically add a script block into the skin's ascx file, below all the HTML in the skin (so it's sort of out of the way), like so:

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            ' first page load logic here

        End If

        ' other page load logic here

    End Sub

    Private Function DoStuff(ByVal input As String) As Integer

        ' custom function logic

    End Function
</script>

If my code requires any additional namespaces, I place them at the top of the skin's ascx file in import statements, like so:

<%@ import namespace="System.Data" %>
<%@ import namespace="System.Collections.Generic" %>
<%@ import namespace="MyCustomLibrary" %>
Tim S. Van Haren