0% found this document useful (0 votes)
21 views

C# -ASP.NET

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C# -ASP.NET

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Introduction to ASP.NET: ASP.NET life cycle, exploring ASP.NET 4.

5
web application, creating a sample ASP.NET 4.5 website,
application structure and state, global.asax application file, web
forms – standard controls, validation controls, master pages, web
services.
INTRODUCTION TO ASP.NET:​

Overview:
-​ Asp.net is one of the most powerful web application development
platforms.
-​ It is becoming more useful & popular with the release of upgraded
versions.
-​ Asp.net 4.5 includes several enhancements in ASP.NET core, run
time & framework and ASP.NET web forms & contain a new version
of MVC(Model, View, Controller) named ASP.NET MVC4.
-​ The new Javascript, HTML & CSS editors are also integrated with
visual studio 2012 to simplify the web appln development.
-​ Asp.net provides services to allow the creation, development &
execution of web appln & web services.
-​ Asp.net is a server side technology.
-​ Web aplns are built using web forms, Asp.net comes with built-in
web forms controls, which are responsible for generating the user
-​ Web forms are designed to make building web-based applications
easy.

❖​ Describing the ASP.net life cycle:


-​ The Asp.net life cycle starts when a client requires a page from the
web server. The Asp.net life cycle shows how Asp.net processes
the web pages to produce dynamic output. It also shows how the
appln & individual pages are instantiated & processed.
●​ Life cycle of an Asp.net appln on IIS 7.5 phases of lifecycle of
asp.net appln:
1.​ User requests for an appln resource.
2.​ The unified pipeline receives the 1st user req.
3.​ For each requests response objects are created.
4.​ An object of HTTPApplication class is created & allocated to the
req.
5.​ The HTTPApplication class processes the user req.
●​ Lifecycle of an Asp.net web pg:
When a user reqs for an Asp.net web pg, the pg passes through a
no.of stages that is collective by called the lifecycle of an Asp.net
web pg. For ex the lifecycle of a pg begins when a user reqs for a
web pg & ends when the pg is rendered completely to the browser.
●​ Diff stages of an Asp.net web pg:
1. pg request: when the user requests for the pg for 1st time,
Asp.net ascertains whether request is new or old one. If the
request is new the pg is compiled & executed. If the request is old
one, then a cached copy of the pg is sent without executing the
pg.
2. start: refers to stage in which request & response properties of
the pg are configured. The pg also ascertains whether the request
is an odd or new one by using ISPOSTBack property.
3.page initialization: refers to the stage in which each control on
the pg is assigned a unique ID by selling unique ID property.
4. load: if the req is an odd one , then the properties of controls on
the pg are loaded with the data from the view statement & control
statement.
5. postback event handling: if the request is odd one , then any
event handles can be involved. After that, the validation controls
used on the pg invoke validate ( ) method.
6. Rendering: the view state & all the controls on the pg are saved.
While rendering, runtime environment invokes render ( ) method
for each control on the pg & writes the output of rendering stage
to o/p stream obj.
7. unload: where response & request properties of the pg are
unloaded & any cleanup op if required is performed.
●​ Asp.net pg lifecycle events:
-​ At each stage of life cycle of a pg, the pg raises some events that
can be handled.
-​ The following are the most common pg events.
1.​ Preinitializing- refers to 1st event that occurs during the lifecycle of
Asp.net web pg.
-​ It is used to check the ISPostBack property of the page to delete
whether the page is being processed for the 1st time.
-​ It also creates/ recreates dynamic controls.
-​ It sets the theme property & the master pg dynamically. It gets &
sets the profile property values.
2.​ Init- refers to the event that is raised after the initialization of all
the controls & applying skin settings. It is used to read/ initialize
control properties.
3.​ Load: refers to on load ( ) event method that is called to set the
properties of controls &establish DB connections.
-​ The pg class 1st calls the on load ( ) event method on the web pg
& then calls the event for each child control, until the pg is loaded.

❖​ Creating a sample Asp.net web appln:

In Asp.net we can create both web applns& web sites.


●​ Diff b/w a web application & a website:

area Web application Website


Structure of Stores information Consists of no
project file about the list of files project file. The
that are included in files are included
ASP.NET project automatically in
website
Compilation Requests you to Dose not required
build a project in VS. manually compile
The classes& the project.
standalone class Website is
files are compiled complied
into single dynamically by
assembly& placed in ASP.NET, which
BIN folder produces multiple
assemblies
namespaces Added to pages, Needs to be added
controls& classes by manually
default
Deployment Request to copy the Request you to
assembly i.e., copy the project
created while source to an IIS
compiling the server
project in an IIS
server

●​ Steps to create a web appln:


1.select start→ program→ ms vs.
2. select FILE→ new→ program option.
3. select web→Asp.net web forms appln.
4. enter the name of appln in name textbox.
5. enter a loc to save appln in location combo box.
6. click OK button.
( web appln with Default.aspx file ( known as source file) appears).
●​ Diff components of VS IDE:
1.​ toolbox: contains no.of tabs where each tab contains a list of
controls that can be dragged & used on a web form
( if it is not visible , select view→ toolbox or press ctrl+alt+x).
2.​ server explore: displays a list of available servers from which data
can be accessed through the n/w
( visible by selecting view→ server explorer or press ctrl+alt+s).
3.​ code editor window: recognizes keywords & identifies of PL. Check
syntax errors
-​ U can access the code editor by double clicking the control or
webform or pressing F7 key in design mode provides intellisense
feature.
4.​ toolbars: provides an easy access to the features of VS.
5.​ solution explorer: provides a conceptual overview of a proj, which
makes managing project elements,∍ class & web forms, easier.
-​ The proj appears in a tree view format, showing nodes & child
nodes.
-​ ( select view→soln explorer or press ctrl+alt+L).
6.​ properties window: makes the task of setting properties of controls
& web forms very easy.
-​ select. view→ properties) or F4.
7.​ view tabs: allows u to see diff layouts of webform ( design tab→
shows design, source tab shows code, split tab → shows both
design & source).

❖​ Creating Asp.net website:

Open VS→ select file → New→website enter a location & name in


