Code Behind Page

S

Shapper

Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file
named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel
 
G

Guest

It would be better if you used either a module or a class to put your common
functions. If you use a class, you can declare your methods as Shared so that
you won't have to create an instance of the class to use them. For example, I
have a class of common data routines called SQLDataRoutines, most of which
are shared. Here is a shared function that returns a SQL connection:

Public Shared Function GetConnection(ByVal connectionString As String)
As SqlConnection
Dim connection As New SqlConnection(connectionString)

Try
connection.Open()
Return connection

Catch ex As Exception
Throw ex
End Try

End Function

So behind any of my WebForms I can call it like this:
dim cnn as SQLConnection
dim strConn as String = "Put your connection string here"
cnn = SQLDataRoutines.GetConnection(strConn)
....the rest of your code

Hope this helps.
 
E

Egghead

Why not make a vb.net or C# class holds all your common functions and put it
in your web site's bin folder?
Egghead
 
S

Shapper

Hello,

Can you give me a simple example of a file which has a class that
includes 2 functions to be used by all pages in my web site:

Function Fill_Content(Dim page As String)
End Function

Sub Build_Menu
End Sub

And how would I call this class in a page code?

Thanks,
Miguel
 
E

Egghead

I think I will do it in my way :)

1. go to file menu, add a new project ( vb.net or C#), choose ClassLibrary
2. at the solution explorer, left click at the ClassLibrary project,
properties, configuration properties, change the output path to your
website's bin folder
3. go to the ClassLibrary project, you should c the class, view code, add
the function,sub, and a constructor inside the class, build the
ClassLibrary.
4. go to your web site project, add the reference, browse to your website's
bin folder. You should c the dll file now. add that.
5. in your page's VB.net/C# code, you should be able to do the following,
or use the object tag in the asp page (I never try it)
dim myclass as ClassLibrary
myclass.whatever(whatever)
or
ClassLibrary myclass = new ClassLibrary();
myclass.whatever(whatever)

If you want to step into the classlibrary for debugging, go to enable
unmanaged debugging and generate debugging information.

Egghead
 
S

Shapper

Hi,

Could you please post the class code generated by Visual Studio or just
send it to me by email?

I am using Web Matrix. I consider it simpler to use.

Thanks,
Miguel
 
E

Egghead

oh, I am sorry.

I do not use web matrix. In fact, I have no idea how to add any third-party
stuff in SharpDevelop or web matrix project at all. That is why I use
VS.net.

Sorry :$

Egghead
 
T

tshad

Shapper said:
Hi,

Could you please post the class code generated by Visual Studio or just
send it to me by email?

I am using Web Matrix. I consider it simpler to use.

I use DW, myself. I don't like VS2003,either. I am waiting for VS2005 -
which I hear is pretty good.

What I do is create a file (.vb), compile it and move it to my bin file.

Here is a simple one I use:

*******************************************************************
imports System

NameSpace MyFunctions

Public Class BitHandling

'*----------------------------------------------------------*
'* Name : BitSet *
'*----------------------------------------------------------*
'* Purpose : Sets a given Bit in Number *
'*----------------------------------------------------------*
Public Shared Function BitSet(Number As Integer, _
ByVal Bit As Integer) As Long
If Bit = 31 Then
Number = &H80000000 Or Number
Else
Number = (2 ^ Bit) Or Number
End If
BitSet = Number
End Function

'*----------------------------------------------------------*
'* Name : BitClear *
'*----------------------------------------------------------*
'* Purpose : Clears a given Bit in Number *
'*----------------------------------------------------------*
Public Shared Function BitClear(Number As Integer, _
ByVal Bit As Integer) As Long
If Bit = 31 Then
Number = &H7FFFFFFF And Number
Else
Number = ((2 ^ Bit) Xor &HFFFFFFFF) And Number
End If

BitClear = Number
End Function

'*----------------------------------------------------------*
'* Name : BitIsSet *
'*----------------------------------------------------------*
'* Purpose : Test if bit 0 to bit 31 is set *
'*----------------------------------------------------------*
Public Shared Function BitIsSet(ByVal Number As Integer, _
ByVal Bit As Integer) As Boolean
BitIsSet = False

If Bit = 31 Then
If Number And &H80000000 Then BitIsSet = True
Else
If Number And (2 ^ Bit) Then BitIsSet = True
End If
End Function

End Class

End Namespace
*****************************************************************************

I then have a batch file - makeBitHandling.bat
****************************************
vbc /t:library bitHandling.vb /r:system.dll
copy bitHandling.dll bin\*.*
****************************************

In my .aspx page I do the following:

<%@ Import Namespace="MyFunctions" %>

I now can call these routines as:

dim id as integer
id = BitHandling.BitSet(id,4)

You can create many classes in different files and use the same namespace
and you they will all be handled as one. For example, I have another class
that handles my emails using the same namespace. It looks essentially like:

******************************************************************
Imports System
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.SessionState
Imports System.Web.Mail
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.HttpCookie
Imports System.Web.HttpCookieCollection
Imports System.Web.HttpResponse
Imports System.Web.HttpRequest
imports System.Web.HttpContext
Imports System.Web.HttpApplication
Imports System.Web.HttpApplicationState
Imports Microsoft.VisualBasic

NameSpace MyFunctions

Public Class Email

Public Shared sub sendEmail ( body as string, emailType as string,
emailTitle as String)
...
end Class
end NameSpace
****************************************************************

I only have to use one import (<%@ Import Namespace="MyFunctions" %>) for
both classes.

Hope that helps,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top