Tonight’s post is going to be short. I wanted to do a very basic overview of the different components of a web forms page.
Each page contains three components:
- Markup (aspx page) – This contains all of your markup and server controls.
- Codebehind (aspx.cs) – This contains all of the logic you are going to perform server-side. For example, if the user selects a button on the form, the page is posted back to the server and the button’s click event is handled server-side and sent back to the client as a response.
- Designer file (aspx.designer.cs) – This is auto-generated by .NET and contains all of the code to generate the controls on the page. Do not make changes to this file.
On an aspx page, the first line is the page directive and, when a new page is created, looks similar to below:
Let’s break this down.
The first thing we see is the code language the page is using. In this case, it is “C#”. If this was a VB.NET application, you would see “VB”.
The next thing is AutoEventWireup. If it is set to true then page events are auto-wired.
Next, we see the CodeBehind attribute, which tells the page which code file contains codebehind.
Lastly, we see the Inherits attribute, which tells the page which class it inherits from. You cannot inherit from more than one class. .NET does not support multiple inheritance.
There are several more attributes you can provide in the page directive, and I invite you to look at this MSDN article for more information.
The codebehind file is just like any other class, but it inherits from System.Web.UI.Page.
And the designer file, which is auto-generated by .NET looks like this:
Notice the comments at the top of this class: “Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.”
What would happen to the designer file if you added an asp label to the page? If you have visual studio already installed on your computer, try it out. Otherwise, download the express edition, create a new web forms application, add some controls, and see what happens.
In my next post, I will break down the aspx page more and cover some basic server controls.