the web loc combo box, to save web site click OK.
I.​ Asp.net website : creates a new website, which includes the
following components.
-​ Different folders that contain the membership pgs, images, client
script files & .css files.
1.​ A data folder
2.​ A master page file
3.​ 3 web pages: Default.aspx, About.aspx, Contact.aspx
4.​ A global appln file
5.​ Global.aspx
6.​ A web.config file
II.​ Asp.net empty website: creates a new website that includes only
web.config file. U need to add pages, files & folders manually to
create a simple website.
-​ After u click OK button, default.aspx file opens. Open code- behind
file by pressing F7
-​ Add the code in code- behind file.
Protected void page- load(---) {
Response.write(“Hello world”);
}
-​ Press F5 to execute.
●​ Specifying a loc for a web appln
-​ VS allows us to create a web appln. With a virtual dir mapped to
IIS server(internet info services) or a stand-alone appln outside
boundaries of IIS server.
-​ VS allows us to store in following 3 locations
o​ File system.
o​ HHTP.
o​ FTP.
1.​ File system: file system website are not associated with any IIS
appln, unless u create IIS virtual direct.
-​ U can run these website by using built-in web server, which is
executed at runtime.
-​ The built-in web server allows u to run web applns even if IIS is
not configured on u’r system.
-​ u can verify the existence of built-in web server by select.
file→New→website option in VS IDE).
-​ U can change the default path of we appln by clicking browse
button of New web site dialog box. The choose location dialog box
appears, with File system tab selected.
2.​ HTTP: VS allows u to develop & store web appln on IIS server
through HTTP option, either in local IIS server, remove IIS server.
-​ In choose location, select local IIS tab to store web site files on
local IIS server. It shows a list of all virtual appln roots on u’r
system.
-​ Very important: u can also create a new virtual direction for u’r
web appln.
-​ Steps to create new virtual direction:
1)​Select local IIS tab in choose location dialog box.
2)​Select the default web site node in choose location dialog box.
enables 2 options a. Create new web appln b. Create new virtual
direction (these option are available at top right hand corner of
choose location).
3)​Click create New Virtual direction (opens new virtual direction
dialog box)
4)​Enter a name for new virtual direction in Alias name text box.
5)​Enter a location, where u want to save new virtual direction in
folder text box.
6)​Click OK (new virtual direction is added to choose location dialog
box).
7)​Click open button in choose location dialog box. (the new direction
is selected in combo box besides the browse button in new web
site dialog box).
8)​Click OK button in new web site dialog box to create a new web
appln.
-​ (when u invoke u’r web appln, the URL , http://localhost/New
virtual direction/deflaut.aspx appears).
●​ Remote IIS server
-​ Remote site option stores files on remote server that is accessible
over a local network. VS user HTTP protocol to host remote web
site on remote IIS server.
1)​Select Remote site tab in choose location: dialog box.
2)​Enter a location of web site in website location text box.
3)​Click open button.
-​ Remote server location is added to new web site dialog box.

3. FTP: enable u to store & code your applns, which reside on a


server. We can use FTP feature over TCP to work on diff locations,
provide server name, port to use, direction & other info.

●​ File Types in ASP.net:


-​ Web applns contain many file types, which are supported &
managed by ASP.net or IIS server, which can be automatically
generated using ADD New Item dialog box.

File location Description


type
.asax Application Refers to global.asax file.
root Reprsents the web
application& contains
optional methods that run at
start or end of application
.asmx Application Refers to XML web service
root or file containing classes &
sub methods that are available to
directory other web application
through SOAP
.aspx Application Refers to ASP.NET webforms
root or containing web controls,
sub presentation& business logic
directory

❖​ Exploring ASP.net web pages

-​ Files with .aspx extension – contains code to implements a web


appln. These web files are stored on IIS virtual dir.
-​ When client require is received by IIS server, the required pages is
searched for & info is sent back in response object to render HTML
markup on client browser.
-​ Web appln is 1st compiled & converted into MSIL code.
-​ 1st time .aspx file compiled to generate the class file subsequent
requires process class file.
-​ Reduces response time.
-​ After creating the appln add default .aspx file by
1)​Right click the project name in solution-explorer & select Add→Add
New Item option.
2)​Select web form option.
3)​Enter name of the web form(default.aspx)
4)​Click ADD button
5)​Write the code.
<div>
<h1> welcome to world of ASP.net</h1>
</div>
-​ 1st line of ASP.net page is enclose b/w <% and %> tags. These
blocks also known as code render blocks contain info about code→
lang:used.
6)​Press F5 to execute.

●​ Code render blocks:


-​ Define inline code that are executed when a web page is rendered.
Tis code is executed by the server output is displayed on browser.
<form id= “form1” run at= “server”>
<div>
<% for (int i = 0; i<=6; i++){ %>
<font size= “<%= i %>” > welcome to ASP.net>
</font>
<% } %>
</div>
</form>
●​ ASP.net coding models:
-​ Generally a web form consists of controls ∍ labels, buttons &
business logic. Use ASP.NET coding techniques to manage these
controls & business logic.
-​ 2 types of coding techniques.
1)​Single – file page model.
2)​Code – behind page model.
1.​ Single – file page model: write code directly in .aspx page of appln.
Disadvantage – difficult to read web pages that mix presentation
(HTML) with functionality (code). Programming code is written in a
script block that contains the attribute run at = “server” to mark it
as code that ASP.net should execute.
<asp; Label 1 id = “Label 1” run at= “server”> </asp;Label>
<asp; Button id= “Button 1” run at= “server” On Click= “Button1 –
click” Text= “Click Me”/>
<script runat= “server”>
Void Button 1 – click (OS, EA, e) {
Label 1. Text= “clicked at” + Date Time. Now. To String
}
</script>
2.​ Code – behind page model:
-​ There are 2 separate files.
(Default.aspx) -(for presentation tags) &​
(Default .aspx .cs) (to implements programming language
logic)
-​ These 2 files are linked together to run web appln.
-​ Default .aspx
→add label, button
-​ Default .aspx .cs
Button1_ click(- -)
Execute by pressing F5
●​ Working with server controls:

-​ In addition to HTML & programming code, ASP.net web page


