Www.tech.findforinfo.com1 Web Form Fundamentals Chapter-2 Unit-2

Preview:

Citation preview

www.tech.findforinfo.com 1

Web Form Fundamentals

Chapter-2 Unit-2

www.tech.findforinfo.com 2

Problem with Response. Write

• Spaghetti code

When the individual output is to be modified the entire

code is to be revised

• Lack of flexibility

Modification of the page after a month, the entire

code needs to be read and later modified

• Confusing content and formatting

Difficulty in creating larger forms

www.tech.findforinfo.com 3

Problem with Response. Write

• Complexity

Tracking of different types of functionalities leads to complexity

www.tech.findforinfo.com 4

Server Controls

• ASP.NET provides two sets of Server Controls

HTML Server Controls• Contains equivalent HTML elements

• Use similar HTML tags

Web Controls• Similar to HTML controls

• Provide a richer object set

• Variety of properties and formatting

www.tech.findforinfo.com 5

Features of HTML control

• They generate their own interface

The properties are set and the underlying HTML tag is updated automatically when the page is rendered and sent to the client

• They retain their stateThe web pages can be created in the same way as

the windows page is created

www.tech.findforinfo.com 6

Features of HTML control

• They fire events

You can respond to these events just like ordinary controls in windows application

www.tech.findforinfo.com 7

The ViewState

• Traditional ASP

• A form is submitted to the server with the filled

information

• The Server returns back with error

• All the information's are lost when is page is viewed by

the user

www.tech.findforinfo.com 8

The ViewState

• The ASP.NET

• The form is submitted to the server with information

• The server returns the page back with error

• Now the page contains the information that was

given at the time the page was sent to the server

HOW? This is because ASP.NET maintains the

viewstate

www.tech.findforinfo.com 9

The ViewState

• The ViewState indicates the status of the page when

submitted to the server

• The Status is defined through the hidden field placed on each

page with a <form> tag, the hidden fields stores information

about the state

• Maintaining the ViewState is the default setting of the

ASP.NET

• If you do not want to maintain the ViewState include the

directive <%@Page EnableViewState=“false” %>

www.tech.findforinfo.com 10

The HTML control class

• The HTML server controls are available in the namespace

System.Web.UI.HtmlControls

www.tech.findforinfo.com 11

The HTML control class

Class Name Tag

HtmlAnchor <a>

HtmlButton <button>

HtmlForm <form>

HtmlImage <img>

HtmlInputButton <input type=“button”> <input type =“submit”>,<input type=“reset”>

www.tech.findforinfo.com 12

The HTML control class

HtmlInputCheckbox <input type=“checkbox”>

HtmlInputControl <input type=“text”>

HtmlInputFile <input type=“file”>

HtmlInputHidden <input type=“hidden”>

HtmlInputRadioButton <input type=“radio”>

HtmlSelect <input type=“select”>

www.tech.findforinfo.com 13

Events

• Web forms are event driven means every piece of code acts in response to the specific event

• The HtmlButtonClass provides the Server Click event

www.tech.findforinfo.com 14

Events

• Imports statement

It provides access to all the important namespace

• Custom Page class and control variables• Single event handler

This event handler retrieves the value from the textbox and converts it into the respected conversion

www.tech.findforinfo.com 15

Event handling changes

• Two parameters (source and e)• Recommended standard for .NET• It allows your code to identify the controls that

sent the event

Private sub convert_Serverclick(sender As Object,e as EventArgs)_Handles convert.ServerClick

www.tech.findforinfo.com 16

Event handling changes

• Second the event handler connects itself to

the appropriate event using the handles

clause

• AutoEventWireup is set to “false” so that the

ASP.NET will always rely on the Handles

keyword to connect to the event handlers<input type=“submit” value=“Ok” id=“convert” OnServerClick=“Convert_ServerClick”runat=“server”>

www.tech.findforinfo.com 17

HtmlControl Classes

• Every html control inherits from the base class HtmlControl

• HtmlControl Events– ServerClick - Click is processed on the server

side– ServerChange - This event reponds when a

change has been made to a text or selection control

www.tech.findforinfo.com 18

Events and Controls that provide it

Events Controls that provide itServerClick HtmlAnchor,Form,Button,

InputButton,InputImageServerChange Text,InputCheckBox,

RadioButton,Select

www.tech.findforinfo.com 19

HtmlInputImage control

• Click the image and the co-ordinates are to be displayed

www.tech.findforinfo.com 20

HtmlControlBase Class

