ASP.NET/Your First Page

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Your First ASP.NET Page[edit | edit source]

No programming book is complete without a Hello World example as the first project/example.

In ASP.NET creating a quick Hello World example is painless and easy. At the same time we are going to explore the structure of a basic ASP.NET page.

Let's look at the basic structure of an ASP.NET page. Doing so will give us a chance to introduce several important aspects relevant to coding ASP.NET pages.

VB.NET

<%@ Page Language="VB" %>
<html>
  <head>
    <title></title>
    <script runat="server">
      ' ASP.NET page code goes here
      Sub Page_Load()
        'On Page Load Run this code
      End Sub
    </script>
  </head>
  <body>
    <form runat="server">
      <!-- ASP.NET controls go here -->
    </form> 
  </body>
</html>

C#

<%@ Page Language="C#" debug="true" trace="false"%>
<html>
  <head>
    <title></title>
    <script runat="server">
      // ASP.NET page code goes here
      void Page_Load() {
        //On Page Load Run this Code
      }
    </script>
  </head>
  <body>    
    <form runat="server">
      <!-- ASP.NET controls go here -->
    </form> 
  </body>
</html>

As you can see, an ASP.NET page has a lot in common with a regular HTML-based page. Now in order to make our Hello World example we just need to add one line of code to the above examples.

VB.Net

      Sub Page_Load()
        'On Page Load Run this code
        Response.Write ("Hello World")
      End Sub

C#

      void Page_Load() {
        //On Page Load Run this Code
        Response.Write("Hello World");
      }

Copy the original code into a new file with the extension aspx. Then make the above modification. When the file is run, assuming it is being hosted on an ASP.NET compatible web server, you will see the words Hello World on your web browser when you access the page.