Solved

how can I share functions between multiple code behind pages ?

Posted on 2004-09-13
12
484 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

17 Experts available now in Live!

Get 1:1 Help Now