Sub Routine for whole site

  • Thread starter Thread starter pvong
  • Start date Start date
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
 
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" %>





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
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
 
Back
Top