Include code in ASP.net - asp.net

I've got an ASP.net file, and I'm trying to include dynamic code which was easy in classic ASP.
Here is what I have tried so far:
<%# Register TagPrefix="TagPre" TagName="header" Src="alg/classes.aspx"%>
and
<!--#include file="alg/classes.aspx"-->
But neither of these seem to work. The content of classes.aspx is:
<script runat="server">
' Square class
Public Class square
Public sqRows As Integer 'Numbers of rows this square has
Public sqCols As Integer 'Number of columns this square has
Public sqArray(,) As Integer 'The square array
' Initialise square array to match size of canvas
Public Sub initSqArray(ByVal canvCols, ByVal canvRows)
ReDim sqArray(canvCols, canvRows)
sqRows = canvRows
sqCols = canvCols
End Sub
End Class
Thanks for any help!

What is the concrete problem or error you got? When you are using .aspx, did you already try to put the VB-Code in the Code-Behind-Sheet and pull it from there?

You should not need to use include or register to access a class. You just need to save your class to a class file (.vb) and put the class in your app_code directory (if you are using a website project) or put it anywhere in a web application project (preferrably a folder for classes) but include it in your project namespace. This should make your class visible anywhere in the website or web application.

Related

How can I add Console dll file in web application?

By following the CHATGPT guidelines I added a Console file dll (Inboundnightlybatchfile is my console file) in the bin folder of my web application. To access the dll need to import in our application. So I write this using Inboundnightlybatchfile; But the dll file name does not appear in the IntelliSense, and it throws a compile time error.
I want to add the Console file dll to an Web application
Well, HOW do you share ANY code in your existing application, or code from "other" applications?
why of course you have/put that code in a class.
I mean in your project now, how do you use/enjoy a bunch of helper code and routines?
You wind up with a class, and then in code create a instance of that class.
So, say I have a class that holds some information about a hotel?
Then you add a class to your project, and then simple use that class in code.
So, say this class:
Public Class Hotel
Public HotelName As String
Public City As String
Public HotelDescripiton As String
Public FirstName As String
Public Lastname As String
Public ReadOnly Property FullName As String
Get
Return FirstName & " " & Lastname
End Get
End Property
End Class
Then in code, you can use that "code"
eg:
Dim OneHotel As New Hotel
OneHotel.HotelName = "Super 8 Hotel"
OneHotel.FirstName = "John"
OneHotel.Lastname = "Smith"
Debug.Print(OneHotel.FullName)
Output:
John Smith
Same goes for a console application. If you want share that code, then move the console code you want to share to a class.
Say I have this console application. When I run it, it will display all my drives on the computer.
So, from command prompt I have this:
So, now lets say I want to use above in my web site - to display/list all the hard drives.
So, the code for above console application is this:
Sub Main()
Dim cMyCode As New clsSharedCode
Dim sDriveList As List(Of String)
sDriveList = cMyCode.GetDrives
For Each s As String In sDriveList
Console.WriteLine(s)
Next
End Sub
But, the "working" code for that console been moved to a class called clsSharedCode.
That is this:
Imports System.IO
Public Class clsSharedCode
Public Function GetDrives() As List(Of String)
Dim DriveList As New List(Of String)
Dim allDrives As DriveInfo() = DriveInfo.GetDrives
For Each d As DriveInfo In allDrives
Dim s As String
s = $"Drive:{d.Name} Volume:{d.VolumeLabel} Type:{d.DriveType} Ready:{d.IsReady}"
DriveList.Add(s)
Next
Return DriveList
End Function
End Class
Ok, so now lets use above in our web site.
We simple set a reference to vbconsole.exe.
So, now this web page markup:
<h3>Drives on computer</h3>
<asp:ListBox ID="ListBox1" runat="server" Height="223px" Width="423px">
</asp:ListBox>
code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim cConsoleCode As New VBConsole.clsSharedCode
Dim sDriveList As New List(Of String)
sDriveList = cConsoleCode.GetDrives
For Each sDrive As String In sDriveList
ListBox1.Items.Add(sDrive)
Next
End If
End Sub
And then we get this:
So, to "share" code? How this works EVEN with a simple plain jane console application is quite much the same as you "always" done this.
This does mean/suggest that the working code in your console application should be moved out to a shared code class that BOTH the console application, and now your web application can use and enjoy.
But, be a a web site, or even a simple console application?
The why and how of sharing the code quite much is the same in .net. You share code by using a object approach, and this is quite much the WHOLE foundation as to how .net code works - including that of sharing code between systems and applications.
So, in my web application, I simple set a reference to vbconsole.exe, and now I am free to use + consume any code in that console appliation. however, as noted, like you ALWAYS done to share code? You will as a general rule move that code out to a class, and then any other application is free to consume + use that code by using the public class code you have in that application. So, that MySharedCode class in the console application can now be used by any other .net application, include that asp.net web site.
so, .net is "primarily" a object based system, and this is the main pillar and foundation of how you re-use and share .net code. You share such code by creating class(s) with that code, so then your existing application (the console application) can use those class(s), and as above shows, by a simple reference to the vbconsole.exe application, the web site can also use + enjoy + consume that code also.
So, becasue a class forces you to seperate out the UI parts from the code parts, then even adopting this approach in a simple console application means that such code can be used + consumed by any type of application. you can write another console applcation, and reference our above console application and use that code.
but, we can also create a windows desktop program, and again reference the vbconsole.exe application. And so can our web based applcation.
And even MORE amazing? You can consume the above class in c#, or vb.net, and again it don't matter!