contains server controls ∍ Text box & Image controls.
-​ Server controls are programmable objects that act as UI elements
on a web page.
-​ The code for a server control is executed & complied at server.
These controls are also known as “server side objects”.
-​ We can define their behaviour by setting their properties.
-​ 1. By explicitly writing properties in server control tag. Or
-​ 2. By defining properties in properties windows.
-​ U can use server control tags or intrinsic HTML tags containing the
runat= “server” to declare server controls in a web appln.
-​ The server controls declared within the intrinsic HTML tags are
executed with the help of System. Web. UI. HTML controls
namespace.

●​ Intrinsic controls:
-​ Are HTML elements that gather user info. They are specified in ASP
namespace. They can create intelligent controls that automatically
maintain standard & provide extra features.
<asp: Button> <asp: Label> <asp: Image> <asp: Hyper
Link>
<asp: Text Box> <asp: Table> <asp: Radio Button> <asp:
List Box> <asp: Table Row>
●​ Diff categories of web server controls:
1.​ Standard controls(Intrinsic – link HTML)
Ex: Label, Text box, Check box.......
2.​ Data controls
Ex: grid view, data list, repeater....
3.​ Navigation controls
Ex: tree view, menu.....
-​ Syntax:
<asp: control name id= “ ” runat= “server”...>
</asp: control name>
●​ Web form is providing 3 views
1.​ Design view: this will display preview of web page output &
supports arrangement of controls using drag & drop.
2.​ Source view” this will display markup content (tags) of web page –
allows creating controls manually.
3.​ Logic view” allows developer to write logic part of web page.

❖​ Application structure & state:

-​ The structure of a web appln includes appln domain, appln lifetime


& appln direct structure.
-​ A web appln also contains Global .asax file, which handles the
events or methods that are raised when the appln is executed.
-​ ∴ The structure of an ASP.Net web appln comprises files, pages
handlers & executable code, which are invoked from the virtual
directs of a web server.
●​ Structure of an application:
-​ Structure of an ASP.net web appln includes the concept of appln
domain, appln lifetime & appln directory structure.
-​ Appln domain is a virtual boundary inside which an appln runs.
-​ While appln lifetime is the span of time for which an appln domain
exists.
-​ Appln directory structure specifies the dir structure that organizes
the various entities associated with an appln → refers, resources &
code files in separate directories (each dir possesses a
well-qualified name)
1.​ The Appln Domain:
-​ We deploy a web appln created in ASP.net on a web server, ∍ IIS &
Windows Activation Services(WAS).
-​ The client browser sends a request for the web appln to web
server, which passes the request to ASP.NET worker process.
-​ Worker process then separates the code execution into diff appln
domains with the web pages & web services hosted on same
virtual dir.
-​ These web services & web pages execute in same appln domain,
execute in diff appln domains.
-​ An appln domain is actually implemented by (CLR) & prime
objective of appln domain is to prevent concurrently running
applns from entering into each other’s domain.
∴ Appln domain provide an isolation boundary for applns, which is
required to ensure that the code running in one appln dose not
affect any other or unrelated applns.
-​ The most vital benefit of appln domain is the independent
execution of web applns.
-​ The main component that helps in the independent execution of
applns is virtual dir some of the primary appln entities that are
stored in virtual dir are.
-​ Web pages: files that are used as web form pages in MS .net
environment. – extension is .aspx.
-​ Web services: files that enable u to share info with applns on other
computers & platforms using XML web services. – extension .aspx.
-​ Code behind files: source code files that contain code for ASP.net
page in a separate class file. - .cs/.vs for c#.vb projs.
-​ Config files: config which stores config settings of a web appln &
machine.config – stores config settings of machine.
-​ Global .aspx files: files that contain event handlers to respond to
events ∍ starting of an ASP.net web appln for 1st time.
●​ Appln Life time:
●​ refers to time span for which an appln domain persists (it does not mean
that appln life time is equivalent to time span for which an appln runs).

-​ An appln domain can shut down in several circumstance including


specific errors conds, config changes or web server shut down.
-​ ∴ , an applns runtime might encounter repeated restarting of
appln domain.
-​ The appln domain might require a restart under following
circumstances.
1.​ Excess requires in queue.
2.​ Excess memory usage by an appln.
3.​ Lengthy life time of the appln.
4.​ Modifications in web .config file.
5.​ Replacement of .dll / web files.

2. Appln dir structure: plays a key role in web appln development.


It segregates all code & resource used by appln among diff
directories, thereby enhancing the productivity of developers.
▪​ Advantage: code can be developed independently by diff team
memory’s in diff modules. Debugging process can also be
performed in a more structured manner, it enhances the reusability
of an appln. It becomes easy to upgrade the appln in long run.
-​ In 1.x version of ASP.net & earlier there was only one default dir &
bin for an appln. However, in ASP.net 4.0 & later versions, there is
support for default dir struct, which provide several built-in dirs for
web applns. These dirs are.
1.​ Bin: contains compiled .net assemblies, containing precompiled we
pages & web service classes. U can directly use these assemblies
in u’r appln , thereby reducing the amount of code that needs to
be written.
2.​ App_code: contains source code files, complied dynamically, to be
used in ASP.net web appln.
3.​ App_Global Resources: contains the resources that can be used in
all web pages as well as controls of web appln.
4.​ App_Local Resources: contains the resources that are accessible
only to a specific web page of an appln.
5.​ App_web references: contains the references to the web service
used by ASP.net web appln.
6.​ App_Data: contain the DB & XML files of ASP.net web appln.
7.​ App_Browsers: contains Browser def files that identify browsers for
ASP.net web applns.
8.​ App_Themes: “the themes used by ASP.net web appln. The
themes can be applied to a control or a web page or entire ASP.net
web appln.

❖​ Global .asax application file:

-​ Resides in root dir & also called ASP.net appln file.


