Referencing class library in inline vb code - asp.net

I'm working on a legacy vb.net application that does most of its work using inline code (
Within that script I need to access functions from a third party .net dll.
The dll(s) themself are stored in the GAC.
Before I started the page looked something like the following
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'''code here
End Sub
</script>
For instance say the dll is called Foo.dll and I need to access the Bar class and the .Run() method
ie.
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim bar as Foo.Bar = new Foo.Bar()
bar.Run()
End Sub
</script>
I have tried to reference the dlls using
<%# Import Namespace="Foo" %>
in the same place as the other imports however I receive the following
error BC30002: Type 'Foo.Bar' is not defined
Is there some other way I should be refering this third party dll, I'm mostly a C# guy and haven't dealt much with VB or this kind of inline code. Note the code I have shown is all from a user control (.ascx)

Turns out I needed to add
<%# Assembly Name="Foo, Version=x.x.x.x, Culture=neutral, PublicKeyToken=sfgfdsgfdsgsdg" %>
as it was in the gac and not being automatically referenced

You have added a reference to the assembly, correct? You do have to have a reference to the 3rd party assemblies before you can use the Imports statements with their namespaces.
If you want to try to create the object without a direct reference to the dlls you could try the CreateObject method. But I suspect a better answer would be to get the references working correctly so that you don't need the CreateObject method.

Related

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

Placing code behind for a DNN page

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.

Resources