Solved

how can I share functions between multiple code behind pages ?

Posted on 2004-09-13
12
487 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:Volkan Vardar
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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