-​ contains the code that is executed when certain events ∍ start of
an appln or errors in appln, are raised.
-​ Events & states ∍ session state & appln state, which are specified
in Global.asax file are applied to all resources of web appln.
-​ For ex, if an appln state variable is defined in Global.asax file, then
all .aspx files within root dir can access the variable.
-​ Various methods corresponding to events that occur in Global.asax
file are invoked in the following order, when a web appln starts
1.​ Begin Request( ): invoked when a require for web appln is
received.
2.​ Authenticate Request( ): invoked just before the authentication of
user credentials is performed.
3.​ Authorize Request( ): invoked when the current users credentials
are successfully validated (used to assign special permissions to
user).
4.​ Resolve Request Cache( ): invoked when the ASP.net page
framework completes the execution of authorization req. (used to
handle output caching of appln that renders cached HTML without
executing the code again.
5.​ Acquire Request State( ): invoked just before session info is
retrieved for the current client req.
6.​ Pre Request Handler Execute( ): invoked before ASP.net page
framework begins to run HTTP Handler to handle a req.
7.​ Post Request Handler Execute( ): invoked after HHTP Handler has
finished executing the req.
8.​ Release Request State( ): invoked just before session info is
serialized from session collection for next req.
9.​ Update Request Cache( ): invoked just before info for handler req
is added to output cache.
10.​ End Request( ): invoked at the end of req.
-​ The methods that are not invoked with user require
Application_Start( ), Session_Start( ), Application_Error( ),
Session_ End( ), Application_End( ), Application_Disposed( ).
-​ Global.asax file is used to manage the appln & session level state.
-​ There are some similarities b/w an ASP.net page & Global.asax file.
-​ 1. Similar to ASP.net page, Global.asax file is complied when a
request for any resource in an ASP.net web appln is made for 1st
time.
-​ 2. When changes are made Global.asax file, ASP.net framework
detects the changes & recompiles the file. New requires are then
directed to recompiled Global.asax file.
-​ The code in Global .asax file is written in the same way as in web
forms. The only diff is the code written in Global .asax file dose not
contain HTML/ ASP.net tags. The code in this file contains method
with predefined names.
-​ The Global .asax file is created either in Notepad or as a compiled
class which is displayed as an assemble in ASP.net web appln.
-​ Ex: create an appln, named Global, which uses Gloabl .asax file to
display date & time.
1.​ Right click the proj name in solution explorer & select Add→Add
New Item.
2.​ Select web form template.
3.​ Click Add (adds default .aspx file).
4.​ (to add Global .asax file). Right click name of web site in solution
explorer.
5.​ Select Add→Add New Item option.
6.​ Select Global Appln class template, then click Add button.
7.​ Add the following code.
<script run at= “server”>
Void Application= On End Request (OS, EA, e)
{
Response write(“this page was executed on:”+ Date Time.
Now. To String( ));
}

❖​ Using states:

- by using in a web appln, u can preserve the state of appln either at server/
client end. The string of web appln helps u to store runtime changes that have
been made to web appln.

-​ There are various methods to store string info.


1.​ Hidden fields: not visible when a web appln is viewed. The content
of this control is sent to HHTP form collection control, alonge with
other controls to the server – acts as a storage area for any page
specific info.
2.​ Cookies: text files that store data at client side when a browser
requires a web page again, cookie is sent along with require.
3.​ Query strings: refers to info strings added at the end of a URL (to
maintain string of web appln – not secure because their values are
exposed.
-​ The following methods are used to save string info.
1.​ Appln state – stores appln data not frequently modified by user.
2.​ Session state – stores info specific to user session.
3.​ Profile properties – store user specific data in a persistent form.
-​ Various state types:
1.​ Appln state – stores data corresponding to all various of ASP.net
web appln.
2.​ Session state – specific info associated with each session.
3.​ View state – stores page specific info.

❖​ Web forms – standard controls


-​ The web form controls are designed to resemble standard windows
formed controls.
-​ These controls are used to design the interface of any web appln u
can access all these controls from Tool box of IDE.
-​ These controls can be easily used by dragging & dropping them on
web form. All these standard controls are derived from control
class.
-​ control→web control→web server control.
-​ The control class:
o​ System.Web.UI.Control is base class for all web server controls.
-​ Some public properties of control class:
o​ ID, Parent, Rendering Compatibility, Skin ID, Template Control
Visible etc.
-​ Public methods of control class:
o​ Apply Style Sheet Skin, Data Bind, Dispose, Render Control.
-​ Public events of control class:
1.​ Data Binding – occurs when a server control is attached to a data
source.
2.​ Disposed – refer to last stage of lifecycle of web server control &
occur when server control is released from memory.
3.​ Init – occur when server control is initialized.
4.​ Load – occur when server control is loaded.
5.​ Pre Render – occur when server control is about to render.
6.​ Unload – occur when server control is unloaded from memory.
-​ Web control class: is a base class containing methods properties &
event common to all web controls.
-​ Properties:
o​ Access key (allows u to quickly navigate to the control), Backcolor,
Border color, Border Style, Border Width, Font, Forecolor, Height,
Skin ID, Style etc.
-​ Public methods:
o​ Apply Style, Merge Style, Copy Base Attributes, Merge Style,
Render Begin Tag, Render End Tag.
-​ Basic standard controls of web control class:
o​ Label, Button, Text Box, Literal, Place Holder, Hidden Field, File
Upload, Image, List Box, Hyper Link, Check Box, Radio Button,
Table, Wizard etc.
1.​ Label:
<asp: Label ID= “Lable1” run at= “server” Text= “Label”>
</asp: Label>
-​ U can add a label control to web page either by dragging &
dropping it from standard tab of Tool box or by double – clicking it
in the Tool box.
-​ Default .aspx .cs file:
Protected void page-load( )
{
Label2. Text= “welcome to world of .Net 4.5”;
Label3. Text= “ou”;
Label3. Back color= System. Drawing. Color. Sky Blue;
}
2.​ Button control: click event
3.​ Text box: text changed
4.​ Literal control: similar to label control is another web server control
used to display text on a web page u can add HTML code in a web
page with the help of Literal control.
5.​ Place Holder control: used as a container to store server controls
that are added to a web page at run time. This control does not
produce any visible output & is used only as a container for other
controls on web page.
o​ Ex: Place Holder1. Controls. Add(Text box1);
6.​ Hidden Field Control: used to store a value that need to persist
across posts to the serer. Normally, view state, session state &
cookies are used to maintain the string of a web page. If they are
disabled/ not available u can use Hidden field control to store state
values. Add hidden field, button, text box to web page.
Page_load( ) {
Hidden Field1. Value= “welcome – Hidden Field”;
}
Button1 – click( ) {
Label2. Text= Hidden Field1. Value;
}
7.​ File upload control: displays a text box & a browse button that
enables u to browse a file from local or remote machine to upload
it on a web server.
8.​ Image control: standard web server control, which is used to
displays an important on a web page.
9.​ Image Button control: is a button control that displays an image
instead of text.
10.​ Image Map control: is a pic on a web page that provides various
links, called hotspots, to navigate to other web pages, depending
on the place where the user clicks.
11.​ List box control: is a std web server control used to select 1 or
more items from a list of items on a web page at run time.
12.​ Drop- Down List control: display the list of data as a drop- down
list from which u can make a single select.
13.​ Bulleted list: used to display items in the from of bulleted list on
a web page.
14.​ Hyper Link: is used to create a link to another web page.
15.​ Link Button control: used to link the current web page to some
other web page.
-​ Similar to Hyper Link control.
-​ Different is: Hyper Link control allows the browser to navigate to a
new web page; Link Button – we can also perform some other
action by handling click & command events of this control.
16.​ Check Box control: creates a check box that can be selected by
clicking it.
17.​ Radio Button: creates a single Radio Button.
18.​ Table control: helps to create a table – useful when u want to
present data in a tabular format u can create a tab at design/ run
time, depending on requirement.
19.​ Wizard control: provides navigation & a UI to collect related
data across multiple steps. It allows u to implements linear/ non
linear navigation through the wizard steps.
U can also create user/ custom controls according to the
requirements.

❖​ Validation controls
-​ Are the controls used for validating the data entered in an input
control ∍ Text Box.
-​ When a user enters data on a web page & submits the page to the
server, validation controls are invoked to check the data entered by
user.
-​ If any of the data is invalid the validation control displays an error
message on the screen.
-​ The error message is defined as a property value of validation
control.
-​ Ensuring proper input from user is called validation.
-​ Ex: user name cannot be blank
Mobile number requests 10 digit nos.
-​ Validation controls can be used to validate user input at client side/
server side, default is client side validation controls will provide
built- in code to perform diff types of validations, this makes
developer job easier & faster in web site development – save time
& enhances efficiency.
-​ (from asp.net 3.0 onwards validation controls script is not visible to
developer)
●​ Validation controls
1.​ Required Field Validator control.
2.​ Range Validator control.
3.​ Compare Validator control.
4.​ Regular Expression Validator control.
5.​ Custom validator control.
6.​ Validation summary control.
●​ Comman properties of validation controls:
1.​ Control to Validate: specify std control id to validate user inpute.
2.​ Text: specify error message to be displayed if user provides an
invalid input.
3.​ Error message: specify error message to be displayed with
validation summary control.
4.​ Enable client script – true/ false
True [default] – client side validation
False - server side validation
●​ Base Validator class:
System. Web. UI. Web controls. Base Validator class provides basic
impl required for all validation controls.
1.​ Required Field Validator:
-​ Simplest controls – used to ensure that
the user has entered data into input
control.
-​ Can be used for mandatory fields.
-​ Ex: go to a website.
Add web form (default2 .aspx)
Add table, add 2 labels, 2 text boxer Required Field Validator

-​ Required Field Validator1 properties:


(user name can not be blank)
Control to Validate - ~ TB1
Text – enter user name
Error message – plz enter
user name

2.​ Range Validator:


-​ Checks whether or not a
value of an input control
is inside a specified range of values.
-​ Properties
Control to validate (data type must be specified
Error message - string, integer, double,
Type- integer date, currency)
Minimum value
Maximum value
-​ Ex: add web form
Name
Age Range Validator1
(age should be b/w 25 to 40)
Control to validate – TB 2
Min value – 25
Max value – 40
Text – invalid age

3.​ Compare Validator:


-​ Used to compare the value
entered by a user into one
input control with another
input control or a const
value using relational operator.
-​ Ex: 1. Age > 25
2. confirm password
●​ Properties:
1)​Control to Validate
2)​Control to compare or Value To Compare
3)​Operator
-​ Ex: user name
Password
Confirm password
●​ Compare validator1 properties:
-​ (confirm password should be same as password
Control to Validate: Text box 3
Control to compare: Text box 2
Operator: equal

4.​ Regular Expression Validator:


-​ Used to validate user input against a particular exp.
-​ Regular exps are used to check whether or not text matches a
certain pattern.
-​ Validation exp: specify an exp to validate user input
o​ \d {10} – exp to accept 10 digit no.
o​ *\d {1, 10} – exp to accept 1 to 10 digit no.
o​ *\d {1,-} – exp to accept any digit no.
o​ [A-Z a-z] {20} – exps to accept string with 20 chars only
alphabets.
o​ [A-Z a-z] {1}. [A-Z a-z| s] {1-20}
b. rama Krishna rao[name dot & space]
-​ Regular exp to det. If text matches a valid e-mail add, \w+([-
+]\w+)*@\w+([- .]\w+)*\.\w+([- .]\w+)* w- standes for word
char(∍ letter, -) and * sign matches the previous char or sub exp 0
or more times (Ex: Zo* matches Z & Zoo)
-​ ASP .net uses some pre built regular exps that can be used to
match well-known seqs of chars.
User name​ ​ ​ ​ Required Field Validator1
​​ ​ ​ ​ ​ Regular Expression Validator1
Email – id​ ​ ​ ​ ​ Regular Expression Validator2
o​ Required Field Validator1 properties:
[user name can not be blank]
Control to validate – text box1
Text – enter user name
Display – select dynamic
o​ Regular Expression Validator1 properties:
[user name requires proper format]
Control to validate – text box1
Validation expression – [A-Z a-z] {1}\. [A_Z a-z\s] {1,20}
Text – enter proper name
Display – select dynamic
o​ Regular Expression Validator2 properties:
[email-id requires valid format]
Control to validate – text box2
Text – invalid email-Id
Validation exp...........[click]
​​ ​ ↓
​​ Select Internet email.add
​​ ​ Or
Validation exp= “\w+([- +]\w+)@\w+([- .]\w+)*\.\w+([-
.])\w+)*”>

