Pages

Hello ASP

Here is a simple ASP page developed with Visual Web Deveoper.using C# as programming language.

I won't spend time on the (long and boring) time required to install on my machine the developing environment, because I don't not know what to say more than it was a long and boring process.

In any case. I have created a new project for an empty Web ASP-NET application. Then in the application I created a new Web Form, and I kept the proposed name (even if I don't expecially like it), Default.aspx. In the process, another file happened to be created, Default.aspx.cs, that looks quite clearly it is a C# file strongly connected with the ASP page.

Here is the source file for the ASP page, slightly change to provide a HTML page title, and some text in the body:

<%@ 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>Hello Simple</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Hello from Simple. Current time is <asp:Label id="myTime" runat="server"/>
</div>
</form>
</body>
</html>

In the first line you see a Page directive where it is specified, among other stuff, that we are using C# as undelying language and the name of the file where to look for the C# code.

In the body we can see that a put an asp:Label, that is nothing more than a label, but it has a funny property runat set to server, obviously meaning that is something that has to run on the server. We keep note also of the other property, ID, that looks like a way of providing access to this element.

Actually, if we have a look to the C-Sharp code, we understand more of the asp:Label, since I put there a line referring to it:

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)
{
myTime.Text = DateTime.Now.ToString();
}
}

All this code was automatically generated but the body of the Page_Load() method. Even not knowing a thing of C# and ASP.NET, it looks that here we are dealing with a class containing code related with a web page, that is picked up as default and that its proposed method is called when the page is loaded, passing an "Object" representing the sender and an "EventArgs" that should be the associated event that caused the method to be called.

What we are doing here is simply setting the Text property of the myTime label, defined in the ASP page, using the current system time converted to a string.

When I executed the project I saw an HTML page having this code:

<!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><title>
Hello Simple
</title></head>
<body>
<form method="post" action="Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJ(...)Grg==" />
</div>

<div>
Hello from Simple. Current time is <span id="myTime">13/02/11 22:59:16</span>

</div>
</form>
</body>
</html>

Bad formatting, some funny stuff, but, besides this, the most interesting line is in the body, where our asp:Label has been converted in good HTML code.

No comments:

Post a Comment