Your First Crappy Little Web Site

First lets create a website. Open up Visual Studio 2008 and go to File > New Web Site > Select ASP.NET Web Site ensure your language is selected as C# then click OK.

This should start you off with a skeleton website  which contains an App_Data folder, a Default.aspx, Default.aspx.cs and a web.config file.

Default.aspx contains all of the HTML Markup for the page and Default.asp.cs contains all of the C# code.

Click on default.aspx then switch to the design view of the page. Then drag a label control to the page and a calendar control.

Right click on the label control choose properties and change the ID to LabelWelcome and then right click the calendar choose properties and changed the ID to CalendarToday.

If you switch to source view your Code should look like this:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="LabelWelcome" runat="server" Text="Label"></asp:Label>
        <asp:Calendar ID="CalendarToday" runat="server"></asp:Calendar>
    </div>
    </form>
</body>
</html>

In your solution explorer (the panel on the right hand side) select the default.aspx.cs file. It will look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

First things first well clear stuff up and document our code. First we’ll remove the unused Using declarations at the top. The only one we currently need is using System. So the rest can go:

using System;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Next we will add a copyright notice to the top of our file:

// <copyright file="Default.aspx.cs" company="MartinBeeby">
//      Copyright (c) 2008 Martin Beeby
// </copyright>
// <author>Mbee</author>
using System;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Next we will briefly document our page to explain to future developers what the page is all about:

// <copyright file="Default.aspx.cs" company="MartinBeeby">
//      Copyright (c) 2008 Martin Beeby
// </copyright>
// <author>Mbee</author>
using System;

/// <summary>
/// The default page is the first page I have 
/// ever written and is the first page on the website
/// </summary>
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Next we are going to add a documentation header to the page load event that will explain what is going to happen when the page loads:

// <copyright file="Default.aspx.cs" company="MartinBeeby">
//      Copyright (c) 2008 Martin Beeby
// </copyright>
// <author>Mbee</author>
using System;

/// <summary>
/// The default page is the first page I have 
/// ever written and is the first page on the website
/// </summary>
public partial class _Default : System.Web.UI.Page 
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// Will populate the labelWelcome with some text 
    /// and set the calendar to todays date
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Now we can finally write some code. As my documentation suggests we will set the text of the Label control to “Hello and welcome to my crappy little website” and set the calendar to today's date.

// <copyright file="Default.aspx.cs" company="MartinBeeby">
//      Copyright (c) 2008 Martin Beeby
// </copyright>
// <author>Mbee</author>
using System;

/// <summary>
/// The default page is the first page I have 
/// ever written and is the first page on the website
/// </summary>
public partial class _Default : System.Web.UI.Page 
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// Will populate the labelWelcome with some text 
    /// and set the calendar to todays date
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        LabelWelcome.Text = "Hello and welcome to my crappy little website";
        CalendarToday.SelectedDate = DateTime.Now;
    }
}

Now save the files and press F5, a box may pop up asking you if you want to modify the web.config file. Click ok, after a few seconds a browser will load which contains a particularly crappy website.

image