• Each Html control inherits from the base class HtmlControl

www.tech.findforinfo.com 21

HtmlControl Base class

Property DescriptionAttributes Provides a collection of all tag attributes

and their valuesControl Provides the collection of control

available under current controlDisabled True Disables the control and the user

cannot interactEnableViewState True:It uses the hidden field to store the

information about the page

www.tech.findforinfo.com 22

HtmlControl Base class

Property DescriptionPage Provides a reference to the web page

that contains the controlStyle CSS style properties that can be used to

formatTag Name Indicates the name of the Html elementVisible False:The control is hidden from the user

www.tech.findforinfo.com 23

HtmlContainerControl class

• Any html control that requires a opening and closing tag also inherits from the HtmlContainer Control

• <a> - HtmlAnchor• <form> - HtmlForm• <div> - HtmlGeneric Control

www.tech.findforinfo.com 24

HtmlContainerControl class

Property Description

InnerHtml Html content between opening and closing of the tags,used for formatting<b>,<i>,<u>

Innertext The text content between the opening and closing of the tags

www.tech.findforinfo.com 25

HtmlInputControl class

Property Description

Type Provides the type of input control <input type=“submit”>

Value Returns the content of the control as string

www.tech.findforinfo.com 26

Page class

• Every web page is a custom class that inherits from the Page class

www.tech.findforinfo.com 27

Page Class Property

Fundamental Property DescriptionApplication & Session It holds the information

about the stateCache Stores Objects for reuseControls Provides a collection of

the web controls in that Page

EnableViewState

www.tech.findforinfo.com 28

Page Class Properties

Fundamental Property DescriptionIsPostBack Boolean Property checks

whether the Page is submitted for the first time

Request Refer to the HttpRequest object that holds the information about the current web request

Response Set the web response or redirect the user to other web page

www.tech.findforinfo.com 29

Page Class Properties

Fundamental Property Description

Server Refers to the HttpServer utility performs some tasks like URL,Html encoding

User If the user has been authenticated then this property will be initialized with the user information

www.tech.findforinfo.com 30

HttpRequest class

• The HttpRequest class encapsulates all the information related to the client request for a web page

www.tech.findforinfo.com 31

HttpRequest class

Property Description

ApplicationPath Gets the virtual directory (URL)PhysicalPath Gets the real directory (URL)

Browser Provides link to the HttpBrowserCapabilities object which contains the browser features

ClientCertificate Gets the security certificate if there exists one

www.tech.findforinfo.com 32

HttpRequest class

Property Description

Cookies Gets the collection cookies sent with the request

Headers\Servervariables

Provides a name\value collection of HTTP headers and server variables

IsAuthenticated IsSecureConnection

Returns true if user is authenticated and secure sockets are used

www.tech.findforinfo.com 33

HttpRequest class

Property Description

QueryString Provides the parameters that were passed along with query string

Url and UrlReferrer

Current address of the page and the address of the page from where the user has navigated from

UserAgent A string representing the browser type.IE provides the value “MSIE”

www.tech.findforinfo.com 34

HttpRequest class

Property Description

UserLanguages Provides a sorted string array that lists the clients language

www.tech.findforinfo.com 35

Server Variables

• Request Method• Server Name• Server Port• Server Protocol• Server Software

www.tech.findforinfo.com 36

The HttpResponse Class

• This class allows you to send information directly to the client

• Some of the important functionalities• Caching Support• Cookie features• Redirect method

www.tech.findforinfo.com 37

Property Description

BufferOutput Default set to (true) the page is not sent to the client

Cache Refers to the HttpCache policy

Cookies The collection of cookies sent with response

Write(),BinaryWrite()WriteFile()

Allows you to write text or binary content directly to the response stream

www.tech.findforinfo.com 38

Property Description

Redirect() Transfers the user to another page or another website

www.tech.findforinfo.com 39

The Server Utility Class

• The ServerUtility class provides some miscellaneous helper methods

www.tech.findforinfo.com 40

Property Description

CreateObject Creates an instance of the COM object that is identified by its ID

HtmlEncode,HtmlDecode

Changes an ordinary string into a string with legal HTML characters

UrlEncode,UrlDecode

Changes an ordinary string into a string with legal URL characters

www.tech.findforinfo.com 41

Property Description

MapPath Returns the Physical path

Transfer Similar to response.redirect method

www.tech.findforinfo.com 42

www.tech.findforinfo.com 43

www.tech.findforinfo.com 44

Recommended