Solved

how can I share functions between multiple code behind pages ?

Posted on 2004-09-13
12
486 Views
Last Modified: 2012-08-13
Hi There,

I am wondering how I can share common functions between code behind pages? I have a few codebehind pages that use common sub routines / functions and I want to make the available to all codebehind pages but I only have to change the code in one place.

Sean
0
Comment
Question by:bullrout
12 Comments
 
LVL 6

Expert Comment

by:vardium
ID: 12042342
add a public class to project.
or a module.
like


Public Class clsCommon
    Public Shared Function myfunc() As String
        'bla bla bla
    End Function
End Class
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12042348
Hi,

you can create a common class ( create new item in your project of type class ) and you can use it to define functions/methods in it that will be called from the other classes in the project


Regards,
B..M
0
 
LVL 1

Accepted Solution

by:
vinayakrishna earned 100 total points
ID: 12042406
You can add a Class file with all the commonly used functions in it,to your solution.

Then you can create an object of this class and access all functions defined in it in all your .cs files.Like this :

ProductController prodcontroller = new ProductController();

prodcontroller.Function1();
0
 
LVL 5

Author Comment

by:bullrout
ID: 12042904
Hi There,

So if I have a .vb file in my dir called common and then I can call functions inside that class file by creating an instance of that class inside another codebehind without actually having a reference to it (such as a file path)?

I tried to do something like below, where I have listbox that is populated from a sql server db, I'm not sure how to declare the class inside the code behind page in order to use it?

Sorry for all of the questions, I am making the transition from asp to asp .net and I not used to oop yet.

Sean

Public Class CommonFunctions

 Public Shared Function myfunc() As String
        'access the db here
    End Function

End class

!--- in a different code behind page

Public Class DoSomething

        lst1.DataSource = commonfunctions.myfunc()
        lst1.DataValueField = "fieldname"
        lst1.DataTextField = "fieldname"
        lst1.DataBind()
        lst1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("pick one","0"))
        lst1.SelectedIndex = 0      

End class


0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12043055
you may need a new in there like

lst1.DataSource = New commonfunctions.myfunc()

but other than that looks good

Regards,

Aeros
0
 
LVL 4

Assisted Solution

by:itcnbwise
itcnbwise earned 400 total points
ID: 12044845
If you work with the code (like me) and compile it without using Visual Studio, here's what you'd do:

Namespace commonIncludes

  Public Class CommonFunctions

    Public Shared Function myfunc() As String
        'access the db here
    End Function

    'mo functions, mo functions, mo functions ...

End class

End Namespace

**************

Then, in any page you want to use that function, import it:

Imports System ...
Imports commonIncludes

Public Class thisIsMyPage: Inherits System.Web.UI.Page

  Public myIncludes As New CommonFunctions

  Sub Page_Load(sender as Object, e as EventArgs)
    Dim thisVar As String = myIncludes.myfunc()
  End Sub

End Class
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:itcnbwise
ID: 12044857
FYI -  of course, that work's with Visual Studio as well, I just wanted to show you all the code needed.  VS tends to hide the more complicated stuff so you don't always understand what it's doing.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12050052
Hi itcnbwise,

I have not compiled any of my code, I'm just referencing a codebehind page via a path, is this the wrong way to do it?

Sean
0
 
LVL 4

Expert Comment

by:itcnbwise
ID: 12050077
Well, there's no wrong way to do it, but there is a better way :)  Pre-compiled pages are stored as DLLs in the /bin/ directory of your web server.  If your page isn't pre-compiled (i.e., the codebehind is just a VB file) then the webserver has to compile it on-the-fly the first time it's requested after a source code change.  Not a big deal, unless you're the first user to request the file :)

In short, you don't need to compile your pages, but there is a performance gain if you do.   Regardless, you should still be able to access an included file.  As I said, I use the free vbc.exe file that comes with .NET to compile all my code on the command line, but you can use Visual Studio if you want to give it a try.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12050095
Hi itcnbwise,

do you have any commandline syntax examples for this? (vbc.exe) and can this be done without a visual studio project file?

Sean
0
 
LVL 4

Expert Comment

by:itcnbwise
ID: 12050158
Sure, here's how you'd compile a page on the command line:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc /nologo /t:library /out:..\bin\YOURFILE.dll /r:System.dll /r:System.Web.dll /r:System.Data.dll /r:System.Xml.dll /r:..\bin\YOURINCLUDESFILE.dll  YOURFILE.aspx.vb

Notice you have to include any libraries you are using into the compile.  You would use a command like this to first compile YOURINCLUDESFILE.dll , then tun this to compile YOURFILE.aspx.vb.

And yes, this can definitely be done with VS I just don't know the steps, because I never use it.  But I have an open source project which explains all this, has the command line compile options in BAT files, and it uses an include file.  Go to http://forum.itcn.com/ - it's a free, open source project I created for newcomers to learn ASP.NET.  Take a look at the source code for more ideas.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12061261
Hi All,

Thanks for all the help, I have made my application into a project and this seems to be making my life easier.

Sean
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now