VB.Net: call sub from shared sub

I have some Ajax on a web page that feeds some data to a server-side VB.Net method. Once that data is in the server-side method, I need to call another server-side method to use the data I just collected. Here is a really simplified example:
' This method gets the input from the Ajax code on the web page.
<System.Web.Services.WebMethod> _
Public Shared Sub GetAwesome(VBInputText As String)
Dim strTest As String = VBInputText
' Now that we have collected input from the user,
' we need to run a method that does a ton of other stuff.
DisplayAwesome(VBInputText)
End Sub
Protected Sub DisplayAwesome(AwesomeIn As String)
' The real app does a lot more than this. For this example, it
' just sets the text of a literal.
litAwesomeResult.Text = AwesomeIn
End Sub
Of course, in the above example DisplayAwesome(VBInputText) gives me the 'Cannot refer to an instance member...' error. So, is it possible now to call Protected Sub DisplayAwesome from Public Shared Sub GetAwesome? I'm hoping to stay close to this sort of solution because it would play very well with the app as it is already written by another coworker.
unfortunately you cannot do this, Since the page method DisplayAwesome is defined as Protected and you requires an instance of the class to access the Protected method. But changes in another instance will not reflect in the current UI. another thing you can do is Make DisplayAwesome as Shared, but this time you cannot access the UI elements inside the shared function.
The thing you can do in this situation is, return data to the called method(in front end) and handle the litAwesomeResult.Text there
Call sub with name of Form Class like this:
FormName.DisplayAwesome(VBInputText)
In VB.Net, you can call the method not shared from a shared method with Name of Form Class by default instance, because The default instance is an object Form type that the VB application framework create and manage it, when the form is added to the project.
For more info see this :
VB.NET Default Form Instances

"Argument not specified for parameter" using Ninject in web forms

