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

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

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.

ASP.NET: Get value of literal.text after page load?

I'm trying to use the following code to get the text value of a literal so that I can pass it to the next page using PreviousPage, but the string is empty, I think it clears the values after the Page_Init stage.
Public ReadOnly Property SendText() As String
Get
Return literal1.Text
End Get
End Property
Is there a way around this?
Im trying to access the data on the next page using:
<%# PreviousPageType VirtualPath="~/Spacing.aspx" %>
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
litTables.Text = PreviousPage.SendText
End Sub
Even better would be if I could set SendText to the HTML contents of a whole div, but the only way I see to access this is using the JavaScript document.getElementById("tables").innerHTML, but I don't see how I could incorporate that into the ReadOnly Property using Get.
I'm using Visual Studio Express For Web 2012.
This isn't really the way you would pass data between pages in an ASP.NET application. The recommended way would be to POST the data or pass it via the URL.
The data I need to transfer is:
<div id="tables">
<asp:Literal ID="lit1" runat="server" ValidateRequestMode="Disabled"></asp:Literal>
<asp:Literal ID="lit2" runat="server"></asp:Literal>
<asp:Literal ID="lit3" runat="server"></asp:Literal>
</div>

Referencing class library in inline vb code

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.

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

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