Tutorials » AspNet tutorials »

Code Behind Files

This article explains the concept of code behind files in VS.NET

In the solution explorer, you can double click and open any aspx file. Also, in one of the previous chapters you learned you can click on "Show All Files" icon to view the related hidden files.

You may have noticed that when VS.NET creates an aspx file, it creates 2 other files too. One with the extension .resx and one with the extension .vb (or .cs if you choose C#).

When VS.NET created the default file 'WebForm1.aspx', it created the following files also:

  • WebForm1.aspx.resx
  • WebForm1.aspx.vb

    (When a file name has more than one dot (.) in it, only the last one will be considered for the extension of the file. So, the full file name is WebForm1.aspx.resx and the extension of the file is .resx.)

    How to create a new ASPX file in Visual Studio?

    Right click on the project name in the solution explorer. Select the menu "Add" > "Add Web Form". This will prompt you to select a file name. Enter the name "MyProfile.aspx".

    Now VS.NET will create 3 files for you:

  • MyProfile.aspx
  • MyProfile.aspx.resx
  • MyProfile.aspx.vb

    Resource File

    The .resx file is know as the resource file. You may not use this file much until you need to work on some advanced features.

    The resource file is used to store various resource objects related to the aspx file. You can store strings or other objects as key-value pairs in this file. You will learn more about the resource files in later chapters.

    What is Code Behind Files ?

    Now let us come to the key of this chapter - The code behind file. In the above example, MyProfile.aspx.vb is the code behind file. As the name indicates, it is the file which holds the code for the aspx page.

    It is a nice idea by the ASP.NET team - to separate a web page into different files. The HTML tags and ASP.NET controls for the UI elements goes to the aspx page. The server side code (VB or C# or any other language) goes to the code behind files. This way, the files looks very clean and neat.

    With this approach, you don't need to mix the html tags and server side tags (<% %>) in the aspx pages.

    In the previous chapter you learned how the aspx knows it's code behind file - by using the @Page directive in the top of the page.

    How to open the code behind file for the aspx page ?

    You can right click on the aspx file name in the solution explorer and select 'View Code'. This will open the code behind file associated with the aspx page.

    Alternatively, you can press "F7" from the aspx page to open the code behind file.

    How to open the aspx file from the code behind file ?

    When you have the code behind file opened in the editor, you can right click on the file name in solution explorer and select 'View Designer' to view the aspx page.

    Alternatively, you can press 'Shift + F7" from the code behind file to open the corresponding aspx file.

    And within the aspx page, you can switch between "Design" mode and "HTML" mode by using the links in the bottom left corner as shown in the top of this chapter.

    How to write code in the code behind file ?

    Open the aspx file and select the "Design Mode". Simply double click on the designer form. This will create an "Page_Load" event handler in the code behind file and open the code behind file automatically.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    The Page_Load event will be fired when the page is loaded. Let us try a small example. Write the following code inside the Page_Load event:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Response.Write ("Hey, my page is loaded ...")
    End Sub

    Now compile and run the web page by pressing "Ctrl + F5". You will see the line "Hey, my page is loaded ..." in the web page.

    Note that, you will not use Response.Write much in your asp.net programming. Instead, you will use ASP.NET web cotnrols to display data dyamically. You will learn more about the web controls in the later chapters.

  • Next Chapter: Introduction to ASP.NET Web Controls
    Previous Chapter: Analyze the default web page created by Visual Studio
    More Chapters: ASP.NET Tutorials
    More Tutorials: Tutorial Index

    Top Contributors
      Last 7 Daysmore...

      Awards & Gifts

      Online Members

      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India