ASP.NET Fundamentals Introduction Active Server Pages (ASP) is a technology developed primarily by Microsoft to create dynamic web sites that allow a visitor to submit values to a web page. The web page would then transmit the results to a web server. This technology requires knowledge of a scripting language such as VBScript or JavaScript. To enhance ASP, Microsoft developed ASP.NET. This new approach allows you to use the .NET Framework and its rich features to create web-based applications. The .NET Framework is a library used to create both computer-based and web-based applications. It is made of different parts. The section that deals with computer applications is referred to as Windows Forms. The part dedicated to the web is called ASP.NET. ASP.NET Prerequisites To create and use ASP.NET applications, you have many options: you can work from your computer or you can work from a remote machine. If you want to work from your computer, it should run the appropriate operating systems and the latest Internet browser. To support databases (even if you don't intend to create web-based databases), you must install the latest version of MDAC, which is a free download from the Microsoft web site. Normally, you may have MDAC installed already but just in case, you need it... Along with one of the appropriate operating systems, you must install the latest version of Microsoft Internet Information Services (IIS). Normally, during the installation of the operating system, you would have been prompted to install it. Otherwise, you can install it from Control Panel -> Add/Remove Programs -> Add/Remove Windows Components -> Internet Information Services (IIS) and follow the steps. If necessary, you must install the .NET Framework. It is likely to be installed already. You should install the latest Internet browser. If you want to check, you can open the Programs in Control Panel and you should see an entry for Microsoft .NET Framework. You can also see its entry in the Start menu. If you don't have it already, you can download it (free) from the Microsoft web site or http://www.asp.net. An ASP.NET Website In our lessons, we will learn how to create web pages. After creating a web page, you should test it. You have two options. You can test a page locally by executing your project. As an alternative, you can test it on a real web site. You have various alternatives. You can set up a web server at home or at your job to host your (own) web site. The process is, or can be, long and there are various issues involved (name registration, DNS, etc). An alternative to hosting your web site is to let another company host it for you. When selecting a company that would host your web site, make sure (you specify to your hosting company that) your site can display ASP.NET web pages. Also make sure they have tested it and it works fine. Based on this, instead of creating ASP.NET websites from your computer, you can work directly on your website. In our lessons, we will create the webpages directly on the website. ASP.NET and Regular Web Files A web site is a series of files, named web pages, grouped in a main parent folder. 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. Fundamentals of Web Sites and Pages Introduction to Websites A Web site, also called a website, is a computer application that displays in a Web browser. To start a website, create a folder or directory on a computer. You can give any name to the folder but, by tradition, it should use only letters and/or numbers or a combination of those. A person who creates or manages a website is called a webmaster. Introduction to Webpages A Website is made of one or more files. The most fundamental type of file used in a website is called a Web page, or webpage, or page. The page is created from a document saved with an appropriate extension. The most widely used extensions for regular webpages are .htm and .html. A normal ASP.NET webpage has the extension.aspx. HTML in a Webpage A Webpage is a text-based document that contains regular readable characters. A Web browser reads that document, interprets it, and presents the result(s) to a visitor of the related webpage. To indicate to the browser how it should display the contents of a webpage, the content of webpage document is written is an easy and flexible language named hypertext-markup language, or HTML. HTML is made of tags. Every HTML tag has a specific purpose, which is defined as its default behavior. Most tags use additional information through attributes. An attribute is created in the starting tag. Here is an example of code for a webpage named exercise.aspx: <html> <head> <title>Employee Time Sheet</title> </head> <body> <p>Welcome to our corporate website.</p> </body> </html> This would produce: If you want the webpage code to follow the latest HTML standards, you can put <!DOCTYPE html> in the top section of the code. Here is an example: <!DOCTYPE html> <html> <head> <title>Employee Time Sheet</title> </head> <body> <p>Welcome to our corporate website.</p> </body> </html> Cascading Style Sheet in a Webpage Cascading style sheet, or CSS, is a language separate from HTML, but that is used to "influence" it. CSS is used to indicate how an HTML tag should display its result. The primary technique of using CSS is to apply a style to a tag of your choice. In this case, CSS is created as an attribute named style. To apply it, in the start tag, add a style attribute and assign the desired style(s). CSS inline allows you to create a style for each tag. If you want to apply the same style to more than one tag, you can create the style in the head section. Such a style becomes available to the whole page. Each style is created as a class. If you have a long list of styles you want to apply to various sections of a document, you should put those styles in a separate file and add its link to the page that needs its style. The linking is created in the head section of the webpage. The formula to create the link is: <!DOCTYPE html> <html> <head> <link rel=StyleSheet type="text/css" href="file-name.css"> <title>Employee Time Sheet</i> </head> <body> </body> </html> You can put the link before or after any other tag that is in the head section. ASP.NET Types of Applications Introduction There are three main categories of applications you can create for your web-based project: HTML only, Active Server Pages, and ASP.NET. This also means that, in your applications, you may deal with various types of files. You can create a complete application using a text editor and IIS only or using a programming environment such as Microsoft Visual Studio. Just as Notepad supports all these types of applications, Microsoft Visual Studio also is equipped to create, handle, and maintain HTML-only applications, Active Server Pages, or ASP.NET files. As mentioned already, in our lessons, we will create our webpage directly on the website. HTML-Based Applications An HTML-based application is a web-based project that mostly includes regular webpages with HTML tags, optional Cascading Style Sheet formatting, and optional scripts to perform some tasks that are difficult or impossible with only HTML. You can also use other formatting languages like XML to support some type of data in your application but an HTML-based application is primarily meant to use HTML tags to present text and graphics to a visitor. With this type of application, you usually don't expect any back-and-forth interaction between the visitor and you. To create an HTML-based application, you must create a folder with a name of your choice. You can create this folder using a file utility such as Windows Explorer. After creating the folder, you can add the desired files to it. ASP.NET Applications An ASP.NET project is an enhanced version of an Active Server Pages application and you can create it using Notepad. For our lessons, we will create our AP.NET websites using Microsoft Visual Studio. Fundamentals of Web Coding 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, including Visual Basic and C#. When developing your webpages, 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 <%, which is called a delimiter. To show the end of that section, you must type %>, which is also called a delimiter. Here is an example: <!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<% %>
</body>
</html>
Most of the time, you will write each delimiter on its own line. Here is an example: <!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> <% %> </body> </html> This technique is not a rule. Sometimes it simply makes your code easier to read. Everything between <% and %> is part of the script and is reserved only for the script. Although we created only one delimiting section, you can create as many delimiting sections as you want. Here are examples: <!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<% %>
<% %>
<% %>
</body>
</html>
Of course, you can create the sections where each delimiter is on its own line: <!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> <% %> <% %> <% %> <% %> </body> </html> Between an opening delimiter <% and a closing delimiter %>, you can add the necessary ASP code, which can consist of ASP code, HTML code, and others. Before an opening delimiter <% or after a closing delimiter %>, you can add HTML code as you want but no ASP code: <!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> HTML Code <% ASP Code, HTML Code, Scripting Code %> HTML Code <% ASP Code, HTML Code, Scripting Code %> HTML Code <% ASP Code, HTML Code, Scripting Code %> HTML Code </body> </html> If you include code in <% and %>, when the resulting page is presented to the user, the codes are removed. This means that a curious visitor cannot see your code. Your Language of Choice ASP.NET is supported by many languages, including Microsoft Visual Basic and C#. To specify your language of choice, in the first line of your page, use the following formula: <%@ Page Language="favorite-language" %> This means that the delimiter used to bind a webpage to an external program is <%@. To specify your preferred language, assign it to the Language attribute. If that language is Visual Basic, use VB. This can be done as follows: <%@ Page Language="VB" %>
Here is an example: <%@ Page Language="VB" %>
<html>
<head>
<title>ASP.NET Tutorials</title>
</head>
<body>
<h1>Active Pages</h1>
<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 with C# or JScript. Each language has its own rules. The Compiler A website has a series of instructions that make it possible for a webpage visitor to interact with a webpage. The main application that manages the code behind-the-scenes is called a compiler. Accessories For Code Writing Visual Basic Code Comments A comment is text that is not treated as 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: <%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h1>Exercise</h1>
<%
' This is a comment.
%>
</body>
</html>
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: <%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h1>Exercise</h1>
<%
REM This is a comment.
%>
</body>
</html>
White Spaces When writing code, you will have to separate different parts with spaces. This is referred to as white space. The amount of space you put between two words is not important. Server Comment To create a comment in a code block, the delimiter to use is <%-- ... --%>. To support the display of items on a web page, the IIS library provides an object called Response. This object is equipped with Write() that is used to display something on a webpage. To access Write(), type Response, followed by a period, followed by Write() as follows: Reponse.Write(Something) The item to display must be written in the parentheses. If you want to display an empty space, a character, or a group of characters, pass it in double-quotes in the parentheses of Response.Write(). Here is an example: <%@ Page Language="VB" %>
<html>
<head>
<title>Department Store</title>
</head>
<body>
<h1>Department Store</h1>
<% Response.Write("Employee Time Sheet") %>
<p>Welcome to our website.</p>
</body>
</html>
This would produce: If the item to display is a regular number, whether natural or decimal, you can write it without the quotes in the parentheses of Response.Write(). Here is an example: <%@ Page Language="VB" %>
<html>
<head>
<title>Department Store</title>
</head>
<body>
<h1>Department Store</h1>
<%
Response.Write("Hourly Salary: ")
Response.Write(22.75)
%>
</body>
</html>
This would produce:
Responding to HTML Tags You can include HTML code in the parentheses of Reponse.Write(). Such code should be provided in double-quotes. Here is an example: <%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <title>Employment Application</title> </head> <body> <h2>Employment Application</h2> <% Response.Write("<p>Before filling out the employment application, please upload the required documents.</p>") %> </body> </html> This would produce:
This consequently means that if you pass bad HTML code to Response.Write(), the result may be unpredictable. |