How to add code behind file to existing page

If you have an existing page that uses code inline and want to associate a code file with it, do the following:

1. Assuming that name of the page is Foo.aspx, create a new code file named Foo.aspx.vb in the same directory.

2. Change page directive to

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Foo.aspx.vb" Inherits="Foo" %>

3. Close and reopen the solution or Web site.

The code file should now correctly show up in the Solution Explorer as a subnode of the page file.

Comments (2)
  1. Rick Scott says:

    Excellent. I had figured this out on my own. Originally, when creating a website, I decided to give the same-page server code thing a try, but found it to be far too cluttered for any decently-sized amount of code.

    At first, I was copying the entire source of the page, closing and deleting the page and then making a new one with the same name (and a Code Behind file) and pasting the code back in. That method actually proved to be a bit easier then typing in code file directives and whatnot 😉

    Another good tip is how to use the master page to write custom functions and properties for consumption by the "slave" pages:

    <%@ MasterType  virtualPath="~/MasterPage.master"%>

    Why this isn’t included by default on new slave-pages is a mystery…

  2. 1. Rename file from .htm to .aspx in Solution Explorer 2. On top pf the page add &amp;lt;%@ Page Language=&quot;VB&quot;…

Comments are closed.

Skip to main content