Unit 2 ASP.net Web Application
Unit 2 ASP.net Web Application
NET Technologies
(01CE1602)
6th Semester
4 Credits
Prof. Ravikumar R N
Dept. of Computer Engineering
Net Technologies are blend of technologies supported
by Microsoft .Net Framework that allows user to create
Objectives various applications.
Students will be able to work with various technologies
provided by Microsoft .NET platform.
To Review the components of .Net Framework
To practice Web based application
Course To create web applications using MVC framework
Outcomes To practice basic database application using ADO.net
To designing, developing, and deploying APIs
Unit 2
ASP.NET Web Application
ASP.Net Web Application
Page life cycle of ASP.NET Application
Web Controls (Button, TextBox, CheckBox, Image etc.)
Rich Controls (Calendar, AdRotator)
Contents
Validation Controls
State management
Cookie
Session
ASP stands for Active Server Pages.
ASP is a development framework for building dynamic web
pages.
ASP and ASP.NET are server side technologies.
Both technologies enable computer code to be executed by
an Internet server.
When a browser requests an ASP or ASP.NET file, the ASP
ASP.NET engine reads the file, executes any code in the file, and
returns the result to the browser.
ASP.NET was released in 2002 as a successor to Classic ASP.
ASP.NET pages have the extension .aspx and are normally
written in C# aka (C sharp).
Latest version 4.8
ASP.NET Web Pages is an SPA application model
(Single Page Application).
A Single-Page Application is an app that doesn't need
to reload the page during its use and works within a
ASP.NET browser.
Think of the apps you use daily: Facebook, Google
Maps, GitHub etc.
The SPA model is quite similar to PHP and Classic ASP.
When an ASP.Net page is called, it goes through a
particular lifecycle. This is done before the response is
sent to the user.
ASP.Net Page
Lifecycle
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
ASP.Net Page and Response object are created. The Request object is used
to hold all the information which was sent when the page was
Lifecycle 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
ASP.Net Page page is displayed again. In such a case, the Postback event
Lifecycle 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 (mirror).
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
First Web
Application
First Web
Application
First Web
Application
First Web
Application
First Web
Application
First Web
Application
First Web
Application
First Web
Application
Set as start page
Build and Run
First Web
Application
Internet Information Services (IIS) is a flexible, general-
purpose web server from Microsoft that runs on
Windows systems to serve requested HTML pages or
files. Latest version 10.0 released in 2018.
An IIS web server accepts requests from remote client
computers and returns the appropriate response.
What is IIS? It enables web servers to deliver, transfer information
across an array of Local Area Networks (LANs)
including corporate intranets.
It has the ability to share information with the user in
numerous forms such as text documents, HTML
webpages, and images.
Server Controls
Label
TextBox
Button
RadioButtonList
LinkButton
Image
ImageButton
Panel
Hyperlink
Web Controls DropDownList
PlaceHolder
Calendar
ListBox
AdRotator
DataGrid
Table
DataList
XML
CheckBox
Literal
CheckBoxList
RadioButton
This control is used to display textual information on
the web forms. It is mainly used to create caption for
the other controls like: textbox.
To create label either we can write code or use the drag
and drop facility of visual studio.
</p>
<br />
</form>
<asp:Label ID="userInput"
runat="server"></asp:Label>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication6
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
Code Behind {
C# }
<body>
DataGrid <form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"></asp:GridView>
</div>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("ID");
table.Columns.Add("Name");
table.Columns.Add("Email");
table.Rows.Add("101", "RK KEYNOTES", "[email protected]");
GridView1.DataSource = table;
GridView1.DataBind();
DataGrid }
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack)
LoadGridData();
}
private void LoadGridData() {
DataTable table = new DataTable();
table.Columns.Add("ID");
table.Columns.Add("Name");
GridView with table.Columns.Add("Email");
namespace WebFormsControlls {
public partial class WebControls : System.Web.UI.Page {
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected void Page_Load(object sender, EventArgs e) { }
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
File Upload string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
FileUploadStatus.Text = "The file has been uploaded.";
}
catch (Exception ex)
{ FileUploadStatus.Text = "Error: " + ex.Message; } }
else { FileUploadStatus.Text = "Please select a file to upload."; } } }
}
Demo
ASP.NET provides implicit object Response and its methods
to download file from the server. We can use these methods
in our application to add a feature of downloading file from
the server to the local machine.
<body>
<form id="form1" runat="server">
<div><p>Click the button to download a file</p>
Download File
<asp:Button ID="Button1" runat="server"
Text="Download" OnClick="Button_click" />
CheckBox
</div>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
You Selected: <asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</form>
protected void Page_Load(object sender, EventArgs e)
{ Label1.Text = "";
}
protected void Button1_Click(object sender, EventArgs e) {
var message = "";
if (CheckBox1.Checked)
{ message = CheckBox1.Text + " ";
CheckBox }
if (CheckBox2.Checked)
{ message += CheckBox2.Text + " ";
}
if (CheckBox3.Checked)
{ message += CheckBox3.Text;
}
Label1.Text = message;
} }
CheckBox
The AdRotator is one of the rich web server control of
asp.net. AdRotator control is used to display a sequence of
advertisement images as per given priority of image.
AdRotator control displays the sequence of images, which
is specified in the external XML file. In a xml file we indicate
AdRotator the images to display with some other attributes, like
image impressions, NavigateUrl, ImageUrl, AlternateText.
In a Adrotator control images will be changed each time
while refreshing the web page.
cookie?
How to clear the cookie information?
userInfo.Expires = DateTime.Now.AddHours(1);
Advantages of Cookie
It has clear text so the user can read it.
We can store user preference information on the client
machine.
Advantages It is an easy way to maintain.
and Fast accessing.
Disadvantages Disadvantages of Cookie
If the user clears the cookie information, we can't get it back.
No security.
Each request will have cookie information with page.
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"
Form Text="Label"></asp:Label>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
//-- Creating Cookie --//
HttpCookie cookie = new HttpCookie("student");