Introducing ASP.NET

ASP.NET is a new and extended technology to the earlier classic ASP, introduced by Microsoft Corporation, which fully supports Microsoft’s .NET Framework. It supplies all the required user interfaces under the name "WebForms" and also works with all .NET languages like Visual C# .NET, Visual Basic .NET, etc. WebForms are used to create user interfaces for web pages. Till now, you’ve had to apply specific HTML tags for creating user interfaces and use an ASP scripting language such as VBScript or JavaScript.

But with the introduction of ASP .NET, you need not apply them any more. All you have to do is to call the custom GUI classes defined in the System.Web.UI.WebControls namespace of the .NET Framework. Moreover, System.Web namespace provides necessary classes, methods, and properties for developing client-server applications and the System.Web.UI namespace interacts with other .NET language like C#, VB .NET. Therefore, a C# file containing some methods or C# syntaxes can be easily called in your ASP applications. These topics will be examined in greater detail in the coming sections.
Benefits of ASP.NET

One major advantage of ASP.NET is that, you can apply the programming techniques of other .NET languages like C#, VB.NET etc. Hence, if you are conversant with any one language of .NET Framework, then it’ll be easy for you to write ASP.NET programs. Moreover, it eliminates the need for learning complex syntaxes. Another benefit of ASP.NET is that it supplies built-in controls for validations. Earlier you’ve to apply complex Java Scripts or VBScripts for validating form elements. Hence, all the essential tools for developing and deploying ASP applications is now under a single umbrella of Microsoft .NET.

Basic requirements

(1) Microsoft Windows 2000 Professional or Server, Microsoft Windows XP
(2) .NET Framework SDK
(3) Text Editor like Notepad
(4) Internet Explorer 5.5 and above
(5) Visual Studio .NET (Optional, but recommended)

Note: You have to install Microsoft Data Access Components 2.7 before installing .NET Framework SDK. You can download the same by clicking here. Please go through the additional notes given at the end of this article regarding Visual Studio .NET.

There are two phases involved in the ASP.NET development cycle, which are

(1) User Interface phase
(2) Coding phase

It’s the User Interface phase which we’ll call ‘WebForms’. So, before moving on to the coding phase, it’s essential for you to be aware of all types of user interfaces that .NET Framework provides. If you’re familiar with ‘WinForms’, then you are more likely to confuse it with WebForms. Keep in mind that WinForms are used to develop standalone GUI applications (Which can be executed by triggering the relevant .exe file), whereas WebForms are used to create Web based applications like Java Applets and for creating user interfaces for ASP applications.

Your First WebForm Program

Copy or enter the following code in Notepad and save the file as Firstwebform.aspx under Inetpub/wwwroot directory:

  1: <form method = "post" runat = "server">
  2: <asp:TextBox runat = "server" ></asp:TextBox>
  3: </form> 

If you save the above file in a directory under the www root, then you have to configure for the directory alias using personal web manager. Please review the additional notes given at the end of this article regarding configuration of Personal Web Manager. The execution of this file is very similar to that of classic ASP. Only thing you’ve to remember is to supply the correct extension for ASP.NET, which is .aspx.

A WebForm control is created using the following syntax:

  1: <form method = "post" runat = "server">
  2: <asp:ControlName runat = "server"></asp:ControlName>
  3: </form> 

The runat attribute is compulsory without which your code will not compile correctly. From the above code, it can be seen that ASP.NET follows the syntax of XML.

Working with controls

ASP.NET provides a lot of controls for building powerful user interfaces. Important among them are Buttons, Radio Buttons, Check Boxes, List Boxes, and Combo Boxes. The .NET Framework also supplies advanced controls like Calendars, AdRotators, and etc. In the following sections, I’ll cover each of these controls with the help of simple examples.

Buttons

Buttons are used to trigger actions. Without buttons, we cannot do any sort of work on the form.

  1: <form method = "post" runat = "server">
  2: <asp:Button id = "b1" runat = "server" text = "OK" ToolTip = "Click here">
  3: </asp:Button> 
  4: </form> 

Radio Buttons

With radio buttons you can select only one item at a time. The only thing you have to remember is that the GroupName attribute should be same for radiobuttons.

<form method = "post" runat = "server">
Select your choice:<asp:RadioButton id = "r1" runat = "server" 
Text = "Male" GroupName = "g1"></asp:RadioButton>
<asp:RadioButton id = "r2" runat = "server" Text = "Female" 
GroupName = "g1">
</asp:RadioButton>
</form> 

CheckBoxes

Checkboxes enables you to select any number of items at a time. They can be activated either by clicking on it or by hitting the space bar.

<form method = "post" runat = "server">
Choose: <asp:CheckBox id = "c1" runat = "server" Text = "Television">
</asp:CheckBox><asp:CheckBox id = "c2" runat = "server" Text = "PC">
</asp:CheckBox><asp:CheckBox id = "c3" runat = "server" 
Text = "Telephone">
</asp:CheckBox>> 

DropDownList