NOTE:

1. more than on validation control can be applied to a single text


box(std control).

2. One validation control can validate only one text box, it can not
validate more than one text box.

3. Text box applied with more than one validation control requires
setting display property.

-​ Static [default]: validation control will occupy space on the web


page irrespective of error is occurred or not.
-​ Dynamic: validation control will occupy space on the web page
irrespective if error is occurred to display error message.

5.​ Customer Validator control


-​ Is used to customize & impl data validation as per u’r requirement.
-​ The logic provided by developer to perform validation is called
“custom logic”.
-​ This control supports client side validation & server side validation.
-​ For creating your own validations use only Java Script.
Script tag must be in head tag.
<script>
Function f( ) {
Var KK=1^[0-9] {10-10}$\:
If (document get Element By Id(“t1”). Match) {
Alert(“correct”);
}
Else {
Alter(“wrong”);
Document. get Element By Id(“t1”).value= “ ”;
}
<\script>
<input type= “text” id= “t” value= “ ”>
<input type= “button” value= “submit” On click= “f( )”>
Inscript type= “text\java script”
T1= textbox

6.​ Validation summary control:


This can be used to display summary of error message at one
place.
-​ Properties:
1.​ Header text – specify heading to error messages.
2.​ Show summary – true\false
True[default] – error message will be displayed within browser
window.
3.​ Show message box – true\false [default]
true→error message will be displayed with in a dialog box, this is
supported only for client side validation.
-​ Ex:
Name​ ​ ​ Required Field Validator1
Mobile number​ ​ Required Expression Validator1
Validation summary1
❖​ Master pages:
-​ Earlier, each page of a web site had a diff style applied to it, which
made if diff for developers to provide a consistent look throughout
web site.
-​ A master page is used to incorporate visual inheritance in a web
site/web appln & define a common layout for its web pages.
(master page is used to create a sample layout, which in turn is
derived by all pages of website.
-​ Master page provides a feature of (standardized user interface)
which is common to all pages.
-​ ∴all pages in a website inherit the master age to maintain the
consistency in website.
-​ A theme is a collection of property settings that define the same
appearance of controls & web pages across entire website.
●​ Need of master pages & Themes:
-​ Master page defines a standardized layout for website. Master
page contains mark-ups & controls that can be shared across diff
web pages.
-​ The functionality of master pages improves the maintainability of
ASP.net website & avoids duplication of code.
-​ In website, if u want to include a header, footer, banners &
navigation menus.
-​ If master page is not used, then layout & control elements must
be define for every page, u have to copy & paste same code in
every page which is tedious & time-consuming process.
-​ It also affect the appearance of web pages.
-​ If u want to modify the header or footer of website, u need to
make modifications in each page manually.
-​ ∴to maintain consistency & manageability, use master page.
-​ Similarly theme a can be used with master pages to define the
style of a website globally & impl same style across the website.
●​ Understanding Master pages:
-​ Master pages enable the template based development of a
website. ASP.net master page defines the overall layout of website
& all web pages are derived from this master page.
-​ It can contain mark-ups, controls, banner, navigation menus &
other elements.
-​ It is an ASP.net file having .master as its extension. A master page
includes std & HTML control.
-​ Master page is define by specifying @ Master directive at the top
of web page.
<%@ Master language= “C#” code file= “master page.master.cs”
Inherits= “Master page”>
-​ This directive states that it belong to a master page file its
properties ∍ banner, header, footer & menus.
-​ By default, a master page contains a single Content Place Holder
control, which adds the dynamic content to a web page, u can add
multiple Content Place Holder controls in a master page.
<asp: Content Place Holder id= “head” runat= “server”>
-​ The diff b/w a simple web page & master page in terms of code is
that the master page contains an empty Content Place Holder
control.
-​ The syntax to refer a master page is, <%@ page language= “C#”
Master Page File= “Master page master” Title= “content page”%>
-​ Master Page File attribute of the @ page directive helps in binding
a master page file with the content page of website.

●​ Key functions of master page:


o​ Defining common properties of a website ∍ header, footer, banner,
navigation, menus & other elements.
o​ Allowing single\multiple content page to access single\multiple
master pages.
o​ Displaying the content of each content page in the Content Place
Holder control of master page.
-​ To create a master page, identify the controls that u want to
display on all pages & add the Content Place Holder control on
master page to place the content of web pages. When u view the
final web page the content page & master page are combined
together.
-​ In ASP.net. master pages are of 2 types
1. Simple master page.
2. Nested master page.
1.​ Simple master page: simplify website design by creating master
pages & content pages. – no overhead of copying & pasting the
code in diff web pages. The combination of simple master page &
content page makes a final page. The functionality of simple
master page is to define a common layout for all pages of website
& use if again & again. – u can save time by easily to maintaining
all web pages.

2.​ Nested Master page: implies that a master page can be included
inside another master page. The master page which contain
another master page inside it, is known as parent master page, &
the master page that is placed inside parent master page is known
as child\sub master page
-​ Is similar to simple master page should be used when you want to
crate a hierarchy of templates for website.
-​ Create parent master page as,
<% @ master language= ”c# ” Auto event wireup=“true code
file=”parent.master.cs” Inherits= “parent/”%>
-​ Create child master page as
<%@master language =”c#” master page file =”~/parent.master
Auto event wire up=”true” code file =” child.master.cs” Inherits
=”child” %>
-​ Ex: website with 3 departments: HR, Marketing, finance. Main
page consist of Header with logo & footer when emp accesses the
site, home page appears. In home page, the emp need to login to
access their department. ∴ u need to design diff interfaces for
every depts.. (which also include header & footer displayed on
main page)
-​ Define parent master page & design its struct & create 3 child
master pages to lay down diff interfaces for each dept. Every child
inherits the struct of parent & also uses Content Place Holder
control to define its own content. Content page of child maps with
parent master page & makes a nested master page.
-​ Nested master page is combination of parent master page, a child
master page and a content page.
-​ Advantage of this technique is whenever u want to change
company logo\header, modify only parent master page & then all
web pages would reflect those changes.

-​ A website can have number of master pages depending on level of


hierarchy.
-​ Creating master page: need for creating master page arises when
you want to incorporate common look & feel across all web page –
create a template that is applied to all content page. A master
Page has master extension, content page has aspx extension.
-​ To create a simple master page – create master page & then a
content page that inherits properties of master page, a child
master page and a content of both master page & child master
page.
-​ Creating simple master page:
Create a website, my org (set ASP.net empty web site template).
Then perform the following steps.
1.​ Right click website name in solution-explore & select add
→Add new item option.
2.​ Select installed →visual c# option from left pane & master page
template from middle pane of Add New Item dialog box & then
click Add button
Master page. master page is added
3.​ Add the following code to master page
%@Master language =”C#” Auto Event Wire up= “true” code File=
“Master Page. master. cs” Inherits= “Master page>
<! DOCTYPE html>
<html xmlns= “http://www/w3.org.1999/xhtml”>
<head run at= “server”>
<title> Master pages </title>
<link href= “style sheet. css” rel= “style sheet” type= “text/css”/>
</head>
<body>
<form id= “form1” runat= “server”>
<div id= “header”>
<h1>
​ .Net 4.5 programming Black Book
</h1>
</div>
<div id= “side bar”>
<div id= “nav”>
​ &ndsp
</div>
</div>
<div id= “content”>
<div class= “item content”>
<strong> <emp> My organization website <br/>
​ <asp: Content Place Holder id= “Content Place Holder1” runat=
“server”>
</asp: Content Place Holder>
</emp> </strong>
<br/> </div>
</div>
<div id= “footer”>
<p class= “left”>
​ All content copyright & copy; logent. Solution Inc. </p>
</div>
<div> </div>
</form>
</body>
</html>
-​ The master page is designed as a template, which contains HTML
elements, static text & img control, for web pages.
4.​ Add a web form by selecting web Form template from middle pane
of Add New Item dialog box.
(The new web form inherits the properties of master page)
5.​ In this content page\web form select ,select master page check
box & click Add button.
6.​ Select Master Page. master option from the contents of folder
pane.
7.​ Click OK button.
Default.aspx page appears
Add this code.
<%@ page title= “ ” Language= “C#” Master page File=
“~\Master Page.master” Auto Event Wireup= “true” Code File=
“Default.aspx.cs” Inherits= “Default”%>
<asp: Content Place Holder ID1= “content1” Content Place Holder
ID= “Content Place Holder ID1” runat= “server”>
<asp: Text Box ID= “Text Box1” runat= “server”>
​ </asp: Text Box> <br\>
<br\>
<asp: Button ID= “Button1” runat= “server” Back Color= “Black”
Font Bold= “True” Font size= “small” Fore Color= “White” Text=
“Click to submit” On Click= “Button1 –click”/>
<br/> <br/>
<asp: Label ID= “Label1” runat= “server” css Class= “title” Text=
“Label” Visible= “False” Width= “132px”>
</asp: Label>
</asp: Content>
8.​ Add the following code to code-behind file.
Using system;
Using system. Collections. Generic;
Using system. Linq;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. Web contents;
Public partial class = Default: System. Web. UI. Page
{
Protected void page-load (OS, EA, e)
{
}
Protected void Button1 – click(Os, EA, e)
{
​ Label1. Text= “u have entered” + Text box1. Text;
​ Label1. Visible= true;
}
}
9.​ Run by pressing F5.
Creating nested master pages:
-​ To create a new nested master page that inherits the properties of
main master page
-​ Select the “select master page” checkbox in add NewItem
dialogbox, while adding a new master page to website
-​ Name this master page as hed.master.
-​ Code for hed.master page
-​ <%@Master Language=”c#”
MasterpageFile=”~/MasterPage.master, AutoEvent=”true”
CodeFile=”hrd.master.cs” Inherits=”hed”%>
-​ <asp:content ID=”content1” ContentPlaceHolderID
=”ContentPlaceHolder1” runat=”server”>
-​ <asp:LabelID=”Label1” runat=”server” Backcolor=”Aqua”
BorderColor=”solid” Fontsize=”Large” Text=”HR Dept”
visible=”true”>
-​ </asp:Label>
-​ <br>
-​ <asp:ContentPlaceHolderID=”contentplaceholder1”
runat=”server”> </asp:contentplaceholder>
-​ </asp:content>
-​ Makje changes in @pagesirective of default.aspx page
-​ %@page MasterPageFile=”~\hed.master”
-​ Just change MasterPageFile name
-​ Run website, by pressing f5
-​ o/p: content& layout of masterpage.hrd& Default pages are
combined together to generate single final page.
Configuring Master pages
-​ To associate a content page with master page, you need to specify
the name of the master page in either the content page by using
@Page directive or config.file(web.config) of website.
Modifying content on a master page
-​ You can update the content on a master page by selecting the
control that needs to be modified on the master page from content
page.
❖​ WEB SERVICES
-​ Web service is an independent& self-sustained unit of software
application hosted on the Internet.
-​ A webservice is an entity that you can program to particular entity
that you can program to provide a particular functionality to
applications over internet.
-​ You can use a web service to calculate simple interest, compound
interest, calculate tax, pay slip etc
-​ A we service allows a websites to communicate with other
websites.
-​ Irrespective of programming language in which they are created.
-​ A web service can be accessed by any application, regardless of
software& hardware platforms on which application is running.
-​ Web services use standards ∍ SOAP& WSDL(SOAP- simple object
access protocol)(WSDL- web service description language)
-​ A web services does not have any user interface, it only contains
logic for providing specific services to its consumers
-​ A web services providers an abstraction between the consumer&
the provider of web service
Advantages of web services over COM& DCOM
1. web services are simple to use& implied on varied platforms.
2. web services are loosely coupled, so their interfaces & method
can be extended.
3. web services do not carry any start info with them, so that
multiple requires can be processed simultaneously.
-​ Web services are most appropriate while communicating across
platforms or enforcing trusted connections b\w server & client.

●​ Infra Structure of ASP.net web services


-​ The Infra structure of ASP.net web services provides a simple API
for web service to create & understand SOAP messages that
inturn help to invoke remote methods. Web services must not
depend on os, object model & PL. Web services should be loosely
coupled & able to communicate when needed & have a universal
data format to transfer data b\w the services & its users.

-​ The infra struct consist of 4 main components.


1.​ Web service Directory.
2.​ Web service Discovery.
3.​ Web service Description.
4.​ Web service Wire Format.
1)​Web service Directory:
-​ Provides a central location for web service providers. – it is a place
where web service users (consumers) can easily locate services
offered by other companies & organisations. – is a centralized
location, where u can publish info about web service. The web
service can be published & discovered by using the specification
called UDDI(Universal Description, Discovery & Integration). The
schema associated with UDDI defines 4 types of info for web
service. 1. Business, 2. Web service, 3. Binding, 4. Specification.
These info types enable a developer to use published services.
2)​Web service Discovery:
-​ Provides the capability to locate web services. This component
locates documents that describe a particular web service, u need
to know where the web service is located. ∴ there must be a
searching(discovery) mechanism, which can locate the required
web service. 2 methods are used to search URL of a web service.
1.​ Web service discovery tool(Disco.exe) – used for publishing &
discovering web service. This tool discovers URLs of all web service
located on a web server & creates a list of them in a file called
Disco file(.disco).
-This file is accessed by clients to know about web service
2.​ UDDI – centralized dir, where web service offered by diff
organisations are published. It is a platform independent method
that is built into .Net platform. A UDDL direct has an advantage
over a DISCO file that it provides a single location where clients
can search web services offered by diff organisation.
●​ Important features of UDDI
-​ Acts as a dir & stores info about web services
-​ Uses www consortium(w3c) & Internet Engg Task Force(IETF) stds
∍ XML, HTTP & DNS protocols
-​ Uses WSDL to describe interfaces to web services
-​ Communicates through SOAP.
●​ Some benefits of using UDDI are
-​ Helps to search for right business organisation.
-​ Enables commerce, once the preferred business is discovered.
-​ Helps to reach new customers & increase access to current
customer.
-​ Helps to expand offerings & market reach.
-​ Allows rapid participation in global Internet economy.

