473,770 Members | 6,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code Behind Page

Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file
named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel

Nov 19 '05 #1
7 2243
It would be better if you used either a module or a class to put your common
functions. If you use a class, you can declare your methods as Shared so that
you won't have to create an instance of the class to use them. For example, I
have a class of common data routines called SQLDataRoutines , most of which
are shared. Here is a shared function that returns a SQL connection:

Public Shared Function GetConnection(B yVal connectionStrin g As String)
As SqlConnection
Dim connection As New SqlConnection(c onnectionString )

Try
connection.Open ()
Return connection

Catch ex As Exception
Throw ex
End Try

End Function

So behind any of my WebForms I can call it like this:
dim cnn as SQLConnection
dim strConn as String = "Put your connection string here"
cnn = SQLDataRoutines .GetConnection( strConn)
....the rest of your code

Hope this helps.

"Shapper" wrote:
Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file
named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel

Nov 19 '05 #2
Why not make a vb.net or C# class holds all your common functions and put it
in your web site's bin folder?
Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file
named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel

Nov 19 '05 #3
Hello,

Can you give me a simple example of a file which has a class that
includes 2 functions to be used by all pages in my web site:

Function Fill_Content(Di m page As String)
End Function

Sub Build_Menu
End Sub

And how would I call this class in a page code?

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:
Why not make a vb.net or C# class holds all your common functions and put it
in your web site's bin folder?
Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file
named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel


Nov 19 '05 #4
I think I will do it in my way :)

1. go to file menu, add a new project ( vb.net or C#), choose ClassLibrary
2. at the solution explorer, left click at the ClassLibrary project,
properties, configuration properties, change the output path to your
website's bin folder
3. go to the ClassLibrary project, you should c the class, view code, add
the function,sub, and a constructor inside the class, build the
ClassLibrary.
4. go to your web site project, add the reference, browse to your website's
bin folder. You should c the dll file now. add that.
5. in your page's VB.net/C# code, you should be able to do the following,
or use the object tag in the asp page (I never try it)
dim myclass as ClassLibrary
myclass.whateve r(whatever)
or
ClassLibrary myclass = new ClassLibrary();
myclass.whateve r(whatever)

If you want to step into the classlibrary for debugging, go to enable
unmanaged debugging and generate debugging information.

Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

Can you give me a simple example of a file which has a class that
includes 2 functions to be used by all pages in my web site:

Function Fill_Content(Di m page As String)
End Function

Sub Build_Menu
End Sub

And how would I call this class in a page code?

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:
Why not make a vb.net or C# class holds all your common functions and put it in your web site's bin folder?
Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

When creating an ASP.NET/VB web site I place my code in a code behind
page, i.e., aspx.vb.

Sometimes I have functions which are common to all aspx files.

Is it possible in page1.aspx to use page1.aspx.vb and also a common file named global.aspx.vb which will hold all the functions common to all
aspx files in a web site.

How can this be done?
Should I do it?

Thanks,
Miguel

Nov 19 '05 #5
Hi,

Could you please post the class code generated by Visual Studio or just
send it to me by email?

I am using Web Matrix. I consider it simpler to use.

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:
I think I will do it in my way :)

1. go to file menu, add a new project ( vb.net or C#), choose ClassLibrary
2. at the solution explorer, left click at the ClassLibrary project,
properties, configuration properties, change the output path to your
website's bin folder
3. go to the ClassLibrary project, you should c the class, view code, add
the function,sub, and a constructor inside the class, build the
ClassLibrary.
4. go to your web site project, add the reference, browse to your website's
bin folder. You should c the dll file now. add that.
5. in your page's VB.net/C# code, you should be able to do the following,
or use the object tag in the asp page (I never try it)
dim myclass as ClassLibrary
myclass.whateve r(whatever)
or
ClassLibrary myclass = new ClassLibrary();
myclass.whateve r(whatever)

If you want to step into the classlibrary for debugging, go to enable
unmanaged debugging and generate debugging information.

Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

Can you give me a simple example of a file which has a class that
includes 2 functions to be used by all pages in my web site:

Function Fill_Content(Di m page As String)
End Function

Sub Build_Menu
End Sub

And how would I call this class in a page code?

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:

Why not make a vb.net or C# class holds all your common functions and
put it
in your web site's bin folder?
Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hello,
>
> When creating an ASP.NET/VB web site I place my code in a code behind
> page, i.e., aspx.vb.
>
> Sometimes I have functions which are common to all aspx files.
>
> Is it possible in page1.aspx to use page1.aspx.vb and also a common
file
named global.aspx.vb which will hold all the functions common to all
> aspx files in a web site.
>
> How can this be done?
> Should I do it?
>
> Thanks,
> Miguel
>



Nov 19 '05 #6
oh, I am sorry.

I do not use web matrix. In fact, I have no idea how to add any third-party
stuff in SharpDevelop or web matrix project at all. That is why I use
VS.net.

Sorry :$

Egghead

"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:u1******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

Could you please post the class code generated by Visual Studio or just
send it to me by email?

I am using Web Matrix. I consider it simpler to use.

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:
I think I will do it in my way :)

