Ads Header

Saturday, September 4, 2010

ASP .Net Sample Application for the First Timers

This article looks at some of the basics of ASP .Net with a small sample program which writes "Hello World" in the browser.

ASP .Net Class Model:

   The ASP .Net has been built on a collection of classes over the .Net framework. Each control, behavior etc., are implemented using one/many of the .Net classes. These classes could be present either in the Global Assembly Cache (GAC) or the local WWWRoot directories.


   In this model, a web page is derived from System.Web.UI.Page, a user control is derived from System.Web.UI.UserControl, the controls are derived from System.Web.UI.WebControls and tables are derived from System.Web.UI.WebControls.Table etc., The most beautiful feature is all these controls can be accessed via intellisense inside Visual Studio .Net.
   All the HTML and FORM elements defined in a web page are now available as server controls viz., Text Boxes, Labels, List boxes, Dropdown list controls, Tables etc., All of them can be processed on the server (even enabling each control with a client side javascript validations).

The Compiled Page Serving/Execution model in ASP .Net:

   ASP .Net in contrast to older technolgy(ASP), has a compiled model. ASP had an interpreted model where the web server will interpret the .asp page every time when a request for the page is made. But with ASP .Net, the first time load of any page is slower. The reason is because the page gets compiled when the first person makes the request. But subsequently, the pages are loaded at a much faster rate as the server has to execute only the compiled code.
   The file extensions of asp .net are .aspx. The forms in these aspx files can still follow either a "Get" or "Post" Method mechanisms. The major difference in asp .net over the classic asp programming is, the previous model needed atleast two pages for a form post process. i.e., one file for collecting the data and one file for processing the data. With asp .net there is no need for a second file. The same web page can post the data to itself.
   Here is one small sample code, which takes input from a text box inside a web form and displays the input on submitting the form.

<%@ Page language="c#" AutoEventWireup="true"%>
<script language="C#" runat="server">
private void Button1_Click(object sender, System.EventArgs e)
{
lblSample.Text = txtSample.Text;
}
&gl;/script>

<form id="Form1" method="post" runat="server">
<P> <asp:Label id="lblSample" runat="server" Width="208px">Label</asp:Label></P>
<P> <asp:Label id="Label1" runat="server" Width="104px">Enter Text Here:</asp:Label>
<asp:TextBox id="txtSample" runat="server"></asp:TextBox></P>
<P> <asp:Button id="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></asp:Button> </P>
</form>

   The above code can be pasted on to an aspx page and opened in Internet Explorer as http://servername/virtualdirectoryname/sample_page1.aspx. If the folder on which this page is hosted is not configured as a virtual directory in IIS using inetmgr, then the page will not load and will return some errors.
   If this page is written using Webmatrix, then executing this asp .net page is a straight forward mechanism. Open the file in webmatrix and press F5 key. This will automatically open it on a web browser.
   The above page can also be written with a clear separation of the Presentation Layer from the Processing Layer. The .aspx page can be made to host just the HTML and another .cs file can hold the business logic. This is dealt in another article titled Using Code behind in Asp .net.

0 Responses to “ASP .Net Sample Application for the First Timers”

Post a Comment