Está en la página 1de 80

ASP.

NET

(Unit 3)
Active Server Page
• ASP is not a language, it’s a platform that
can host scripting languages like VBScript
and JScript.
• This platform runs on a Web server,
typically IIS, but ASP is also available from
third-party vendors for use on other Web
servers.
Requests
Responses
1 7
6 10

ISAPI Filters
Internet Information Server
ISAPI Extensions Script Execution
ASP.DLL
5 9
ASP Script Script Code Script Engine
Engine 4 8 Cache

2
3

.asp file
Problem with ASP
• One of the problems with programming
traditional Active Server Pages is that the
server-side code is mixed in with the HTML and
client-side code.
• the code is interpreted rather than compiled,
resulting in slower performance.
• session state is not shareable within a Web farm
environment.
• ASP also uses late binding when making calls to
compiled COM components, resulting in slower
performance.
ASP.NET
• ASP.NET represents an exciting new
platform for creating Web sites with the
.NET Framework, using any .NET
language.
Benefits of ASP.NET
• Layout control
– Using Web Forms in ASP.NET and positioning
controls such as text boxes and buttons is easy, and
Visual Studio .NET will create the appropriate HTML
code for the target browser that is selected.
• Structure
– ASP.NET brings structure back into programming by
offering a code-behind page, which separates the
client-side script and HTML from the server-side
code.
Benefits of ASP.NET
• Compiled code
– ASP.NET solves the problem of running
interpreted script by compiling the server-side
code into IL (Intermediate Language). IL code
is significantly faster than interpreted script.
• Early binding
– ASP.NET also uses early binding when
making calls to COM components, resulting in
faster performance.
Benefits of ASP.NET
• Security
– ASP.NET has an enhanced security
infrastructure that can be quickly configured
and programmed to authenticate and
authorize Web site users.
• Performance
– ASP.NET contains performance
enhancements, such as page and data
caching.
Benefits of ASP.NET
• Diagnostics
– ASP.NET offers an enhanced tracing and
debugging option, which will save time when
you are ready to get the system running.
• Session state
– ASP.NET has an improved session object.
Session state can be configured to be shared
among all servers in a Web farm.
Benefits of ASP.NET
• .NET Framework.
– Since ASP.NET uses the .NET Framework, ASP.NET
also inherits the features of the .NET Framework,
such as:
• Automatic memory cleanup via garbage collection
• Cross-language inheritance
• A large object-oriented base class library
• The use of ADO.NET to access databases
• Web services
– ASP.NET also provides the Web service
infrastructure. It is possible to create a Web service
with very few lines of code.
Working
• First IIS determines that the file is .aspx
• If the application is loaded for first time, it
automatically creates the application
domain and application objects.
Global.aspa variables are created
• Once application is running it will never
shut down unless you reboot the computer
Working
• It compiles and caches an executable
version of .aspx file. If this task has
already been performed and the file has
not been changes, ASP.net will use the
precompiled version.
• It starts and runs all its code. At this stage
everything is working together as a set of
in-memory .NET objects.
Working
• Instantiate the page, fire page events and
run the event handling code.
• Render the page to HTML, one control at
a time
• Web response
Requests Responses

1 7 6 9

Modules

Page ASP.NET
Handler Http Runtime
2
ASPX Class
Engine Instance
8

3 4 5

