Click here to Skip to main content
15,891,184 members
Articles / Web Development / ASP.NET
Article

How to Use a DLL as a Code Behind File

Rate me:
Please Sign up or sign in to vote.
2.23/5 (9 votes)
11 Aug 20072 min read 63.8K   1.1K   24   9
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


Written By
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Shawinder Sekhon18-Jul-12 10:40
Shawinder Sekhon18-Jul-12 10:40 
QuestionHow to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
sridhargcet15-Aug-07 19:46
sridhargcet15-Aug-07 19:46 
AnswerRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
chris_wanaka16-Aug-07 11:15
chris_wanaka16-Aug-07 11:15 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
sridhargcet16-Aug-07 18:56
sridhargcet16-Aug-07 18:56 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
chris_wanaka21-Aug-07 13:09
chris_wanaka21-Aug-07 13:09 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
hassanhaleem12-Jun-11 21:51
hassanhaleem12-Jun-11 21:51 
GeneralRe: How to compile and use a .aspx.cs code-behind file as a .dll code-behind file. Pin
sandsip13-Jul-13 20:32
sandsip13-Jul-13 20:32 
GeneralCreating DLL's Pin
Fred_Smith12-Aug-07 23:07
Fred_Smith12-Aug-07 23:07 
GeneralRe: Creating DLL's Pin
chris_wanaka13-Aug-07 15:16
chris_wanaka13-Aug-07 15:16 

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

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