![]() |
Web Application Fundamentals |
If you have previously done web development, you are probably familiar with the concept of a web site, which is primarily a series of files as web pages grouped in a main parent folder. ASP.NET uses the same approach: An ASP.NET project is one or more files presented as an application. Like any regular web site, an ASP.NET application can include different types of files, that is, files with different extensions intended to serve different purposes. This means that, to create an ASP.NET application, you can start by creating HTML files which are files with .htm or .html extension. You can also use Cascading Style Sheet (CSS) files that would allow you to better format or interpret HTML tags. You can also use script-oriented files that have the .js, .vbs. .pl, etc extensions to complement the other pages.
As you would proceed for a normal web site, to start an ASP.NET application, you can create a folder with a name of your choice. Here is an example of a new folder named Grier Summer Camp:
After creating a folder for your project, you should create a virtual directory. To do this, you can first display the Internet Information Services or the Internet Services Manager window. Then, in the left frame (of the MMC), expand the name of your server (if you are planning to use another server or computer but is not listed, right-click Internet Information Services, click Connect, type the name of the computer and click OK) and optionally expand the Default Web Site node (if you are using MS Windows XP Professional, you must expand Web Site under the computer). Right-click Default Web Site -> New -> Virtual Directory. In the first page of the wizard, you would click Next. In the second page, you must type the name you will use on the browser to access the application you are creating. Here is an example:
In the third page, you can type the full path to the folder you created or you can click Browse to locate and select the folder you had previously created. In the Browse For Folder dialog box, locate the folder you created:
The fourth page allows you to specify how the pages on this site will be accessed: After clicking Next in the fourth page, you can click Finish in the fifth page: A new web site would have been created for your application. |
|
ASP.NET Application Development |
|
ASP.NET Files |
|
After creating the web site, you can add the necessary files to it. You can create text files and save them with the appropriate extension. When saving the files, make sure you store them in the folder you would have created. This would make it possible for the browser to locate them through the virtual directory. ASP.NET own pages are text-based files with the .aspx extension. The content of the file is up to you and it doesn't have to have special code than regular HTML tags. Here is an example of a normal HTML-tagged file: <html> <head> <title>Grier Summer Camp</title> </head> <body> <center><h1>The Grier Summer Camp</h1></center> <p>Located in the heart of Rockville, Maryland, the <b>Grier Summer Camp</b> is an attractive setting aimed at entertaining and supporting youth activities during their summer vacation.</p> <h2>Program Presentation</h2> <p>We feature a state of the art, regularly renovated mansions with world class sport facilities.</p> <p>Coolfront Island, our star house of the season, is a 2-story like chateau featuring nicely finished brown bricks whose sight is tremendously appealing. The estate introduces two large living rooms with exquisite velour furniture. The colonial veranda offers a patio spanning two walls with a lost view down the far-reaching landscape. In this particular setting, besides their usual activities, children will learn special human needs to make their experience unforgettable.</p> <p>Please consult our catalogue and see why the Washington Tribune called us <i>the most attractive summer camp of the area</i>.</p> </body> </html> After creating the file, you should save it with the .aspx extension in a folder of your choice:
After saving the file, to preview it, in the Internet Information Services window, first locate the virtual directory of the project. Then, you can right-click the file and click Browse. Alternatively, you can open the browser and access its Address bar, change the address to http://localhost/ followed by the name of the virtual directory, and followed by the name of the file with its extension:
|
|
ASP.NET Visually |
|
The technique we used above, including creating a folder, a virtual directory, and the necessary file(s) requires that you follow those steps to get the project ready. This is perfectly fine if you have time to create and write all necessary tags required in a web page. To make it a little easier, there are various tools you can use to visually design your ASP.NET web pages. Microsoft Visual Studio .NET is a commercial programming environment that allows you to create an ASP.NET application as a project. To get it, you must purchase it. To create an ASP.NET project using it, you can display the New Project dialog box, select the project group of your choice, select ASP.NET Application and click Open. "ASP.NET Web Matrix is a community-supported, easy-to-use WYSIWYG application development tool for ASP.NET. It can be installed via a quick 1.3 MB download (about 5 minutes using a 56Kb modem). New features include: Access database support, J# support, design time enhancements including improved table editing and user-control rendering, many bug fixes, and much more! Best of all? It's absolutely free!" From http://asp.net/webmatrix". To get Web Matrix, you can download it from http://asp.net/webmatrix. |
|
Using Microsoft Visual Studio .NET |
|
Introduction |
|
Notepad's simplicity is well suited if you are just writing code. In some cases, if you are developing a graphically intensive application, Notepad may present some limitations as everything would have to be typed (manually). To accelerate ASP.NET application development, Microsoft provided complete support in Microsoft Visual Studio .NET. This environment allows you to create an application and have a better view of where items are positioned on the web page. This can be a tremendous help if your application includes "visual" controls.
The lessons on this site assume that you have Microsoft Visual Studio 2005 installed in your computer. To start Microsoft Visual Studio 2005, on the Taskbar, you can click Start (All) Programs -> Microsoft Visual Studio 2005 -> Microsoft Visual Studio 2005
To create an ASP.NET application in Visual Studio .NET, you can display its New Project dialog box. In the dialog box, you can select either Visual Basic Projects, Visual C# Projects, Visual J# Projects. In the Templates section, you can click ASP.NET Web Application. In the Location text box, on the right side of http://localhost/, you can accept the suggested name of the project or you can type the name you desire: ![]() Once you click OK, Visual Studio would create the application's folder, the virtual directory for your application, and many files. |
|
Compiling and Executing a Project |
|
The instructions created for an ASP.NET project are written in plain text including HTML tags and script sections. In previous ASP applications, the code simply had to be interpreted by the browser. With ASP.NET, you must compile the project the same way you would proceed for a Windows application. To compile and execute an ASP.NET project in one step, on the main menu, you can click Debug -> Start Without Debugging. Although there are other techniques or details in compiling (or debugging) and executing a project, for now, this is the only technique we will use until further notice. |
|
Opening a Project |
|
As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project:
|
|
Language Support |
|
Introduction |
|
An ASP.NET application is a series of files that contain code. To write this code, you can use one or more languages. The primary language used in most web pages is HTML. To enhance the behaviors of your web pages, you can add scripted code to them. The code you write must be an appropriate language. ASP.NET supports various languages. When Microsoft developed ASP.NET, the company also made it possible to use the regular Visual Basic .NET language to create web-based applications. In fact, they went further than that by providing support for other languages other than MSVB .NET. Because a server can be presented with scripts written in any supported language, the server needs to know what language you are using so it is prepared to better interpret your code. |
|
Code Delimiters |
|
When developing your web pages, you can include HTML and scripting code in the same file but you must distinguish them. While HTML uses its own tags, to show the beginning of an Active Server Pages script, you must type <%. To show the end of that section, you must type %>. Everything between the <% and the %> delimiters section is part of the script and is reserved only for the script. |
|
Your Language of Choice |
|
By default, ASP.NET is primarily supported with Microsoft Visual Basic .NET, Visual C#, J#, and JScript. Other languages may be supported also. To specify the language of your choice, in the first line of your page, you can use the following formula: <%@ Page Language="FavoriteLanguage" %> The FavoriteLanguage factor must be the name of the language you use for your code. It can be VB, C#, or JScript. For example, if you will be using Visual Basic .NET, you can write this line as: <%@ Page Language="VB" %> Here is an example: <%@ Page Language="VB" %>
<html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("<b>Lecturer:</b> Benjamin Jacobson") %>
<br>
<% Response.Write("<b>Time Allocated:</b> <i>52 minutes</i>") %>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
In the same way, you can replace VB in the above code with C# or JScript. As you may be aware, each language has its own rules that you must follow when programming in it. Just changing the name of the language from the above line and leaving the rest of the code unchanged doesn't complete the job; in fact, simply changing the name of the language is a guaranty that some of the code on the page would not work anymore. Consider the following change: <%@ Page Language="C#" %>
<html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("<b>Lecturer:</b> Benjamin Jacobson") %>
<br>
<% Response.Write("<b>Time Allocated:</b> <i>52 minutes</i>") %>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
This would produce: ![]() Every statement in C# and JScript must end with a semi-colon. This means that, if you prefer to use C# for the above code, you would have to include an ending semi-colon for each statement: <%@ Page Language="C#" %>
<html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("<b>Lecturer:</b> Benjamin Jacobson"); %>
<br>
<% Response.Write("<b>Time Allocated:</b> <i>52 minutes</i>"); %>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
|
|
Accessories For Code Writing |
|
Code Comments |
|
In the programming world, a comment is text that the compiler would not consider when reading the code. As such a comment can be written any way you want. In Visual Basic, the line that contains a comment can start with a single quote. Here is an example: ' This line will not be considered as part of the code Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be read. Here is an example: Rem I can write anything I want on this line Comments are very useful and you are strongly suggested to use them regularly. They can never hurt your code and they don't increase the size of your application. Comments can help you and other people who read your code to figure out what a particular section of code is used for, which can be helpful when you re-visit your code after months or years of not seeing it. |
|
Line Continuation: _ |
|
You will regularly need to expand your code on more than one line. This happens if you are writing an expression that involves many entities that must belong to a group. To continue a line of code to the next, type an empty space followed by an underscore symbol, then continue your code on the next line. |
|
The Carriage Return-Line Feed Constant |
|
Visual Basic provides the vbCrLf operator. It is used to interrupt a line of text and move to the next line. |
|
Active Objects |
|
The Response Object |
|
To support the display of items on a web page, Active Server Pages provides an object called Response. This object is equipped with different methods for different tasks. The method used to display something on a web page is called Write. Its syntax is: Reponse.Write(Something) The item to display must be passed as argument. If you want to display an empty space, a character, or a group of characters, pass it double-quoted in the parentheses of this method. Here is an example: <html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("Lecturer: Benjamin Jacobson") %>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
This would produce: ![]() If the item to display is a regular number, whether natural or decimal, you can pass it directly to the method. Here is an example: <html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("Lecturer: Benjamin Jacobson") %>
<p>Time Allocated: <% Response.Write(52) %> minutes </p>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
This would produce: |
![]() |
|
Responding to HTML Tags |
|
When you write HTML tags in your code, the browser would interpret them and publish a result to the user. As opposed to HTML tags, most of the scripts you write are not directly intended to your user's browser. They are presented to the web server that analyzes the script and responds to it. In fact, if you include Active Server Pages scripts in <% and %>, when the resulting page is presented to the user, the script sections are removed. This also means that a curious visitor would not even know what your code looks like. For example, here is the code presented to the browser from the above web page: ![]() When a file with active code is sent to the server, the server checks the <% %> sections and their contents. Any section that is contains, for example, double-quoted strings passed to the Response.Write() method and considered "as is". The server would remove <%, the Response.Write() method, its double-quotes, and %>. This means that, whatever you pass to this method would be sent to the browser the same way you passed it. This allows you to include HTML tags as part of the Response.Write() argument but it is your responsibility to send it as regular HTML code. If you make mistakes, they would be presented to the browser, the server might not care and the server cannot or would not fix it. Based on this, you can include normal HTML code and pass it to the Response.Write() method. Consider the following example: <html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Lesson 2: Active Pages</h1>
<% Response.Write("<b>Lecturer:</b> Benjamin Jacobson") %>
<br>
<% Response.Write("<b>Time Allocated:</b> <i>52 minutes</i>") %>
<p>This lesson shows different ways of displaying items on a
web page. The instructions involve both HTML and scripts</p>
<h3>Enjoy</h3>
</body>
</html>
When the server receives this code, it removes the <% Reponse.Write(" and the ") %> sections. Then its sends the rest to the browser: ![]() The browser is then receives normal HTML tags that it must interpret and present the result in the intended web page:
In the same way, you can write any sophisticated HTML code and pass it to the Reponse.Write() method. This consequently means that if you pass bad HTML code to a Response.Write() method, the result may be unpredictable. |
|
|
||
| Previous | Copyright © 2005-2008 FunctionX, Inc. | Next |
|
|
||