ASP.NET
Page Page
Class
ASP.NET File Types
.aspx Asp.net web pages
.ascx Asp.net user controls
.asmx Asp.net web services
Web.config This is xml-based configuration file
for Asp.net application
Global.asax This is the global application file
.disco or These are special discovery files
.vsdisco used to help clients find web
services.
ASP.NET File Types
.vb or .cs These are code-behind files created
in VB or c#
.resx These files may exist if you are
using Visual Studio.NET. They are
used to store information that you
add at design time
.sln, .suo, These files are used by VS.NET to
.vbproj, group together projects and
.csproj solutions. They store list of related
files and some options for the
VS.NET IDE.
• The Bin Directory
– Every web application directory can have it. It
holds .NET assemblies used by your
application.
– Example, you can develop a special database
component in any .net language, compile it to
a DLL file and place it in bin directory.
ASP.net will automatically detect it and allow
any page in that application to use it.
How to write ASP.net page
• Write scripting as well as html and asp .net
control code in single page
• Code behind
– Create a separate cde file (.vb or .vb.net or.cs) to
match every .aspx file
– The aspx file contains the user interface logic, and
asp.net tags that creates controls on the page.
• Compiled code behind files
– Use it as precomipled assemblies.
Web form inheritance
Example
• Without code behind
– Hello.aspx
• With code behind
– Default.aspx
Global.asax
• It is for application code
• It can’t contain any HTML or ASP.NET
tags
• It contains event-handling code that reacts
to application or session events
Web.config
• It uses a special XML format.
• It is used for configuration
Web controls
• Web controls are special ASP.NET tags
understood by the server.
• Web server controls are also created on
the server and they require a
runat="server" attribute to work.
• Syntax
• <asp:control_name id="some_id"
runat="server" />
Web Controls
• AdRotator:Displays a sequence of images
• Button:Displays a push button
• Calendar:Displays a calendar
• CalendarDay:A day in a calendar control
• CheckBox:Displays a check box
• CheckBoxList:Creates a multi-selection check
box group
• DataGrid: Displays fields of a data source in a
grid
• DataList: Displays items from a data source by
using templates
web controls
• DropDownList: Creates a drop-down list
• HyperLink: Creates a hyperlink
• Image: Displays an image
• ImageButton: Displays a clickable image
• Label: Displays static content which is programmable
(lets you apply styles to its content)
• LinkButton :Creates a hyperlink button
• ListBox: Creates a single- or multi-selection drop-down
list
• ListItem: Creates an item in a list
• Literal: Displays static content which is
programmable(does not let you apply styles to its
content)
web controls
• Panel: Provides a container for other controls
• PlaceHolder: Reserves space for controls added
by code
• RadioButton: Creates a radio button
• RadioButtonList: Creates a group of radio
buttons
• BulletedList: Creates a list in bullet
formatRepeaterDisplays a repeated list of items
bound to the control
• Style: Sets the style of controls
web controls
• Table: Creates a table
• TableCell: Creates a table cell
• TableRow: Creates a table row
• TextBox: Creates a text box
• Xml :Displays an XML file or the results of
an XSL transform
AdRotator control
• The AdRotator control is used to display a
sequence of ad images
• This control uses an XML file to store the
ad information. The XML file must begin
and end with an <Advertisements> tag.
Inside the <Advertisements> tag there
may be several <Ad> tags which defines
each ad.
AdRotator control
example
• <html>
<body>
<form runat="server">
<asp:AdRotator AdvertisementFile="Ad1.xml"
runat="server" target="_blank" />
</form>
<p><a href="ad1.xml" target="_blank">View XML
file</a></p>
</body>
</html>
Button control
• The Button control is used to display a push button.
Calendar control
• Used to display a calendar in the browser.
• This control displays a one-month calendar that
allows the user to select dates and move to the
next and previous months.
Example
CalendarDay control
• The CalendarDay control represents a day
in a calendar control.
Check box control
• The CheckBox control is used to display a
check box.
DropDownList Control
• Used to create a drop-down list.
• Each selectable item in a DropDownList
control is defined by a ListItem element!
• This control supports data binding
HyperLink control
• Used to create a hyperlink.
Image Control
• used to display an image
• Properties
• AlternateText: An alternate text for the image
• DescriptionUrl: The location to a detailed description for
the image
• GenerateEmptyAlternateTextSpecifies whether or not
the control creates an empty string as an alternate text
• ImageAlign: Specifies the alignment of the image
• ImageUrl: The URL of the image to display for the link
• Runat : Specifies that the control is a server control. 
Must be set to "server"
Image Button Control
• used to display a clickable image
Label control
• The Label control is used to display text on
a page. The text is programmable.
LinkButton Control
• The LinkButton control is used to create a hyperlink
button.
ListBox Control
• Used to create a single- or multi-selection
drop-down list.
• Each selectable item in a ListBox control is
defined by a ListItem element!
• This control supports data binding
ListItem control
• The ListItem control creates an item in a list.
• This control is used with the list controls such as
<asp:ListBox>, <asp:RadioButtonList> and
<asp:BulletedList>
Panel Control
• The Panel control is used as a container
for other controls.
• This control is often used to generate
controls by code and to display and hide
groups of controls.
• This control renders as an HTML <div>
element.
Panel control
RadioButton Control
• used to display a radio button.
• To create a set of radio buttons using data binding, use
the RadioButtonList control
BulletedList Control
• The BulletedList control creates a list in bullet format.
• Each item in a BulletedList control is defined by a
ListItem element!
Style control
• The Style control is used to set the style of
a web control.
Table, TableCell, TableRow
controls
• The Table control is used in conjunction
with the TableCell control and the
TableRow control to create a table.
TableRow
TableCell
TextBox control
• used to create a text box where the user can input text.
XML Control
• The XML control is used to display an XML
document or the results of an XSL Transform.
• At least one of the XML Document properties
must be set or no XML document is displayed.
• You can also specify an XSLT document that will
format the XML document before it is written to
the output. You can format the XML document
with the Transform property or the
TransformSource property.
Properties
AutoPostback
• It submit the page back to the server when
it detects a specific user action.
• It gives code the chance to run again and
create a new, update page.
• To capture a change event for a web
control, set AutoPostBack property to
True.
Web controls that support
automatic postbacks
Event Web controls
Click Button, ImageButton
TextChange TextBox
CheckChanged CheckBox, RadioButton

