Unit 3 Asp Net
Unit 3 Asp Net
NET 1
Unit 3: ASP.NET
2. In VS, this is very easy. Open the File menu and select "New Web Site". You will be
presented with the following dialog:
4. Enter the name for your new site in the Location box.
1. In Solution Explorer, right-click the project name, and then click Add New Item.
2. In the Add New Item dialog box, under Installed Templates, click the language that you
want to work with, and then click Web Form
3. If you want the code for the page to be in a separate file, be sure that the Place code in
separate file check box is selected. If you want to keep the code and markup in the same
file, clear this check box.
4. In the Name box, type a name for the new Web page, and then click Add.
The new ASP.NET Web page is created and displayed in Visual Studio.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1> Welcome to ASP.NET </h1>
</div>
</form>
</body>
</html>
The application is run by selecting either Start or Start Without Debugging from the Debug
menu, or by pressing F5 or Ctrl-F5. The program is built i.e. the .exe or the .dll files are
generated by selecting a command from the Build menu.
Listing:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
for (int i = 0; i <= 6; i++)
{
%>
<font size="<%=i%>">Welcome to ASP.NET 4.5</font><br />
<%
}
%>
</div>
</form>
</body>
</html>
ASP.NET provides two types of coding techniques, Single- file model and Code-Behind page
model.
Listing:
<script runat="server">
void Buton1_Click(Object sender, EventArgs e)
{
Label1.Text = "Clicked at" + DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick ="Button1_Click"
Text="Click Me!" />
</div>
</form>
</body>
</html>
The code-behind file uses the partial class rather than the main class and it does not inherit the
.aspx file, where as both the files are combined together during compilation to implementation
the complete application.
Listing
CodeBehindExample.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
CodeBehindExample.aspx.cs
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Clicked at " + DateTime.Now.ToString();
}
}
Attributes Description
AutoEventWireup the Boolean value that enables or disables Page events that are
being automatically bound to methods; for example, Page_Load
The Import directive imports a namespace into a web page, user control pate of application. If
the Import directive is specified in the global.asax, then it will apply to the entire application. If it
is in a page of user control page, then it would apply to that page or control. The basic syntax
for an Import directive is:
Attributes Description
Src the path to the source file to be linked and compiled dynamically
Compilation in ASP.NET
ASP.NET first compiles the code into one or more assemblies that are nothing but the files with
the extension .dll. When the code is compiled, it is converted into a language independent and
CPU- independent language called Microsoft Intermediate Language(MSIL).
Structure of an Application
The structure of the web application includes the concepts of application domain, application
lifetime, and application directory structure. Application domain is a virtual boundry inside
which an application runs, while application lifetime is the span of time for which an application
domain exists. Application directory structure , on the other hand, specifies the directory
structure that organizes the various entitites associated with an application , such as
references, resources and code files in separate directories.
To understand the concept of domain, we need to understand how an web application works
after it is deployed. We deploy a web application created in ASP.NET on a webserver, such as IIS
the client browser sends a request for the Web application to the Web Server, which is further
then passed to the ASP.NET worker process. The worker process then executes various pages in
their respective domains.
Therefore the application domain provides an isolation boundary for applications, which is
required to ensure the code is running in one application is not affected by another application.
Application lifetime refers to the time span for which an application domain persists.
• Bin – contains compiled .NET assemblies, containing precompiled web pages and web
service classes.
• App_Code – Contains source code files,compiled dynamically ,to be used in the ASP.NET
web application
• App_GlobalResources – Contains the resources that can be used in all Web pages as
well as the controls of the ASP.NET Web application.
• App_LocalResources -- Contains the resources that are accessible only to a specific Web
Page of the ASP.NET Web application.
• App_Data – Contains the database and XML files of the ASP.NET Web Application.
• App_Browser – Contains browser definition files that identify browsers for different
ASP.NET Web Application.
Session_Start() – fired when session starts on each new user requesting a page.
Using States
State is quite an innovative concept in the web development because it eliminates the
drawback of losing state data due to reloading of a web page. By using states in a web
application you can preserve the state of the application either at the server or the client end.
The state of a web application helps you to store the runtime changes that have been made to
the web application. There are various methods for storing state information. These include the
following:
1. Hidden fields: Refers to the control that is not visible when a web application is
viewed in the browser.
2. Cookies: Refers to the text files that store data, such as user ID and preferences at
the client end.
3. Query Strings: Refers to the information strings added at the end of a URL to
maintain the state of a web application.
The following are some of the methods used to save state information on the web server
1. Application State: Stores application data not frequently modified by users. An object of
the HttpApplicationState Class is used to store the state of an ASP.NET Web
Applications.
2. Session State: Stores information specific to a user session. User session refers to the
duration for which a user uses a web site. An object of the HttpSessionState class is used
to store the session state for each ASP.NET web application that is executed.
Application State
Application state is used to store data corresponding to all the variables of an ASP.NET web
application. The data in application state is stored once and read several times. Application state
uses the HttpApplicationState class to store and share the data throughout the application. You
can access the information stored in an application state by using the HttpApplication class
property. Data stored in application state is accessible to all the pages of the application and is
the same for all the users accessing the application. The HttpApplicationState class provides a
lock method which you can use to ensure that only one user is able to access and modify the
data of an application at any instant of time.
Session State
Each client accessing a Web application maintains a distinct session with the web server and
there is also specific information associated with each of the sessions. Session state is defined in
the <sessionsSate> element of the Web.config file. It also stores the data specific to a user
session in session variables. Different session variables are created for each user session. In
addition, session variables can be accessed from any page of the application. When a user
accesses a page a session ID for the user is created.
The session ID is transferred between the server and the client over the HTTP protocol using
cookies.
View State
View state stores page – specific information when a page is posted back to the server. When a
page is processed, the current state of the page and its control is hashed (transforming a
sequence of characters into a fixed length value) into a string and save as a hidden field on the
page. Such a state of the page is called view state.
The ViewState property is used to save the view state of each control used in a page. If the
ViewState property is not used, when the values written in the controls are not retained when
the page is reloaded
View state is maintained in a web page by default. You can include the following directive at the
top of the web page to disable its view state.
To disable the view state of web control, add the following attribute to the control:
Page.EnableViewState = “false”
Listing Example:
Let’s create a sample web application, StateImplementation, which uses all the three state
types—application, session and view. After creating an empty web site follow the steps:
Global.asax File
<%@ Application Language="C#" %>
<script runat="server">
</script>
Default.aspx File
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
font-weight: bold;
text-decoration: underline;
}
</style>
</head>
<body>
</div>
</form>
</body>
</html>
HTTP Handlers
Examples: ASP.NET uses different HTTP handlers to serve different file types. For example, the
handler for web Page creates the page and control objects, runs your code, and renders the final
HTML.
Postback
PostBack is the name given to the process of submitting an ASP.NET page to the server for
processing. PostBack is done if certain credentials of the page are to be checked against some
sources (such as verification of username and password using database). This is something that
a client machine is not able to accomplish and thus these details have to be 'posted back' to the
server.
Postback includes cross-post-page posting, which is the name given to the process of receiving
the response of a request by the server on another page.
Questions:
1. What is ASP.NET?
2. Define Postback.
3. What are Http Handlers?
4. What is a state? Explain various methods used to save state information?
5. Write a short note on Global .asax file.
6. Explain various methods of Global.asax File.