working with asp pages. slide 2 the tag (1) remember that most asp.net pages contain a single tag...

23
Working with ASP Pages

Post on 19-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Working with ASP Pages

Page 2: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 2

The <form> Tag (1) Remember that most ASP.NET pages

contain a single <form> tag with the runat attribute set

It’s possible to have multiple <form> tags but only one can be visible at a time Visible property is true This capability is great for implementing

wizards (sequential dialog boxes) The MultiView and Wizard control can

also be used

Page 3: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 3

The <form> Tag (2) Generally <form> tags are the

outermost tag but can be children of container controls <table>, <div>, etc…

Non-container (server) controls must appear inside of a <form> tag or exceptions will be thrown at run-time No compile error occurs

Page 4: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 4

The HtmlForm Class (1) A form is accessed on the server using

the HtmlForm class The DefaultButton property sets the

form’s default button The button clicked when enter is pressed

The InnerHtml property contains the markup between the opening and closing tags

Page 5: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 5

The HtmlForm Class (2) The HasControls method indicates

whether a from has child controls The FindControl finds the control

instance with a particular ID Only direct children are searched

The Focus method sets input focus to a particular control

Page 6: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 6

Cross-Page Posting (Introduction 1) You can use a HyperLink control to

create a link to another page Any Button control can be used for

cross-page postbacks Set the PostBackURL property to the

desired Web page

Page 7: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 7

Cross-Page Posting (Introduction 2) Use the Server.Transfer method

The target page is passed as an argument Any code following Server.Transfer is

not executed

Server.Transfer(“DestinationPage.aspx”);

Page 8: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 8

Cross-Page Posting (Getting Data from the Previous Page) The PreviousPage property of the Page

object contains a reference to the page that caused the postback

This property allows you to get at the controls from the previous form and their values This is magically accomplished through

viewstate

Page 9: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 9

Cross-Page Posting (Example) Get a reference to a control on another

page and print the text TextBox txt; txt = (TextBox)PreviousPage.FindControl( "txtPreserve");

Response.Write(txt.Text);

Page 10: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 10

Detecting Cross-Page Postbasks (1) Test the PreviousPage property to see if

it is null If it is, the page was not loaded as a cross-

page postback Test the IsCrossPagePostBack property

of the ‘posting’ page

Page 11: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 11

Detecting Cross-Page Postbacks (2) The @PreviousPageType directive

allows you to use strongly typed syntax if page “b” will only be posted from page “a”

<%@ PreviousPageType virtualPath=“a.aspx” %>

Page 12: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 12

Cross page Postback vs. Server.Transfer Cross page postbacks are client-based The Transfer method is server-based

Both allow you to reference the PreviousPage property

Page 13: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 13

Introduction to Error Handling Just like desktop applications,

exceptions are thrown in various cases Divide by 0, IO, Database, etc.

Unhandled, we call these the yellow page of death

What happens depends on the following Web.config setting:

<compilation debug="false">

Page 14: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 14

Error Message (Debug=true)

Page 15: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 15

Error Message (Debug=false)

Page 16: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 16

Error Message – Enabling Debugging

Page 17: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 17

Error Message – Enabling Script Debugging

Page 18: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 18

Handling Errors (Introduction) Exception handlers are created (as

usual) using try, catch blocks The Page_Error event handler gives

you a global exception handler for the page

The Application_Error event applies to the entire application itself

Page 19: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 19

Handling Errors (Introduction) Use Server.GetLasError() to get

information about the most recent error Use Server.ClearError() to clear the

exception Create other exception handlers, as

necessary

Page 20: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 20

Configuring Custom Errors It’s all controlled by the customErrors

section of the Web.config file Attributes

defaultRedirect attribute causes a redirect to the specified page

Mode attribute enables / disables custom errors

On – Enables custom for remote and local hosts

Off – Disables custom errors RemoteOnly – (default) Custom errors are

displayed to the remote client

Page 21: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 21

Configuring Custom Errors (Example)<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">

<error statusCode ="403" redirect="NoAccess.htm" />

<error statusCode="404" redirect="FileNotFound.htm" />

</customErrors>

Page 22: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 22

Tracing (Introduction) It’s possible to trace execution by

enabling tracing in the Web.config file @Page directives can also be used Example

<trace enabled="true" pageOutput="true"/>

See handout

Page 23: Working with ASP Pages. Slide 2 The Tag (1) Remember that most ASP.NET pages contain a single tag with the runat attribute set It’s possible to have multiple

Slide 23

Tracing (Output)