Sub Routine for whole site

P

pvong

I'm a newbie. I'm using VS2008 & VB.net
I have a simple site and each page uses the same sub-routine. I copy and
paste to each page and that's no big deal but that can get tiresome. I was
wondering if it's possible to create a MASTER sub routine where all the
pages can call it and perform it's task for that page. If so, where would I
create something like that in VS2008?

Thanks!
Phil
 
J

Juan T. Llibre

re:
!> I was wondering if it's possible to create a MASTER sub routine
!> where all the pages can call it and perform it's task for that page.

Wonder no more.

There's 2 solutions to that problem :

1. Create a helper class in your application's App_Code directory.
( anyName.cs or anyName.vb )

You can now call your master routine from any page.

2. Create a helper class ( anyName.cs or anyName.vb ), compile the class
to an assembly, and place the assembly in the /bin directory of your application.

vbc /t:library /out:anyName.dll anyName.vb

anyName.dll will be created/compiled in the current directory.

Move the assembly to the /bin directory of your application and fire away.
If you want to have VS.NET use it, reference it in your project.

If you want to use C#, then the command line would be :

csc /t:library /out:anyName.dll anyName.cs

If you need to import .Net classes, include them in your command line:

vbc /t:library /r:system.dll /r:system.web.dll /out:anyName.dll anyName.vb
or
csc /t:library /r:system.data.dll /r:system.web.dll /out:anyName.dll anyName.cs

After that, you can import the namespace into any page with :

<%@ Import Namespace="YourClassname" %>
 
P

pvong

Jaun, thanks for replying but I'm still a little confused. Let me break it
down a little more. I'm only programming in VB.net for and asp.net page.

1)------This is my sub routine------
Sub CkKeyID()
Dim KeyID As String = Request.QueryString("KeyID").ToString
If Not Session("ClientID").ToString = KeyID Then
Server.Transfer("/KeyIdError.aspx")
End If
End Sub
------------------
As you can see, all it's doing is checking to see if my sessionID is = to
KeyID

2) I created a KeySub.vb in App_Code and I pasted this code in. When I do
that, I get "Not Declared" for request, session and server. What should I
put in her to replicated this sub routine?

3)I pretended the code works for KeySub.vb in App_Code and I imported the
class at the top of the page. Once I do that, how do I call the sub
routine? I usually type just the sub routine name it works but when I type
CkKeyID(), but that did not work. I tried KeyIdSub.Equals("CkKeyID") but I
get the error msg "Reference to a non-shared member requires an object
reference."



Basically, what do I have to put in the KeyIdSub.vb to re-create my sub
routine and how do I call it from an aspx page?



Thanks for helping.

Phil
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top