SelectedIndexChanged DropDownList, ListBox,


CheckBoxList,
RadioButtonList
How PostBack events works
• Finally the control that has this property
set to true is connected to the –
doPostBack function using the onclick or
onchange attributes.
• ASP.NET automatically changes a client
side JavaScript event into a server-side
ASP.NET events.
Page LifeCycle
• On the client side, the javascript
_doPostBack event is invoked, and the
page is resubmitted to the server.
• ASP.NET recreates the page object using
the .aspx file
• ASP.NET retrieves state information from
the hidden viewstate field, and updates the
controls accordingly
Page LifeCycle
• The Page.Load event is fired
• The appropriate change event is fired for
the control
• The Page.UnLoad event fires and the
page is transformed from a set of objects
to an HTML page.
• The new page is sent to the client.
• Examples
Validation Controls
• Validation controls to prevent users from
submitting the wrong type of data
• It includes how to control, how validation
errors are displayed, how to highlight
validation error messages, and how to use
validation groups.
• Syntax
• <asp:control_name id="some_id"
runat="server" />
Type of Validation controls
• RequiredFieldValidator—Enables you to
require a user to enter a value in a form
field.
• RangeValidator—Enables you to check
whether a value falls between a certain
minimum and maximum value.
• CompareValidator—Enables you to
compare a value against another value or
perform a data type check.
Type of validation control
• RegularExpressionValidator—Enables
you to compare a value against a regular
expression.
• CustomValidator—Enables you to
perform custom validation.
• ValidationSummary—Enables you to
display a summary of all validation errors
in a page.
Example
• <asp:TextBox id=”txtProductName”
Runat=”server” />
<asp:RequiredFieldValidator
id=”reqProductName”
ControlToValidate=”txtProductName”
Text=”(Required)” Runat=”server” />
CompareValidator
• If the input control is empty, the validation will succeed.
Use the RequiredFieldValidator control to make the field
required.
CompareValidator control
CustomValidator
CustomValidator
RangeValidator
• The validation will not fail if the input control is
empty. Use the RequiredFieldValidator control to
make the field required.
• The validation will not fail if the input value
cannot be converted to the data type specified.
Use the CompareValidator control, with its
Operator property set to
ValidationCompareOperator.DataTypeCheck, to
verify the data type of the input value.
RangeValidator
RangeValidator
RegularExpressionValidator
• Both server- and client-side validation are
performed unless the browser does not
support client-side validation or the
EnableClientScript property is set to false.
• The validation will not fail if the input
control is empty. Use the
RequiredFieldValidator control to make the
field required.
RegularExpressionValidator
RegularExpressionValidator
RequiredFieldValidator
• With this control, the validation fails if the input
value does not change from its initial value. By
default, the initial value is an empty string ("").
• Leading and trailing spaces of the input value
are removed before validation.
• The InitialValue property does not set the default
value for the input control. It indicates the value
that you do not want the user to enter in the
input control.
RequiredFieldValidator
RequiredFieldValidator
ValidationSummary
• Summary of all validation errors occurred
in a Web page.
• The error message displayed in this
control is specified by the ErrorMessage
property of each validation control. If the
ErrorMessage property of the validation
control is not set, no error message is
displayed for that validation control.
ValidationSummary
• Examples

También podría gustarte