Placing code behind for a DNN page - asp.net

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?

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" %>

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 the # Import directive in the page, instead of the Imports statement in your VB.

Related

* is not declared. It may be inaccessible due to its protection level

I've got a problem with one of my web forms pages.
this is how it started.
I needed to completely revamp a page, I renamed the old pages default.aspx.vb.old and default.aspx.old and created brand new default.aspx and default.aspx.vb pages
every time I add a control to the aspx page and try and reference it in the code behind, I get the error 'lblError' is not declared. It may be inaccessible due to its protection level
Page decalaration
<%# Page Title="" Language="VB" MasterPageFile="~/_Masters/Principle.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Magazine_Default" EnableViewState="false" %>
Label control in Default.aspx
<asp:Label ID="lblError" runat="server" CssClass="cError" Visible="false" />
Default.aspx.vb Code Behind
Partial Class Magazine_Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblError.Text = "There was a problem retrieving the magazines, please try again later"
End Sub
End Class
the page works, but visual studio will not build because it insists there is an error.
I thought it might be something up with my visual studio, like cache or something, so i nuked any temp files relating to the project.
and this morning a colleague did a git pull and has the same problem, errors are there, refuses to build but the page works.
The project itself is a website project, so there's no designer files where I can change the access modifier of the control.
I have also tried creating a new file, using the web form scaffolding that visual studio provides through add->web form, which has all the correct page directives in the .aspx and correct class declarations in the .aspx.vb, and it still does it.
There is also no red squiggly line under lblError.Text
When I create a new page in the different folder, it doesn't happen.
As you renamed old default.aspx page, you need to rename its class too. Generally when you rename aspx page, it will not rename class name automatically. So you need to do that manually.
So for default.aspx page,
Inherits="Magazine_Default_Old"
And for default.aspx.vb page,
Partial Class Magazine_Default_Old
This will solve issue.

ASP.NET VB Page_Load not firing

Getting straight to the point, I've been searching around and can't find a solution to this. I have an ASP.NET web page with a VB code behind that isn't entering the Page_Load Sub.
I've read suggestions regarding adding "Handles Me.Load" to the Sub or checking the AutoEventWireup(tried setting it true, it's currently set to false).
I'm not finding anything so any help is appreciated in advance. Below is some of the code.
Front End:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPage.Master" CodeBehind="ThisPage.aspx.vb" Inherits="Project.ThisPage" %>
<asp:Content ID="Content" ContentPlaceHolderID="ContentPH" runat="server">
<%-- insert content here --%>
</asp:Content>
Back End:
Partial Public Class ThisPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Insert logic here
End Sub
End Class
I assume other code shouldn't be necessary as the only thing happening is some variables are being set and assigned to a control that's on the page. If that's not the case I can add more. I tried setting a break point and it's just never entering the sub.
If this is a partial class, the aspx page should inherits from the base class. Suppose the base class is Base1, then try change the .aspx file line
Inherits="Project.ThisPage"
to
Inherits="Base1"
This happened to me today. It turned out that a colleague committed an .aspx file that had a call to a code-behind function that didn't exist.
A broken commit in other words.
I pulled the code and it built without errors in VS2015 but when the broken page was due to load, the browser reported that it couldn't find it. A breakpoint in page_load wasn't hit. There was nothing to debug.
It took me a while to figure it out (which is how I found myself here), so I'm adding this answer in case someone else has the same problem.
I had just upgraded to Windows 8.1 from Windows 7 and Visual Studio selected the wrong project in the solution as the StartUp project. Selecting the correct project resolved the issue.

aspx file not seeing code behind

I have a web application that I have been developing for some time. Yesterday when I loaded the project into VS the aspx files could no longer see the aspx.vb files. I reverted to the backup I had just made and everything was fine. Today, the same problem.
Example:
FILE: QuerySql.aspx
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master"
CodeBehind="QuerySql.aspx.vb" Inherits="WhatIsPouring_Ajax.QuerySql" %>
FILE: QuerySql.aspx.vb
Public Class QuerySql Inherits DBFunctions
Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
tbPassword.Text = sPassword
tbUser.Text = sUserName
tbServer.Text = sDBServer
End If
tbError.Text = "The system is loaded."
End Sub
There is a public variable sDBServer in the code behind file. The .vb file sees all the controls on the aspx side. If I type <%=sDBServer %> it displays an error "sDBServer is not declared..."
I believe it has something to do with the project configuration somewhere, but I don't know where to go the fix it. I have cleaned, built, rebuilt, etc many times.
Any help is appreciated.
Your code behind is missing the Partial keyword, e.g.
Partial Class QuerySql Inherits DBFunctions
Make sure that the designer generated class also has the same keyword to bind the ASPX file to your class.
If the designer file is missing you can regenerate it by right-clicking the ASPX page and selecting "Convert to Web Application"
How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?
I have experience this problem as well. The vb.net web project i worked on, showed thousands of errors like 'BC30002 Type '....' is not defined. But this code did not seem to be faulty.
For me the following worked, but have no clue why!!:
Every designer class looked like this:
Partial Public Class CSSSRTOMOpleidersSearch
Removed the 'public' statement:
Partial Class CSSSRTOMOpleidersSearch
After that, the codebehind and designer were able to find each other

Add button to existing site

There is an existing site with no compiled DLLs, it's all .aspx.vb and .aspx files.
First question is that I can see
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master"
MaintainScrollPositionOnPostback="true" AutoEventWireup="true"
CodeFile="ThisPageName.aspx.vb" Inherits="ThisPageName" %>
But where is the file that it is inheriting from? I work in C# more and in the compiled variety, I can see this other source file
Partial Class ThisPageName
But where is the other half of the partial class to be found?
The reason for the question is that I'm trying to activate a commented out <asp:button> but when I add the event handler:
Protected Sub btnWasHidden_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnWasHidden.Click
I get this:
Compiler Error Message: BC30506: Handles clause requires a WithEvents variable
defined in the containing type or one of its base types.
And
E:\path\path\htdocs\ThisPageName.aspx.vb(304) : error BC30451:
Name 'btnWasHidden' is not declared.
I am not sure about non-compiled sites, but normally the "other half" of the page class is kept in the pagename.aspx.designer.vb file, which VS updates as you modify the markup (.aspx) page.
If you are doing this outside of the context of Visual Studio, you might need to add the member variable for the control to the class manually in either the designer file or your main class file (code behind).
EDIT: Here is how the .aspx.designer.vb file typically generates member variables for server-side controls:
Protected WithEvents <control_id> As <full namespace and type of control>
e.g.
Protected WithEvents TextBox1 As Global.System.Web.UI.WebControls.Button
Just follow that pattern in the normal code-behind .vb file and it should work.
In your case I believe it will be:
Protected WithEvents btnWasHidden As Global.System.Web.UI.WebControls.Button

Adding custom attribute to a class defined in asp.net inline code

I have an asp.net page that is already written with inline vb.net. All I really need to do is add a custom attribute to the class but it can't figure out where to add it.
<%# Page Language="vb" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
...
End Sub
</script>
In the code that checks for the attribute on the page object, I can see the page.GetType.fullName = "ASP.test_aspx" with the page filename as test.aspx.
Is this possible?
I don't think you can do this directly as the class is autogenerated and I'm pretty sure that the compiler doesn't have provision for adding attributes. Personally, I'd rewrite the page with a separate class file (I always prefer to do that anyway). If that isn't an option, depending on the attribute and what is using it, you may be able to create a class that inherits from Page with the attribute on and inherit from in the the #Page directive

Resources