Describes web services & business processes programmatically in a


single, open & secure environment.

3)​Web service Description:


-​ Provides the description of a web service in XML format, known as
WSDL. WSDL document defs mesg formats ∍ HTTP-GET &
HTTP-POST, to facilitate communication b\w a client & a service
mesg pattern dets how mesgs can be exchanged b\w a client & a
service. The behaviour of web service depends on mesg pattern.
-​ WSDL is an XML-based language, that defines web service. It
contains diff XML elements to define a web service main elements
of WSDL documents.
Element Description
name

Types Describes all data types are used by a web


services.

messages Describes messages that are used to


communicate from one point to another, when
data is passed by web services.

Port type Describes set of abstract operation where


each operation links to an input message&
output messages

Binding Describes communication protocols that are


used by web services

Port Describes an address for binding& therefore


defining a communication end points

Service Describes a set of related end points, where


an endpoint is defined as a collection of
binding& an address(URK)

4)​Web services wire format


-​ Provides an interface to transmit data between a web services& its
users through protocols such as HTTP-GET& HTTP-POST.
-​ A web service can use any RPC protocol such as DCOM or COBRA,
to transmit data.
-​ But these protocols are not recommended for creating universally
available web services.
-​ Therefore you need to work with frequently used protocols such as
HTTP& SOAP, to transfer data between web services & its uses.
Common protocols used by web services to transmit data
Protocol name Description