1. go to file menu, add a new project ( vb.net or C#), choose ClassLibrary 2. at the solution explorer, left click at the ClassLibrary project,
properties, configuration properties, change the output path to your
website's bin folder
3. go to the ClassLibrary project, you should c the class, view code, add the function,sub, and a constructor inside the class, build the
ClassLibrary.
4. go to your web site project, add the reference, browse to your website's bin folder. You should c the dll file now. add that.
5. in your page's VB.net/C# code, you should be able to do the following, or use the object tag in the asp page (I never try it)
dim myclass as ClassLibrary
myclass.whateve r(whatever)
or
ClassLibrary myclass = new ClassLibrary();
myclass.whateve r(whatever)

If you want to step into the classlibrary for debugging, go to enable
unmanaged debugging and generate debugging information.

Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

Can you give me a simple example of a file which has a class that
includes 2 functions to be used by all pages in my web site:

Function Fill_Content(Di m page As String)
End Function

Sub Build_Menu
End Sub

And how would I call this class in a page code?

Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:

> Why not make a vb.net or C# class holds all your common functions and

put it
> in your web site's bin folder?
> Egghead
> "Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
> news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
>

> > Hello,
> >
> > When creating an ASP.NET/VB web site I place my code in a code
behind > > page, i.e., aspx.vb.
> >
> > Sometimes I have functions which are common to all aspx files.
> >
> > Is it possible in page1.aspx to use page1.aspx.vb and also a common
file
> > named global.aspx.vb which will hold all the functions common to

all > > aspx files in a web site.
> >
> > How can this be done?
> > Should I do it?
> >
> > Thanks,
> > Miguel
> >


Nov 19 '05 #7
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:u1******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

Could you please post the class code generated by Visual Studio or just
send it to me by email?

I am using Web Matrix. I consider it simpler to use.
I use DW, myself. I don't like VS2003,either. I am waiting for VS2005 -
which I hear is pretty good.

What I do is create a file (.vb), compile it and move it to my bin file.

Here is a simple one I use:

*************** *************** *************** *************** *******
imports System

NameSpace MyFunctions

Public Class BitHandling

'*----------------------------------------------------------*
'* Name : BitSet *
'*----------------------------------------------------------*
'* Purpose : Sets a given Bit in Number *
'*----------------------------------------------------------*
Public Shared Function BitSet(Number As Integer, _
ByVal Bit As Integer) As Long
If Bit = 31 Then
Number = &H80000000 Or Number
Else
Number = (2 ^ Bit) Or Number
End If
BitSet = Number
End Function

'*----------------------------------------------------------*
'* Name : BitClear *
'*----------------------------------------------------------*
'* Purpose : Clears a given Bit in Number *
'*----------------------------------------------------------*
Public Shared Function BitClear(Number As Integer, _
ByVal Bit As Integer) As Long
If Bit = 31 Then
Number = &H7FFFFFFF And Number
Else
Number = ((2 ^ Bit) Xor &HFFFFFFFF) And Number
End If

BitClear = Number
End Function

'*----------------------------------------------------------*
'* Name : BitIsSet *
'*----------------------------------------------------------*
'* Purpose : Test if bit 0 to bit 31 is set *
'*----------------------------------------------------------*
Public Shared Function BitIsSet(ByVal Number As Integer, _
ByVal Bit As Integer) As Boolean
BitIsSet = False

If Bit = 31 Then
If Number And &H80000000 Then BitIsSet = True
Else
If Number And (2 ^ Bit) Then BitIsSet = True
End If
End Function

End Class

End Namespace
*************** *************** *************** *************** *************** **

I then have a batch file - makeBitHandling .bat
*************** *************** **********
vbc /t:library bitHandling.vb /r:system.dll
copy bitHandling.dll bin\*.*
*************** *************** **********

In my .aspx page I do the following:

<%@ Import Namespace="MyFu nctions" %>

I now can call these routines as:

dim id as integer
id = BitHandling.Bit Set(id,4)

You can create many classes in different files and use the same namespace
and you they will all be handled as one. For example, I have another class
that handles my emails using the same namespace. It looks essentially like:

*************** *************** *************** *************** ******
Imports System
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.Sess ionState
Imports System.Web.Mail
Imports System.Data
Imports System.Data.Sql Client
Imports System.Web.Http Cookie
Imports System.Web.Http CookieCollectio n
Imports System.Web.Http Response
Imports System.Web.Http Request
imports System.Web.Http Context
Imports System.Web.Http Application
Imports System.Web.Http ApplicationStat e
Imports Microsoft.Visua lBasic

NameSpace MyFunctions

Public Class Email

Public Shared sub sendEmail ( body as string, emailType as string,
emailTitle as String)
...
end Class
end NameSpace
*************** *************** *************** *************** ****

I only have to use one import (<%@ Import Namespace="MyFu nctions" %>) for
both classes.

Hope that helps,

Tom
Thanks,
Miguel

"Egghead" <ro************ **@shaw.ca> wrote in message
news:ro******** ******@shaw.ca:
I think I will do it in my way :)

