This article describes how to use a compiled DLL as a code-behind file in place of a .aspx.vb file, although the same principle can also be used for other languages.
The main advantage of doing this and the reason for me figuring it out is that it makes it possible to distribute code as a .dll without giving recipients access to the source code.
This article is written specifically for Visual Web Developer 2005 Express Edition.
Using the code
- Create the page as normal with an aspx.vb code-behind file.
- In the code-behind file, make the Sub declarations Public instead of Protected. Otherwise, you will not be able to call them in the .dll file from your page.
- For the compiler to recognise them as members of their class, you will need to declare all references to controls using:
Protected WithEvents controlName As System.Web.UI.WebControls.ControlType
You will receive an error message like the one shown below in the Error List window - just ignore it:
'uxSubmit' is already declared as 'Protected Dim WithEvents uxSubmit As System.Web.UI.WebControls.Button' in this class.
- You will also need to declare the Class as Public instead of Partial.
- If you don't already have it, download and install the .NET Framework 2.0 Software Development Kit (SDK) from the Microsoft website.
- From the Start menu, open the SDK command prompt.
- Navigate to the directory containing your code-behind file.
- Enter the following command to compile your code-behind file:
vbc /t:library /r:system.dll,system.web.dll default.aspx.vb
- The .dll file will have been created in the same directory as your code-behind file. Now you need to add this to your web application's bin directory. Go back to Visual Web Developer and add a reference to the .dll file you just created:
Website > Add Reference > Browse
The .dll file will be placed automatically in your project's Bin directory.
- The last thing you will need to do is delete the reference to the CodeFile in your .aspx page's Page header tag.
- You can now delete the .aspx.vb code-behind file if you wish - try it in the example I've provided.
If you have any comments or can suggest any improvements, please get in touch.
2007-08-13 1028: Updated introduction, added conclusion.
2007-08-12 1852: Initial draft.