Just to re-iterate the title, this is web forms and not mvc. Also it's using vb.net.
I have used nuget to add ninject.web to my project. I have configured NinjectWebCommon's "RegisterServices" method as follows:
Private Shared Sub RegisterServices(kernel As IKernel)
kernel.Bind(Of ilbdUOW)().[To](Of lbdUOW)()
End Sub
Everything compiles clean. When I run the simple web form (.aspx page, no master page), I get the following error:
BC30455: Argument not specified for parameter '_uow' of 'Public Sub New(_uow As lbD.data.lbdUOW)'.
And it points to line 52 in the compiled code:
Line 51: Public Sub New()
Line 52: MyBase.New
Line 53: Dim dependencies() As String
which (I believe) comes from the top of the .aspx page when compiled, where I have the following code for the "new" constructor:
<Inject()> _
Public Sub New(_uow As ilbdUOW)
Uow = _uow
End Sub
(well, I say "I believe" but I know it is as if I change the "ilbdUOW" from the interface to the class of "lbdUOW" it changes the error message accordingly so that's definitely where it's coming from)
If I put a debug stop on the "uow = _uow" line in my "New" constructor and also another debug stop in the "RegisterServices" method in the NinjectWebCommon file, the debugger only stops on the "RegisterServices" debug point and never gets to hit the debug stop in the main .aspx page.
I'm now completely lost. I am trying to get my head around what is relatively new to me and I thought I was pretty much getting there. My understanding is that the code in "RegisterServices" in NinjectWebCommon will recognise that I'm requiring an instance of my "ilbdUOW" interface when it is referenced in a constructor (such as my "new" constructor in my .aspx page) and will inject it automatically, thus making the unit of work instance available for me to access the database with.
If anyone can point me in the right direction I'd be really grateful, thanks.
Edited to add:
#mipe34
Not on the "new" constructor, no:
Public Sub New(ByVal repositoryProvider As IRepositoryProvider)
MyBase.New()
CreateDbContext()
repositoryProvider.DbContext = DbContext
_RepositoryProvider = repositoryProvider
End Sub
I have also set up the "RegisterServices" section to map those interfaces too, but it makes no difference to the error I get whether those extra mappings are in there or not, it seems to fail before I get to that point.
I have had these lines in and out of the "RegisterServices" section and it makes no difference:
kernel.Bind(Of RepositoryFactories)().[To](Of RepositoryFactories)().InSingletonScope()
kernel.Bind(Of IRepositoryProvider)().[To](Of RepositoryProvider)()
WebForm are created by ASP.NET not Ninject You can't use constructor injection on WebForms. You have to do property injection instead.

Accessing public class variables in asp.net without session