This control looks like a typical combo box in Visual Basic. Users can select any one item at a time.

<form method = "post" runat = "server">
<asp:DropDownList runat = "server">
<asp:ListItem id = "d1" runat = "server" value = "India">India
</asp:ListItem>
<asp:ListItem id = "d2" runat = "server" value = "China" >China
</asp:ListItem>
<asp:ListItem id = "d3" runat = "server" value = "Russia">Russia
</asp:ListItem>
<asp:ListItem id = "d4" runat = "server" value = "USA">USA
</asp:ListItem>
<asp:ListItem id = "d5" runat = "server" value = "UK">UK
</asp:ListItem>
</asp:DropDownList>
</form> 

List Box

With List boxes you can select multiple items by scrolling up and down.

<form method = "post" runat = "server">
<asp:ListBox runat = "server">
<asp:ListItem id = "l1" runat = "server" value = "India">India
</asp:ListItem>
<asp:ListItem id = "l2" runat = "server" value = "China">China
</asp:ListItem>
<asp:ListItem id = "l3" runat = "server" value = "Russia">Russia
</asp:ListItem>
<asp:ListItem id = "l4" runat = "server" value = "USA">USA
</asp:ListItem>
<asp:ListItem id = "l5" runat = "server" value = "UK">UK
</asp:ListItem>
</asp:ListBox>
</form> 

Image Button

You can create a button with an image on it. Try out the following code and observe the output:

<form method = "post" runat = "server">
<asp:ImageButton id = "b10" runat = "server" 
src = "f:\logo.gif"></asp:ImageButton>
</form> 

Calendar

This is an advanced control in ASP.NET. Your users can choose a date with the help of this control. Try out by copying the following piece of code:

<form method = “post” runat = "server">
<asp:Calendar id ="c1" runat = 'server"></asp:Calendar>
</form> 

Link Button

You can easily give a link to your button using this new LinkButton control.

<asp:LinkButton id = "ib1" Text = "click me" 
href = "http://www.yahoo.com" runat = "server" Font-Name="Verdana" Font-Size="14pt" > </asp:LinkButton> 

Hyperlink

If you’re still using the typical <a> tag for creating hyperlinks, check out the code for this new .NET control:

<asp:hyperlink runat = "server" target = "_blank" 
navigateurl = "http://www.developer.com" text = "<b>Hello</b>">
</asp:hyperlink>

Validating Fields

Sometimes users may submit the form without filling in the required information. In order to avoid this, .NET supplies a control named RequiredFieldValidator, which checks the corresponding control attached to it. You will learn more about validations in a separate article.

<asp:RequiredFieldValidator id = "valid1" runat = "server" 
ErrorMessage = "We need your email id" 
ControlToValidate = "t1">
</asp:RequiredFieldValidator> 

AdRotator

You can randomly display advertisements by using AdRotator component. However, it involves several processes. Firstly, you have to create an XML file containing the listing as shown below:

<?xml version = "1.0"?>
<Advertisements>
<Ad>
<ImageUrl>logo.gif</ImageUrl>
<TargetUrl>http://www.logo.com</TargetUrl>
<AlternateText>Ok</AlternateText>
<Impressions>80</Impressions>
</Ad>
<Ad>
<ImageUrl>logo1.gif</ImageUrl>
<TargetUrl>http://www.microsoft.com</TargetUrl>
<AlternateText>Microsoft</AlternateText>
<Impressions>80</Impressions>
</Ad>
</Advertisements> 

You can modify this code by including your preferences. Make sure that both the XML file and the required images are in a same directory. The last step is to create a .aspx file with the following code:

<asp:adrotator id = "ad1" runat = "server" width = "50" 
height = "50" advertisementfile = "ourads.xml">
</asp:adrotator> 

The above set of code produces a static Web page (i.e. Without any major actions). If you need to add some sort of dynamism to your pages, then you have to learn about event handling. It’s similar to JavaScript, but with some variation. As we’ve already seen, you can apply actions using any .NET language. But for our examples in this article, I use C#. The listing shown below shows how to activate a WinForm Button:

<html>
<head>
<%@language = "C#"%>
void show(Object sender, EventArgs e){
t1.Text = "Welcome to ASP.NET" }
</head>
<body>
<asp:TextBox id = "t1">
<asp:Button id = "b1" Text = "Click here" 
onClick = show()>
</body>
</html> 

About Visual Studio

Visual Studio shortly called as VS provides a very easy way for designing user interfaces using WebForms. You are required to just place the controls from the Toolbox to the form and set relevant properties form the properties dialog box, just like Visual Interdev 6.0. Now Visual Studio .NET provides a single development environment with which you can develop any .NET applications using any .NET language. In the earlier version, Visual Studio 6.0, each language had a separate environment. Hence, developers faced many problems while migrating from one language to another. They have to understand the new environment and learn new syntax while migrating from Visual Basic 6.0 to Visual C++ 6.0. These difficulties are eliminated with the introduction of .NET technology and Visual Studio .NET.

Leave a Comment