ASP.NET Tutorial

Introduction

ASP.NET is a web development technology and a part of Microsoft .Net platform. Web application is totally different from standard Windows application. A web application is deployed on a web server.  Web browser on a client machine is access the web application using HTTP. Request from the web browser and response by the web server is done through HTTP.

When any user writes website address on address bar of browser and hit the OK button then the following steps are performed.

  • A GET request is sent to web server through HTTP.
  • The Web server processes the GET request and sends a response back to the Web browser.
  • Web browser then processes the response.
  • If the user again submit the page to web server by clicking button or by some event, then HTTP post the data back to server.
  • Again server processes the request and sends HTML to client (Browser)
  • Web browser again processes the response, which came from server, and displays the Web page to the user on the browser.
This process continues till the user explicitly close the application.

In earlier version of Visual Studio .NET (as example 2002/2003 IDEs), developers had to use IIS for all Web applications. In later version visual studio includes a built - in Web server that you can use for development. Based on location, In Visual Studio 2008 and later you can create file-system–based server on your computer, an IIS server, or an FTP server web application.

File system: You can store all the files of your web application in desired folder. It uses inbuilt server of visual studio. If you want to run and debug your Web site locally but do not want to use local IIS, then you can use file system.

FTP: If Web site is hosted on a remote computer and you want to connect via FTP to manage your files on a remote server then FTP is good choice.

HTTP: If a site deployed inside IIS, then HTTP-based Web site is used.

Creating a new website

Start Visual Web Developer and select File, New Web Site. The New Web Site dialog box appears as given below. Provide the location and name of your website. If you don’t want inbuilt master pages along with other web pages then choose ASP.NET empty web site otherwise choose ASP.NET forms web site. We have chosen ASP.NET empty web site.

new web site dialog box

Click on OK button. It will create new empty website. Right click on solution explorer and new web form. You will get Default.aspx and Default.aspx.cs page.

ASP.NET provides two types of coding model.

  • Code-inline model.
  • Code-behind model.
In code inline model, your logic and presentation code are written in a single aspx page.

Code-behind model provides clean code separation of the page’s business logic from its presentation logic. Your presentation logic goes in .aspx file and your cs or vb code (business logic) in .aspx.cs file.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
    <form id="form1" runat="server">
        <div>
    
        </div>
    </form>
  </body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}


Default.aspx page is starts with page directive.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

In ASP.NET a directive always begins with the special characters <%@ and ends with the characters %>. When the page is compiled, directive provides the important information to compiler.

General syntax of a directive is as follows.

<%@ [Directive] [Attribute=Value] %>

Following are the important directive in ASP.NET 4

  • Assembly
  • Control
  • Implements
  • Import
  • Master
  • MasterType
  • OutputCache
  • Page
  • PreviousPageType
  • Reference
  • Register
@Page directive: This directive is used by every .aspx page and controls the behavior of asp.net page. It provides the important information to compiler.

@Master directive: It similar to page directive but mainly used with master page. The content page uses master page as template. @Master directive has less attributes than @Page directive.

@Control directive: When you create custom/user ASP.NET control, then control directive is used. It allows you to create the properties to be inherited by the user control.

Example:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

@Import directive: The @Import directive is used to import a namespace into the ASP.NET page or user control. By using particular namespace with the help of import directive, all the classes and interfaces of the namespace are available to the page or user control. This directive supports only a single attribute called Namespace.

Example:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


@Implements directive: The @Implements directive is used to implement .NET Framework interface. This directive supports only a single attribute name as Interface.

@Register directive: This directive comes in picture when you drag and drop a user control onto any of your .aspx pages. When you drag a user control onto the .aspx page, @Register directive creates automatically at the top of the page.

@Assembly directive: The @Assembly directive attaches assemblies in web pages. It supports Name and Src Attributes.

Example:
<%@ Assembly Name="MyWebAssembly" %>
<%@ Assembly Src="MyWebAssembly.cs" %>


@PreviousPageType directive: It is used for Cross-page posting between ASP.NET pages. It supports TypeName and VirtualPath attributes.

@MasterType directive: This directive is used, when the master page is accessed from the Master property.

@OutputCache directive: It is used for Page Output Caching. It catches the page for given time duration.

@Reference directive: As it name suggests it refer to another ASP.NET page or user control. Referred page is compiled along with ASP.NET page.

ASP.NET folders

ASP.NET provides different folders for containing different types of folder. Some folders added implicitly in your website but you can also add these folders explicitly.

  • App_Code: This folder contains class files (.cs,.vb, and .jsl files)
  • App_Data: It contains databases such as Microsoft SQL Server Express Edition .mdf files.
  • App_Browsers: It Contains browser definition files (.browser) that uses to identify browsers.
  • App_GlobalResources: Contains resources files such as .resx and .resources files.
  • App_Themes: It contains specific theme for your site. App_Themes folder has SkinFile as subfolder that contains .skin file.
  • App_WebReferences: Contains Web reference files such as .wsdl, .xsd, .disco, and .discomap files.
  • Bin: This folder contains the assembly or .DLL file. These files are referenced in your application automatically.