asp.net basics course textbook: build your own asp.net website: chapter 2

17
ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Upload: walter-warren

Post on 24-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

ASP.Net Basics

Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Page 2: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Lesson Objectives

Introduces more advanced ASP.Net topics such as: ASP.Net Page Structure ASP.Net Server Controls Directives View State Namespaces

Page 3: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

ASP.Net Page Structure An ASP.Net page consists of the following

elements: Directives Code declaration blocks Code render blocks ASP.Net server controls Server-side comments Server-side include directives Literal text and HTML tags

Not every element always appears on a given page, we will learn when to use them

Page 4: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Directives Control how the page is compiled Specify settings when transferring

between pages Aid in debugging Allow importing of classes Start with the <@ sequence and end with

a %> sequence ASP.Net directives can appear anywhere

on the page, but are usually placed as the first lines in the file

Page 5: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Three Common Directives The Page directive defines page-specific

attributes like the language to be used as in: <%@ Page Language=“C#” %>

The Import directive makes functionality defined elsewhere through the use of namespaces as in: <%@ Import Namespace=“System.Web.Mail”

%> The Register directive links a user control

to the ASP.Net page as in: <%@ Register TagPrefix=“ux”

TagName=“footer” Src=“footer.ascx” %>

Page 6: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Code Declaration Blocks When you add programming logic to

your .aspx page, it resides inside a <script runat=“server”> tag

Code declaration tags usually are placed in the <head> of your ASP.Net page

If you don’t specify the language of the code, it will default to the language in the Page directive <script runat=“server” language=“C#”>

Page 7: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2
Page 8: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Code Render Blocks Used to define inline code or inline

expressions <% String Title = “Harry Potter”; %> <% Title %>

The first line contains a complete line of C# code, the declaration and assignment of a String variable

The second line writes out the Title variable onto the page

Page 9: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

ASP.Net Server Controls Server Controls represent the dynamic

elements users interact with. There are four types of server controls:

HTML Controls ASP.Net Controls Validation Controls User Controls

Most server controls must reside within a

<form runat=“server> tag

Page 10: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Advantages of Server Controls HTML elements can be accessed from

within code to change their characteristics, check their values, or dynamically update them

ASP.Net controls retain their properties even after the page was processed. This process is called the View State

With ASP.Net controls, developers can separate the presentational elements and the application logic so they can be considered separately

Page 11: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

What is the View State???

The persistence of data after it is sent to the server for processing is possible because of the View State

If you have created forms using HTML controls, you have experienced the loss of data after form submission

The data is maintained in the view state by encrypting it within a hidden form field

Page 12: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

ing at the View State

Look at the source code of the file after the page has been submitted to see code similar to this… i.e. <input type= hidden”

name=“VIEWSTATE” value=“dWtMTcy0TAy0DawNzt)PDtsPGk6Mj47PjtsPHQ802w8aTWzPj+02wPGw5uAXJdGFaGaxk6t4=“ />

The View State is enabled for every page by default If you don’t intend to use the View State, set the

EnableViewState property of the Page directive to be false <%@ Page EnableViewState=“False” %>

Page 13: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Server-Side Comments Server-side comments will not be processed by

ASP.Net It used the <%-- beginning sequence and the --

%> ending sequence <% -- This is a server-side comment --%>

The difference between HTML comments and ASP.Net comments is that ASP.Net comments are processed by the browser or the ASP.Net runtime

Don’t use HTML comments to comment out ASP.Net code

HTML comments only hide things from the browser

Page 14: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Server-Side Include Directives

These includes help developers insert segments of code into a page from an external file

There are two techniques for doing this: Using the file attribute, we give the physical path to

the file on the server either as an absolute path starting from the drive letter or a relative path to the current file

<! -- include file=“myinclude.aspx” --> (relative path) Using the virtual attribute, you can specify the file’s

location from the absolute root of the site, or from a relative path to the current page.

<! -- include virtual=“/directory1/myinclude.aspx” --> (absolute path

Page 15: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Literal Text and HTML Tags One cannot do without text and

HTML elements to display information from your ASP.Net controls and programming code

Without these there would be no format to the page

The surrounding <html>, <head>, and <body> tags make it possible for a browser to understand our page

Page 16: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Languages .Net supports many different languages Programmers used to VBScript or

JavaScript to do their programming will have more robust, strongly-typed, and feature-rich choices in VB.Net and C#.Net VB.Net builds on the RAD that became

popular in the 90’s. VB.Net is easy to read, use and maintain.

C#.Net was developed to keep the simplicity of VB and the power and flexibility of C++ (some say to replace Java). C# does away with confusing C++ syntax.

Page 17: ASP.Net Basics Course Textbook: Build Your Own ASP.Net Website: Chapter 2

Summary On an ASP.Net page,

you will probably use: directives, code declaration blocks, code render blocks, includes, comments, and controls.

Two languages supported by ASP.Net are VB.Net and C#.Net. We will focus on C#.Net syntax for this course.

In the next chapter we will cover some C# programming basics.