Click here to Skip to main content
Click here to Skip to main content

How to Use a DLL as a Code Behind File

, 11 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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

About the Author

chris_wanaka

New Zealand New Zealand
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberMember 238651118-Jul-12 10:40 
QuestionHow to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmembersridhargcet15-Aug-07 19:46 
AnswerRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmemberchris_wanaka16-Aug-07 11:15 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmembersridhargcet16-Aug-07 18:56 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmemberchris_wanaka21-Aug-07 13:09 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmemberhassanhaleem12-Jun-11 21:51 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pinmembersandsip13-Jul-13 20:32 
GeneralCreating DLL's PinmemberFred_Smith12-Aug-07 23:07 
GeneralRe: Creating DLL's Pinmemberchris_wanaka13-Aug-07 15:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 12 Aug 2007
Article Copyright 2007 by chris_wanaka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid