I have a few questions about putting code into separate files.
First, can someone define codebehind? wierd. I have a CS degree, but
I have never heard this term before.
Also, what is the most effective way of storing classes and functions in a separate,
reuseable file?
I tried an ascx file, but I came upon a problem:
i have my index.aspx page, filea.ascx, and fileb.ascx. I'm basically using
filea, and fileb to hold functions. now i need a function in filea to use
a function in fileb. how do i do that? i can't seem to be able to do it, since
I need to create an instance of the usercontrol before i can use it, but
I can't create a user control inside of an ascx file.A codebehind file is a file that contains all the code (methods, event handlers, variables, etc.) for a particular UI page (.aspx or .ascx). If you've done any windows forms programming you will understand this concept better: each page has its own codebehind file to handle things like button-presses. It's the same for asp.net programming: any time you want something to "happen" on a page in your application, the page simply calls a postback to the server, and usually some code in the codebehind file is executed (like when somebody presses a button on a web page).
This is one of the huge advantages of using asp.net over asp - you can now write a web application the same way you would write a windows application, in whatever language you choose (as long as you have a .net compliant compiler for that language) since now all the processing happens server-side instead of depending on client-script capabilities in each user's browser. Just wire-up the event handlers for buttons and the like in your codebehind file and it's all executed on the server.
If I've thoroughly confused you, go to this page to understand more about the web forms code model:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformscodemodel.asp
If you want to create reusable code that you can call from anywhere in your application, I would suggest creating a separate Class Library project (.dll). Put all of your classes, static methods, and whatever else you want in a file (or multiple files) in the project. You'll just want to make sure they are all declared within the same namespace. Then compile this project. You will end up with a .dll file that now encapsulates all the classes and methods you defined in the project.
In the project where you want all this functionality exposed, just add a reference to the .dll you created. Now all those classes are exposed to your project.
Hope this helps!
Michael Ferguson
Student Ambassador to Microsoft
thank you, mpherg
The stuff about the dll files. can you point me towards any examples?
How do i reference it? is the file compiled?
You reference it something like this...
first i create a vb-source file.
Namespace [Namespacename]
Public Class [Classname]
Public Function myFunction()
' code goes here
End Function
End Class
End Namespace
now we can reference myFunction from aspx-files...
Dim Testing As [Namespacename].[Classname] = New [Namespacename].[Classname]()
' now i call my function in the vb.class
Testing.myFunction()
Hope this makes it more clear.. if not you can try to download the starterkits from
http://www.asp.net/Default.aspx?tabindex=9&tabid=47
do i need to do something else? like compile the code?
i get this error message:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30002: Type 'mynamespace.test' is not defined.
you need to cimpile to get the dll.
visual studio will do that for you...
I opened up my test.vb file with visual studio, but i see no "build" option.
is this doable without visual studio?
There is a menu called "Build". Select "Build Solution" under that menu.
OK, How do I do this WITHOUT having to compile it? My web service doesn't allow for compiled dll's. I need to reference other modules from with my page class but when I add the namespace the page class still cannot find this module. Any ideas?
0 comments:
Post a Comment