Tip:
Highlight text to annotate it
X
We're now going to make a simple "Hello World" web application to demonstrate writing, compiling
and running code in ASP.NET. Before we begin I'll go ahead and simplify this document somewhat.
The first step is to add a Label control, which simply holds a piece of text. We specify
its name using the ID attribute, and mark it to be run at the server.
Next, we add a script block somewhere before this element. Since we have now defined a
label control called MyLabel we can access its members by typing its name followed by
a dot. As we can see IntelliSense gives us a list with all its fields and methods. The
member we're looking for is the Text field to set the text to be displayed on the label,
in this case Hello World.
The Hello World program is now complete. To launch it go to the debug menu and select
start. Visual studio will now compile the web application
and run internet explorer so we can view the page.
If we open the page menu and select view source, we can see that all traces of code are gone
and only XHTML remain. This is because Visual Studio has launched a web server in the background,
which actually ran the web application and sent us this XHTML file. This means that our
browser doesn't need to support any scripting at all to be able view the page.
To stop the execution we just close the window.
This method of including the script block inside the ASPX file is actually not the preferred
method. While it works fine for small programs like this it can quickly become difficult
to edit and debug in larger programs. This is why ASP.NET introduced the CodeBehind technique,
which allow us to completely separate the code from the markup.
This is what the code file is there for. Let's take the content of the code block and move
it to the C# file's load function. If we now run the page again we can see that
it gives the same result.