Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NET
ASP.Net
• ASP.Net is a web development platform provided by Microsoft. It is
used for creating web-based applications. ASP.Net was first released
in the year 2002.
• The first version of ASP.Net deployed was 1.0. ASP.Net is designed to
work with the HTTP protocol. This is the standard protocol used
across all web applications.
• ASP.Net applications can also be written in a variety of .Net
languages. These include C#, VB.Net, and J#. In this chapter, you will
see some basic fundamental of the .Net framework.
• The full form of ASP is Active Server Pages, and .NET is Network
Enabled Technologies.
ASP.NET Architecture and its Components
• The architecture of the.Net framework is based on ASP.Net is a framework which is used to develop a
the following key components Web-based application. The basic architecture of the
• Language – A variety of languages exists for .net ASP.Net framework is as shown
framework. They are VB.net and C#. These can be
used to develop web applications.
Object creation - The next stage is the creation of the HttpContext, HttpRequest & HttpResponse by the web
server. The HttpContext is just the container for the HttpRequest and HttpResponse objects. The HttpRequest
object contains information about the current request, including cookies and browser information. The
HttpResponse object contains the response that is sent to the client.
• HttpApplication creation - This object is created by the web server. It
is this object that is used to process each subsequent request sent to
the application. For example, let's assume we have 2 web
applications. One is a shopping cart application, and the other is a
news website. For each application, we would have 2 HttpApplication
objects created. Any further requests to each website would be
processed by each HttpApplication respectively.
• Dispose - This event is called before the application instance is
destroyed. During this time, one can use this method to manually
release any unmanaged resources.
• Application End - This is the final part of the application. In this part,
the application is finally unloaded from memory.
ASP.Net Page Lifecycle
When an ASP.Net page is called, it
goes through a particular lifecycle.
This is done before the response is
sent to the user. There are series
of steps which are followed for the
processing of an ASP.Net page.
• Page Request- This is when the page is first requested from the server. When the page is requested, the
server checks if it is requested for the first time. If so, then it needs to compile the page, parse the response
and send it across to the user. If it is not the first time the page is requested, the cache is checked to see if
the page output exists. If so, that response is sent to the user.
• Page Start – During this time, 2 objects, known as the Request and Response object are created. The Request
object is used to hold all the information which was sent when the page was requested. The Response object
is used to hold the information which is sent back to the user.
• Page Initialization – During this time, all the controls on a web page is initialized. So if you have any label,
textbox or any other controls on the web form, they are all initialized.
• Page Load – This is when the page is actually loaded with all the default values. So if a textbox is supposed to
have a default value, that value is loaded during the page load time.
• Validation – Sometimes there can be some validation set on the form. For example, there can be a validation
which says that a list box should have a certain set of values. If the condition is false, then there should be an
error in loading the page.
• Postback event handling – This event is triggered if the same page is being loaded again. This happens in
response to an earlier event. Sometimes there can be a situation that a user clicks on a submit button on the
page. In this case, the same page is displayed again. In such a case, the Postback event handler is called.
• Page Rendering – This happens just before all the response information is sent to the user. All the
information on the form is saved, and the result is sent to the user as a complete web page.
• Unload – Once the page output is sent to the user, there is no need to keep the ASP.net web form objects in
memory. So the unloading process involves removing all unwanted objects from memory.
First Web Application
• Click on New then Project
• Select Web then ASP.NET Web Application
• Click on OK button. It will create new empty website. On Right side solution
explorer . You will get Default.aspx and in it Default.aspx.cs page.
using System;
<%@ Page Language="C#" AutoEventWireup="true" using System.Collections.Generic;
CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System.Linq;
<!DOCTYPE html> using System.Web;
<html xmlns="http://www.w3.org/1999/xhtml"> using System.Web.UI;
<head runat="server"> using System.Web.UI.WebControls;
<title></title>
</head> public partial class _Default : System.Web.UI.Page
<body> {
<form id="form1" runat="server"> protected void Page_Load(object sender, EventArgs e)
<div> {
}
</div>
</form> }
</body>
</html>
• 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] %>
ASP.NET First Program
• The first step involves the creation of a new project in Visual Studio. After launching Visual Studio,
you need to choose the menu option New->Project.
• The next step is to choose the project type as an ASP.Net Web application. Here we also need to
mention the name and location of our project.
In the project dialog box, you can see various options for creating different types of projects.
Click the Web option on the left-hand side.
When we click the Web option in the previous step, we will be able to see an option for
ASP.Net Web Application. Click this option.
We then give a name for the application, which in our case is DemoApplication. We also
need to provide a location to store our application.
Finally, we click the 'OK' button to let Visual Studio to create our project.
• If the above steps are followed, you will get the default.aspx file in tab of Visual Studio. And at right
hand side you will find Solution Explorer
• If you follow all of the above steps and run your program in Visual Studio, you will get the default
output displayed in the browser.
Directive in ASP.NET
• 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. @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.
@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.
@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.
@PreviousPageType directive: It is used for Cross-page posting between
ASP.NET pages.
.
Select web forms option in left corner and then select web form and hit add
button.
Now click on the add button and this form will add to our project.
After adding form, we can see that this is now in our project as we have shown in
the below image.
Double click on this form and this will show some auto generated code like this:
// user-form.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="user-
form.aspx.cs"
Inherits="asp.netexample.user_form" %>
<!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>
If we run this file on the browser, it does not show any output. So, let's print some message by this form.
The modified code is as below.
// user-form.aspx
ASP.NET provides various controls like: server controls and HTML controls for the Web Forms.
Server Controls
Control Name Applicable Events Description
Label None It is used to display text on the HTML page.
TextBox TextChanged It is used to create a text input in the form.
Button Click, Command It is used to create a button.
LinkButton Click, Command It is used to create a button that looks similar to the hyperlink.
ImageButton Click It is used to create an imagesButton. Here, an image works as a Button.
Hyperlink None It is used to create a hyperlink control that responds to a click event.
DropDownList SelectedIndexChanged It is used to create a dropdown list control.
ListBox SelectedIndexCnhaged It is used to create a ListBox control like the HTML control.
DataGrid CancelCommand, EditCommand, DeleteCommand, It used to create a frid that is used to show data. We can also perform paging, sorting, and formatting very
ItemCommand, SelectedIndexChanged, easily with this control.
PageIndexChanged, SortCommand,
UpdateCommand, ItemCreated, ItemDataBound
DataList CancelCommand, EditCommand, DeleteCommand, It is used to create datalist that is non-tabular and used to show data.
ItemCommand, SelectedIndexChanged,
UpdateCommand, ItemCreated, ItemDataBound
Repeater ItemCommand, ItemCreated, ItemDataBound It allows us to create a non-tabular type of format for data. You can bind the data to template items, which are
like bits of HTML put together in a specific repeating format.
AdRotator AdCreated It allows us to specify a list of ads to display. Each time the user re-displays the page.
Table None It is used to create table.
XML None It is used to display XML documents within the HTML.
Literal None It is like a label in that it displays a literal, but allows us to create new literals at runtime and place them into
this control.
ASP.NET Web Forms Label Property
AccessKey
Description
write code or use the drag and drop facility of BackColor It is used to set background color of the control.
visual studio IDE. BorderColor It is used to set border color of the control.
• This is server side control, asp provides own BorderWidth It is used to set width of border of the control.
tag to create it. The example is given below.
• < asp:TextBoxID="TextBox1" runat="server" > Font It is used to set font for the control text.
• Server renders it as the HTML control and Text It is used to set text to be shown for the control.
produces the following code to the browser.
ToolTip It displays the text when mouse is over the control.
• <input name="TextBox1" id="TextBox1" type
="text"> Visible To set visibility of control on the form.
• This control has its own properties that are Height It is used to set height of the control.
tabled
Width It is used to set width of the control.
• This control is used to perform events. It is also used AccessKey It is used to set keyboard shortcut for the
control.
to submit client request to the server. To
TabIndex The tab order of the control.
create Button either we can write code or use the
drag and drop facility of visual studio IDE. BackColor It is used to set background color of the
control.
• This is a server side control and asp provides own tag BorderColor It is used to set border color of the control.
to create it. The example is given below.
• < asp:ButtonID="Button1" runat="server" Text="Sub BorderWidth It is used to set width of border of the control.
mit" BorderStyle="Solid" ToolTip="Submit"/> Font It is used to set font for the control text.
• Server renders it as the HTML control and produces
the following code to the browser. ForeColor It is used to set color of the control text.
• <input name="Button1" value="Submit" id="Button1 Text It is used to set text to be shown for the
" title="Submit" style="border- control.
style:Solid;" type="submit"> ToolTip It displays the text when mouse is over the
control.
• This control has its own properties that are tabled Visible To set visibility of control on the form.
• To create HyperLink either we can write code or use the BackColor It is used to set background color of the control.
drag and drop facility of visual studio IDE. This control is
listed in the toolbox. BorderColor It is used to set border color of the control.
• This is a server side control and ASP.NET provides own tag BorderWidth It is used to set width of border of the control.
to create it. The example is given below.
Font It is used to set font for the control text.
• < asp:HyperLinkID="HyperLink1" runat="server" Text="Jav
aTpoint" NavigateUrl="www.javatpoint.com" ></asp:Hyp ForeColor It is used to set color of the control text.
erLink>
• Server renders it as the HTML control and produces the Text It is used to set text to be shown for the control.
following code to the browser. ToolTip It displays the text when mouse is over the control.
• <a id="HyperLink1" href="www.javatpoint.com">JavaTpoi
nt</a> Visible To set visibility of control on the form.
• This control has its own properties that are tabled below. Height It is used to set height of the control.
• Server renders it as the HTML control and produces the ForeColor It is used to set color of the control text.
following code to the browser.
Text It is used to set text to be shown for the control.
• <input id="RadioButton1" type="radio" name="gender"
value="RadioButton1" /><labelforlabelfor="RadioButt ToolTip It displays the text when mouse is over the control.
on1">Male</label>
• This control has its own properties that are tabled Visible To set visibility of control on the form.
below.
Height It is used to set height of the control.
• This control has its own properties that are tabled Height It is used to set height of the control.
below.
Width It is used to set width of the control.
Submit Button Automatically POSTs the form data to the specified page listed in the Action attribute in the FORM tag
Password Field An input area on an HTML form, although any characters typed into this field are displayed as asterisks
CheckBox Gives the user a check box that they can select or clear
Radio Button Used two or more to a form, and allows the user to choose one of the controls
Dropdown Displays a list of items to the user, but only one item at a time will appear. The user can click a down arrow
from the side of this control and a list of items will be displayed.
Session State
• When a user connects to an ASP.NET website, a new session object is
created. When session state is turned on, a new session state object is
created for each new request. This session state object becomes part of
the context and it is available through the page.
• Session state is generally used for storing application data such as
inventory, supplier list, customer record, or shopping cart. It can also keep
information about the user and his preferences, and keep the track of
pending operations.
• Sessions are identified and tracked with a 120-bit SessionID, which is
passed from client to server and back as cookie or a modified URL. The
SessionID is globally unique and random.
• The session state object is created from the HttpSessionState class, which
defines a collection of session state items.
Application State
• The ASP.NET application is the collection of all web pages, code and
other files within a single virtual directory on a web server. When
information is stored in application state, it is available to all the
users.
• To provide for the use of application state, ASP.NET creates an
application state object for each application from the
HTTPApplicationState class and stores this object in server memory.
This object is represented by class file global.asax.
• Application State is mostly used to store hit counters and other
statistical data, global application data like tax rate, discount rate etc.
and to keep the track of users visiting the site.