![]() |
Once in a while, something happens in the computer industry happens that simply cannot be ignored and when it is created by the biggest software company in the world, everyone has to sit up and take notice. The .NET (pronounced DOT NET) initiative grew out of work being done in the Visual J++ department which had created a set of libraries called WFC (Windows Foundation Classes) allowing Java applications to be compiled for the Windows (and only the Windows) platform. This got up the noses of the creators of Java and so it fizzled out. Then there came C# (pronounced C Sharp) which is a hybrid language based on VB, Java, C and C++ and is a wholly Microsoft created product built to run on the .NET platform created from the WFC. This wasn't however just a single technology but also encompassed server and web technology as well and so ASP became ASP.Net. Why should you change? Because it provides the programmer with a tools that make ASP.Net much easier to use than previous versions (for example the datagrid which does away with complicated HTML to display data in a table from a database in a single line of code but more on that later).
For the purposes of these tutorials, I have decided to use C# as the language but you could equally use any other that I have covered in previous tutorials such as VB, J# or JScript. You would be wise to first read my HTML tutorials and my C# tutorials before atempting to follow these although I have tried to make it as simple as possible for beginners.
Now I know you're itching to get coding but there are a few things that you need to setup before we can go any further. First of all you need to be running a web server of some kind because ASP.Net is a Web based technology. Users of Windows 2000 and XP Pro are lucky because they already have a web server available to them called IIS (Internet nformation Services) which can be installed from the Add/Remove programs utility in Control Panel. Windows 98 users also have a Personal Web Server (PWS) available which can again be installed in the usual way. Windows XP Home users however have no such luxury. A web server wasn't shipped with XP Home and PWS won't install. There are various ways of getting the Windows 2000 version of IIS to run on XP Home but this is not recommended. However, all is not lost as MS have unwittingly provided a web server that will run on XP Home and it is call Cassini which can be downloaded here. Run the exe and install it to c:\cassini which is the default. You need to set all the files created in c:\cassini to read/write (select all and select properties from the right-click menu) then open build.bat in notepad and add the following line:-
path=c:\windows\microsoft.net\framework\v1.1.4322\
or whatever it is on your machine then save it. Run the build.bat file and it should compile. Then create a batch file called run.bat in the same directory which should contain:-
CassiniWebServer c:\inetpub\wwwroot 80 /
and then create a directory c:\inetpub\wwwroot which will contain all your files. To test if it is working, run the run.bat file which should run Cassini - the dosbox can be closed and the Cassini screen can be minimized. Create a file called default.htm in the wwwroot directory and put the following in it:-
<HTML> <TITLE>Web Server Page</TITLE> <BODY> Web Server page </HTML> </BODY>
Start your web browser and use the following address:-
and the above HTML page should display. If it doesn't or you get an error then you've missed a step somewhere.
Windows XP Pro, 2000 and 98 users can rejoin us at this point. Now to some coding. Our first ASP.Net program:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
Hello World in HTML<BR>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
// This is the usual Hello World! program
Response.Write("Hello World in ASP.Net<BR>");
}
</script>
Doesn't look to bad does it? Compare this with a C# program that does the same thing:-
using System;
class hello
{
// This is the usual Hello World! program
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
There are a few gotchas to iron out here. Note that in ASP.Net we import libraries of code (which all the other languages use) rather than using in compiled C# which is a little odd (why use using at all?). Also not that main() is not used in ASP (again, why not?) and the program is run (runat) on the server not on the users machine (although in our case the server is being run locally). Response.Write will write to the browser window which can be thought of as a page to be written to. I have shown that we could use HTML to achieve the same thing (which is why the nessage is displayed twice) but this won't always be the case.
Now a word about the use of the programming language C#. C# can be compiled as a standalone exe. It can also be used as a scripting language for ASP.Net (interpreted). This can be confusing because in the past the various languages have been differentiated by different names as below:-
Compiled |
ASP |
| Visual Basic | VBScript |
| Not Applicable | Jscript |
The various languages now available look like this:-
Compiled |
ASP |
| Visual Basic | VB |
| C# | C# |
| Jscript | Jscript |
| J# | J# |
In effect, all the various languages can now be used as both compiled and ASP.Net applicatons and are effectively called the same thing. Hopefully, that is all a bit clearer now.
As this is running in a web browser, the program doesn't produce a GUI window and there is no need to compile it. Now we'll go through the code a line at a time. Bear with me, it is relevent and will help you understand what is to follow when we get to the more interesting stuff.
<%@ Import namespace="System" %>
This is telling the compiler to include the system library which it needs to compile a console program. It is important that all the percentage signs and sharp brackets are included as it will be interpreted as text to be displayed rather than as a command. There then follows some standard HTML which you should be familiar with. Now we get:-
<script language="c#" runat="server">
This is telling the server that it should be interpreting C# code and to run it on the server.
private void Page_Load(object sender, System.EventArgs e)
{
This is the beginning of a C# function which will be automatically run when the page loads.
// This is the usual Hello World! program
This is a comment as found in standard C#. It doesn't need to have a semi-colon or anything on the end as the compiler will read right up till the end of the line automatically.
Response.Write("Hello World in ASP.Net<BR>");
}
</script>
The familiar main function is not used in ASP.Net strangely. The text inside the Response.Write statement will be interpreted as HTML so the <BR> function prints a newline as in standard HTML. Note that as this is a C# command, it must be terminated by a semi-colon. The script is then closed. Save the file as hello.aspx and run from the browser with the address:-
http://localhost/hello.aspx
And that's all there is to it. Well, alright there is much work still to be done but I'll take you gently through it all until we are ready to read and write databases.