HTTP-GET Creates a query string of


name or value pair such as
name=”raj” & appends it to
URL

HTTP-POST Passes the name or value


pair in the body of HTTP
requests message

SOAP Exchanges information in a


decentralized, distributed
environment

MIME(multipurpose internet Defines the standard


mail extension) represents for complex
message bodies such as
messages with embedded
graphics or audio clips

-​ These protocols are used to invoke web methods of a web


services.
1. HTTP-GET&HTTP-POST- these protocols can transmit only
enumerations, arrays, strings. By default they are not allowed to
call local web services
2. SOAP- one of key elements in a web service, is a protocol used
for messaging
- Completely XML based SOAP specifications are standardized by
w3c
- provides complete set of rules for messages called SOAP
envelops, rule for issues such as data encoding, message handling,
message binding.
- one of the advantages of using SOAP message is simple XML
document, which always contains the mandatory envelope& body
elements can contain few optional elements such as <header>&
<fault>
Creating a web service
Points to be considered while using creating a web service
1.​ Ensure that the communication between a web application that
requests a web service& web server is asynchronous.
2.​ Minimize the number of requested that a web application needs to
send to a web server to obtain web service
3.​ Minimize the time a web server takes to respond to the request of
a web application.
4.​ Ensure that the web service is created by using the concepts of
object oriented programming.
The tasks we need to perform while creating a web service
1.​ Creating a web service
2.​ Declaring the web service
3.​ Setting the web service attribute
4.​ Describing method for a web service
Creating a web service
Create web service with the help of web service template available
in ADD ‘NewItem dialogbox.
When u create a web service, it already contains Helloworld
function in code-behind file
Declaring own web service
-​ Declaring using @web service directive that provides information
required by compliers.