I am using this example I found to learn how to load class files and access variables through them. This is in a file called Class1.vb in the App_Code folder (this is not an app project):
Imports Microsoft.VisualBasic
Public Class my_class
Public Shared Sub my_sub()
Dim vartest As String
vartest = 10
HttpContext.Current.Session("myvar") = vartest
End Sub
End Class
This is the codebehind on the aspx file:
Imports my_class
Partial Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
my_class.my_sub()
Label1.Text = HttpContext.Current.Session("myvar")
End Sub
End Class
How could I access the vartest variable without using a session, since if this is accessed by multiple functions at the same time the variable can be overwritten I assume. Is it possible to go the other way, where a variable is sent to a class file?
It sounds like you need a quick overview of some basic ASP.Net Webforms concepts. Up first I'll counter a common newbie misconception:
Your Page class does not hang around on the web server for very long
I think many new ASP.Net developers have this idea of the web server keeping a single instance of their page class for every user session that hits their site, and each postback or event uses this same page class instance. That's just not how it works. ASP.Net page class instances are nearly always created and destroyed again in well under a second, and most experienced developers see it as a big problem if it takes longer.
ASP.NET relies on the HTTP protocol
The thing to remember here is ASP.Net still relies on the HTTP protocol, and http boils down to requests and responses. When you view a web page, your browser first sends a request to a server. The server responds, usually with an html document. The browser will then parse the html; based on what it sees in the html the browser may send more requests to the server for additional resources, such as javascript, images, or css files. Each request results in a separate response, and the browser uses all these resources to render the page to the screen. However, the ASP.Net runtime normally does not have to process the additional requests (that would make things slower) — ony the initial html needs ASP.Net support; you want the other resources to be basic files that can be cached.
The ASP.Net runtime creates a new instance of your class for every request.
When the ASP.net runtime processes a request for a page, it will create a new instance of your page class. The runtime will follow the ASP.Net Page lifecycle (this should really be named the "ASP.Net Page Request Lifecycle"), and call certain methods or raise certain events in this class instance, in a specific order defined by the lifecycle.
This means every postback or event runs in a different instance of your class.
It also means every postback or event is rebuilding and transmitting all of the html the goes into your page, and not just the portions you want to change. For your server code, the consequence is the only thing class-level variables are really good for in ASP.Net is things that will be used within a single http request. For the browser, the consequence is you're working with a brand new DOM after every event.
To understand all of that, it's important here to also have a good understanding of the difference between a class and an instance of a class. A couple items in your question make me unsure whether you have this understanding yet.
The ASP.Net runtime shares one application instance among all users of your site
The web server typically only has one instance of your application for the entire web site and all it's users. Therefore, anything with a Shared/static scope is common to every user. It's rarely appropriate in ASP.Net for anything to be Shared/static.
So how do you handle data that should live with a single user or visit to your site?
This is exactly what the Session is for. A session will always be unique to an individual request at any given time. You're worried about multiple functions accessing the session at the same time, but this does not happen. The ASP.Net Page Lifecycle ensures that unless you manually spawn additional threads, only one function at a time is running for a given HttpContext and Session. If a user somehow sends two requests at about the same time that should have the same Session/HttpContext, one will be held by the ASP.Net runtime until the other is completed. If you don't want to reference the session all the time, you can build properties in your class that wrap session variables. See #Pankaj's answer for an example.
First, a Session has user-scope, so it will not be overwritten by another Request.
Is it safe to access asp.net session variables through static properties of a static object?
You could encapsulate the access into a property:
Public Shared Property MyVar() As String
Get
If HttpContext.Current.Session("MyVar") Is Nothing Then
HttpContext.Current.Session("MyVar") = ""
End If
Return DirectCast(HttpContext.Current.Session("MyVar"), String)
End Get
Set(value As String)
HttpContext.Current.Session("MyVar") = value
End Set
End Property
Then you can get the variable by:
Label1.Text = my_class.MyVar
In addition to the "Tim Schmelter" reply....
You can create a BaseClass which will inherit from
System.Web.UI.Page
Place the property as suggested by "Tim". The only change you need to do is to change the access modifier to Protected and you should remove Public and Shared
You can also keep other common functions, properties that can we reused in other classes also... Similarly you can create BaseControls as well for your User controls
Finally, inherit this class in the web form....
Hope this will help you...
Base Class code
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Public Class BaseClass
Inherits System.Web.UI.Page
Protected Property MyVar() As String
Get
If HttpContext.Current.Session("MyVar") Is Nothing Then
HttpContext.Current.Session("MyVar") = ""
End If
Return Convert.ToString(HttpContext.Current.Session("MyVar"))
End Get
Set
HttpContext.Current.Session("MyVar") = value
End Set
End Property
End Class
Sample Code "Behind Code" - Showing the usage of Protected member Data from Base Class
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Partial Class Default5
Inherits BaseClass
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim str As String = Me.MyVar
End If
End Sub
End Class
Generally you can use different places to store application state: Application (application wide, saves state into application domain), Session (there can be saved everything what will be accessed by current browser session), ViewState (variables stored in hidden input field and will be posted on every postback). Of course you can also save state to database or file. I'm not sure what you want to achieve, but looks like you looking for something like ViewState.
Read ASP.NET State Management

Getting project web page names as an enumeration in asp.net

I know that in markup view Visual Studio will provide you with an enumeration of all the page names in your project (add an element and see what you get from Intellisense when specifying the ImageUrl attribute).
My question is: how do I get to that enumeration?
If that's not possible, what would be the best way in asp.net to get the names of your pages without having to hard code strings all over the place? E.g., I'd like to be able to do something like this:
Response.Redirect(PageNames.Default);
(Where PageNames is an enum of some sort)
Is this possible? Thanks in advance!
Here is one suggestion...
Define a class that includes the pages you want, either manually or by reading a Site Navigation file:
static class PageNames
{
public static string Default = "~/Default.aspx";
public static string Contact = "~/Contact.aspx";
public static string About = "~/About.aspx";
}
You can use the class by calling the property name:
Response.Redirect(PageNames.Default);
Another option, that I have looked at but not tried out yet (might next week tho'):
http://blog.devarchive.net/2008/01/auto-generate-strong-typed-navigation.html
Looks very cool, uses T4 templating to generate a strongly typed navigation hierarchy.

Resources