Web Forms Basics: The Three Components of an ASPX Page

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.

Markup

On an aspx page, the first line is the page directive and, when a new page is created, looks similar to below:

image

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.

Codebehind

The codebehind file is just like any other class, but it inherits from System.Web.UI.Page.

image

Designer

And the designer file, which is auto-generated by .NET looks like this:

image

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s