Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Use a DLL as a Code Behind File

0.00/5 (No votes)
11 Aug 2007 1  
How to compile and use a .aspx.vb code-behind file as a .dll code-behind file.

Introduction

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

  1. Create the page as normal with an aspx.vb code-behind file.
  2. 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.
  3. 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.

  4. You will also need to declare the Class as Public instead of Partial.
  5. If you don't already have it, download and install the .NET Framework 2.0 Software Development Kit (SDK) from the Microsoft website.
  6. From the Start menu, open the SDK command prompt.
  7. Navigate to the directory containing your code-behind file.
  8. Enter the following command to compile your code-behind file:
    vbc /t:library /r:system.dll,system.web.dll default.aspx.vb
  9. 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.
  10. The last thing you will need to do is delete the reference to the CodeFile in your .aspx page's Page header tag.
  11. You can now delete the .aspx.vb code-behind file if you wish - try it in the example I've provided.

Conclusion

If you have any comments or can suggest any improvements, please get in touch.

History

2007-08-13 1028: Updated introduction, added conclusion.

2007-08-12 1852: Initial draft.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here