class Specifies the name of the class


that implements web service
Codebehind Specifies file name of the class
that implements of web services.
debug Specifies whether to debug web
service during compilation
Language Specifies language

-​ <%@web service attribute=”value”[attribute=”value”....]%>


-​ <%@web service language=”c#” class=”web service”%>
Setting web service
-​ Not mandatory you can set to default namespace,
http://tempurl.org for a web service( to differentiate your web
service from others, change default namespaces)
-​ Declaring methods for a web service
-​ - declaring the methods that define the functionality of web
service.
-​ - the web method attribute enables a web service methos to
communicate with web application.
-​ SYNTAX:
-​ [web method]
-​ Public return type function –name()
-​ To create a web service, first create empty website, add web
service.

1.​ Create method for implementing web service


Code-behind file of service1.asmx
[web method]
Public string Hello world()
{
Return “Hello world”;
}
[web method]
Public double calculate simple interest(double p, double r, int t)
{ double simple interest
Simple interest=p*t*r/100;
Return simple interest;
}
2.​ Press f5 to execute
3.​ Enter p,r,t
4.​ Click invoke button
-​ New web page displays the interest
Deploying a web service
-​ After creating a web service, next step is to make the web service
available to other web application over internet deploy web service
using install shield limited edition software in VS2012 IDE

You might also like