1. go to file menu, add a new project ( vb.net or C#), choose
ClassLibrary
2. at the solution explorer, left click at the ClassLibrary project,
properties, configuration properties, change the output path to your
website's bin folder
3. go to the ClassLibrary project, you should c the class, view code, add
the function,sub, and a constructor inside the class, build the
ClassLibrary.
4. go to your web site project, add the reference, browse to your
website's
bin folder. You should c the dll file now. add that.
5. in your page's VB.net/C# code, you should be able to do the
following,
or use the object tag in the asp page (I never try it)
dim myclass as ClassLibrary
myclass.whateve r(whatever)
or
ClassLibrary myclass = new ClassLibrary();
myclass.whateve r(whatever)

If you want to step into the classlibrary for debugging, go to enable
unmanaged debugging and generate debugging information.

Egghead
"Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
> Hello,
>
> Can you give me a simple example of a file which has a class that
> includes 2 functions to be used by all pages in my web site:
>
> Function Fill_Content(Di m page As String)
> End Function
>
> Sub Build_Menu
> End Sub
>
> And how would I call this class in a page code?
>
> Thanks,
> Miguel
>
> "Egghead" <ro************ **@shaw.ca> wrote in message
> news:ro******** ******@shaw.ca:
>

> > Why not make a vb.net or C# class holds all your common functions and


put it
> > in your web site's bin folder?
> > Egghead
> > "Shapper" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in message
> > news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
> >

> > > Hello,
> > >
> > > When creating an ASP.NET/VB web site I place my code in a code
> > > behind
> > > page, i.e., aspx.vb.
> > >
> > > Sometimes I have functions which are common to all aspx files.
> > >
> > > Is it possible in page1.aspx to use page1.aspx.vb and also a common


file
> > > named global.aspx.vb which will hold all the functions common to
> > > all
> > > aspx files in a web site.
> > >
> > > How can this be done?
> > > Should I do it?
> > >
> > > Thanks,
> > > Miguel
> > >

>

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5360
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script> sections? I can't seem to figure out the syntax for for calling code-behind directly. The method is within the class my page inherits from and is public, but when I try to call it from my page I get this error: CS1520: Class, struct, or...
5
36880
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my code behind. However I am not able to do this at all, I am going about this wrong, I think and need guideance. If this was just straight HTML I would do this: <table> <tr><td>field 1</td><td>value 1 set by code behind</td></tr>
171
7796
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
6
5516
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp; DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; "&amp;Subject=" &amp; DataBinder.Eval(Container.DataItem,"ProductName") ....
17
2714
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
29
3680
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form id="Form1" method="post" runat="server"> <%
7
2886
by: Alan Silver | last post by:
Hello, I am just looking at VWD and seeing what needs doing to take an existing site I've written by hand and importing it into VWD. I've already discovered that I need to rename my code-behind files to match the .aspx file name so VWD will tie the two together, and I'm now wondering what to do about the location of the code-behind files. Is there any set convention for where the code-behind files live? I have been putting them in the...
7
1753
by: Alan Silver | last post by:
Hello, I have installed the 2.0 framework, and am looking at converting some of my 1.1 pages to use partial classes. I don't (yet) have VS2005, so I'm doing this by hand, but am having problems. I have a simple page that I made in the beta2 version of VWD. The code behind looks like... using System;
2
4823
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the properties, events etc. of the user control in the ASPX page in this way (assume that the ASPX page...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.