net 開發 (asp.net) 台南市教育局網路中心 在職教師校務行政系統程式...

80
.NET 開開 (ASP .NET) 台台台台台台台台台台 台台台台台台台台台台台台 台台台台台 [email protected] w 台台台台台

Upload: malcolm-arnold

Post on 26-Dec-2015

276 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

.NET 開發 (ASP .NET)

台南市教育局網路中心在職教師校務行政系統程式開發進階班

[email protected]

行政網路組

Page 2: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

目前 Web Ap 困境

AP 間資料無法整合 不同設備無法共享資訊 程式碼受限在特定裝置 昂貴建置成本

解決 .NET

Page 3: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

.NET 如何解決

DCOMWeb Service

RPCSOAP(XML+HTTP)

COM 整合於 new Class Library(.NET Framework)

XML+SOAP+WSDL

Unified Programming Classes(---Web Service)

VB:Visual Basic runtime library

C++:MFC 、 ATL(Active Template library)

Java:JVM

Page 4: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Microsoft .NET 願景 XML Web Services 平台

Any TimesAny TimesAny PlacesAny PlacesAny DevicesAny Devices

Page 5: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

作業系統作業系統

..NETNETEnterpriseEnterprise

Server Server ..NET FrameworkNET Framework

.NET 平台

..NETNETBuildingBuilding

BlockBlockServiceService

Visual Studio.NETVisual Studio.NET

Page 6: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

.NET Framework 之設計目標

簡化應用程式開發– 較少的雜工、較大的效能

一致的程式模型– 跨程式語言與應用程式型別

使用網際網路標準架構– XML、標準通訊協定‧‧

簡化佈署 (deployment ) 與管理– 元件、版本、可用性

提供穩定 , 安全 , 具擴充性 , 且高效能的執行環境

Page 7: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Windows COM+ Services

Common Language Runtime

Base Class Library

ADO.NET 與 XML

ASP.NET Windows Forms

Common Language Specification

VB C++ C# JScript …

Visu

al Stu

dio

.NE

TFramework, 語言及工具

Page 8: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

應用程式 的演進

在 COM以前,應用程式之間是完全獨立的個體

應用程式

程式碼與資料結構

Page 9: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

應用程式的演進

COM 提供了元件整合的方法, 但是各個元件必須提供聯結的管道 (plumbing)且物件無法直接作溝通

Page 10: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

應用程式的演進

透過 .NET Framework共通語言執行環境 (CLR),元件是建構在共通的底層上,不須再做聯結的管道的雜工,且物件可以直接的做溝通

Page 11: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

The .NET Framework Library

Base Class LibraryBase Class Library

ADO.NET and XMLADO.NET and XML

Web Forms Web ServicesWeb Forms Web ServicesMobile Internet ToolkitMobile Internet Toolkit

WindowsWindowsFormsForms

ASP.NETASP.NET

Page 12: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

System

System.Data System.Xml

System.Web

Globalization

Diagnostics

Configuration

Collections

Resources

Reflection

Net

IO

Threading

Text

ServiceProcess

Security

Design

ADO

SQLTypes

SQL

XPath

XSLT

RuntimeInteropServices

Remoting

Serialization

Serialization

Configuration SessionState

Caching Security

ServicesDescription

Discovery

Protocols

UIHtmlControls

WebControls

System.Drawing

Imaging

Drawing2D

Text

Printing

System.WinForms

Design ComponentModel

.NET Framework 名稱空間

Page 13: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Base FrameworkSystemSystem

ThreadingThreading

TextText

ServiceProcessServiceProcess

SecuritySecurity

ResourcesResources

ReflectionReflection

NetNet

IOIO

GlobalizationGlobalization

DiagnosticsDiagnostics

ConfigurationConfiguration

CollectionsCollections

RuntimeRuntime

SerializationSerialization

RemotingRemoting

InteropServicesInteropServices

Page 14: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Data And XMLSystem.DataSystem.Data

System.XmlSystem.Xml

SQLTypesSQLTypes

SQLClientSQLClient

CommonCommon

OleDbOleDb

SerializationSerialization

XPathXPath

XSLTXSLT

Page 15: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Page 開發

伺服器端伺服器端 UIUI 控制項控制項 Provide VB-Like ModelProvide VB-Like Model

編譯過的程式語言編譯過的程式語言 VB, C#, JScript, COBOL, etc.VB, C#, JScript, COBOL, etc.

程式碼和內容分離程式碼和內容分離 Developers and designers can work independentlyDevelopers and designers can work independently

支援多種客戶端支援多種客戶端 Rich DHTML, HTML 3.2, etc…Rich DHTML, HTML 3.2, etc… Mobile controls for small devicesMobile controls for small devices

Page 16: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET 部署

““XCOPY” XCOPY” 部署部署 不需要註冊不需要註冊 不需要暫停伺服器不需要暫停伺服器

支援所有的支援所有的 web web 上資源上資源 Web pages, web servicesWeb pages, web services Compiled components (DLL)Compiled components (DLL) Configuration files( Web.Config )Configuration files( Web.Config )

Page 17: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Operating SystemOperating System

Common Language RuntimeCommon Language Runtime

Base Class LibraryBase Class Library

ADO.NET and XMLADO.NET and XML

ASP.NETASP.NETWeb Forms Web ServicesWeb Forms Web Services

Mobile Internet ToolkitMobile Internet Toolkit

WindowsWindowsFormsForms

Common Language SpecificationCommon Language Specification

VBVB C++C++ C#C# JScriptJScript J#J#V

isua

l Stu

dio

.NE

TV

isua

l Stu

dio

.NE

T

Framework, Languages, And Tools

Page 18: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

語言

.NET 平台和你所使用語言無關– 所有語言使用的 .NET 類別都是一樣的– 你可以使用已經熟悉語言

Common Language Specification– 所有語言保證可使用的功能

適用語言– VB, C++, C#, J#, JScript

協力廠商所開發語言– APL, COBOL, Pascal, Eiffel, Haskell, ML, Oberon,

Perl, Python, Scheme, Smalltalk…

Page 19: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

標準化

.NET Framework 和 C# 的子集合已經遞交給ECMA– 規格制定已經完成– 2001 年 12 月最後投票決定– 支援廠商包含 Intel, Hewlett-Packard

Page 20: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

建置環境 (1)

熟悉三種開發環境 Browser-IE( 提供使用物件、 JavaScript) Web Server-IIS(WWW 、 SMTP 、安全 ) DataBase-SQL( 建置維護調校、 TSQL)

熟悉開發工具Visual Studio .NET

Page 21: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

建置環境 (2)

Visual Studio .NET 認識 版本 (February 13, 2002)

安裝 說明

Page 22: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

整合開發介面Help 、 Server Explorer

簡易部署 (.NET Framework 特性 --copy) IL 特性

Metadata- 描述程式執行時所需的資訊,是所有資料的資訊集區。例如 :

程式所引用的類別、記憶體資源的配置、 IL 編譯成 Native Code 所需資訊及安全性管理。

Visual Studio.NET 特性

程式碼

程式碼 Native Code

Native Code

IL

編譯器

JIT

Page 23: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Visual Studio.NET

安裝準備 beta1-beta2-RC1-RTM- 正式 Hardware Requirements : Processor-Recommended: Pentium III-class, 600MHz1 RAM-Recommended: 128 MB for 2000 Professional, 256 MB for

2000 Server, 96 MB for Workstation, and 192 MB for NT 4.0 Server, 192 MB for XP Professional

Available Hard Disk Space :500 MB on system drive, 3 GB installation drive

Operating System-Windows® 2000, Windows XP, or Windows NT 4.0

Page 24: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Visual Studio.NET

Software Requirements: Identifying software requirement issues Develop ASP Web applications and XML Web services: Internet Information Services (IIS) Compile code related to Microsoft Windows Message Queuing

(MSMQ) :Message Queuing Services Debug code on remote machines:Visual Studio Remote Debugger Use source code control to version stored procedures : Visual Studio 6.0 Stored Procedure Versioning

Visual SourceSafe Microsoft SQL Server 注意 : 1.Windows NT 4.0 does not support ASP.NET, COM+, or multi-processor garbage

collection. These features are only available on Windows 2000 and Windows XP or later.

2.URLScan 安裝引響 WebService

Page 25: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Visual Studio.NET

Performance TipsGeneral tips: Turn off virus checking on devenv.exe. Defragment your hard drive after installing the product. Check the recommended RAM and processor requirements for the edition you

have installed, and consider upgrading your memory or disk speed. ( 參考 Hardware Requirements) While you have IDE open: Close tool windows before you shut down that you do not use at start up to

increase your start up speed the next time you launch. Do not choose to display the Properties window on start up of the integrated

development environment (IDE). The Properties window displays automatically when you open a solution.

Do not choose to display the Start Page or Dynamic Help on start up. While this option can increase your start up speed, these windows do provide valuable information, so displaying them on start up can be worthwhile.

Page 26: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

建置環境 (3)

在 C:\Inetpub\wwwroot 目錄下建 tn 子目錄 開啟電腦管理 \Internet Information Services\

Default Web Site 新增虛擬目錄 \ 別名 \ 選擇目錄 \ 完成

Page 27: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASPASPX 好處

可將程式碼與網頁的內容分離 針對一些共通性的工作 , 減少程式碼的撰寫 允許程式碼編譯 、元件的部署更容易 寫出的程式適合於 Web Farms 環境中執行 針對應用程式架構提供更好的工具絕佳的開發平台

– 豐富的網頁架構 – “ Web Forms”– 支援 XML Web Services– 模組化的結構、大量的支援工具

部署容易、加強穩定性及可用性 提昇效能及延展性

Page 28: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

Microsoft® ASP.NET is the next generation technology for Web application development.

*Web Forms are the heart and soul of ASP.NET.

*Web Forms are the User Interface (UI) elements that give your Web applications their look and feel. Web Forms are similar to Windows Forms in that they provide properties, methods, and events for the controls that are placed onto them.

*Web Forms are made up of two components: the visual portion (the ASPX file), and the code behind the form, which resides in a separate class file

Page 29: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

The Purpose of Web Forms:Web Forms and ASP.NET were created to overcome some of the

limitations of ASP. These new strengths include: •Separation of HTML interface from application logic •A rich set of server-side controls that can detect the browser and send out appropriate markup language such as HTML •Less code to write due to the data binding capabilities of the new server-side .NET controls •Event-based programming model that is familiar to Microsoft Visual Basic® programmers •Compiled code and suppo rt for multiple languages, as opposed to ASP which was interpreted as Microsoft Visual Basic Scripting (VBScript) or Microsoft Jscript® Allows third parties to create controls that provide additional functionality

Page 30: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

There are two types of controls that you can use to create your user interface: HTML controls and Web Form controls. HTML Controls<input type="text" id=txtFirstName size=25> Any HTML element can be marked to also run as an HTML control when the Web Form is processed on the server by adding "runat=server" to the tag:

<input type="text" id=txtFirstName size=25 runat=server>

Page 31: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

HTML controls available in ASP.NET( 右圖 )Common client-side events ( 下圖 )

Control Description

OnBlur: Control loses focus

OnChange: Contents of the control are changed

OnClick: Control is clicked on

OnFocus: Control receives focus

OnMouseOver: Mouse moves over this control

Page 32: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

Web Form Controls:Web Form controls are created and run on the Server just like the HTML controls. they render the appropriate HTML and send that HTML into the output stream. All Web Form controls inherit from a common base class, namely the System.Web.UI.WebControls class. This base class implements a set of common properties that all of these controls will have.

Page 33: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

Server-side controls used in ASP.NET and Web Forms 重要特性 :All of these controls change their output based on the type of browser detected for the user. If the user's browser is IE, a richer look and feel can be generated using some DHTML extensions. If a down-level browser is detected (something other than IE), normal HTML 3.2 standard is sent back to the user's browser.

Page 34: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to ASP.NET and Web Forms---Overview of ASP.NET and Web Forms

宣告 "tags"– HTML, server controls tags, static text

允許程式碼與網頁分離

<<tags>tags>codecode

<<tags>tags> codecode

single filesingle file separate files (“code-behind”)separate files (“code-behind”)

Page 35: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to Web Forms Pages

Components of Web Forms In Web Forms pages, the user interface programming

is divided into two distinct pieces: the visual component and the logic

Web Forms File Structure

Page 36: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

執行程序

ASPXFile

RequestRequest

ASPXASPX

EngineEngine

ParseParse

(( 解析解析 ))

Gen’dPageClass

GenerateGenerate

ResponseResponse

RequestRequest

InstantiateInstantiate

ResponseResponse

Code-Code-behindbehindclassclass

PagePage

ClassClass Instantiate, Instantiate, Process and Process and Render(Render( 轉換轉換

))

CompileCompile(( 編譯編譯 ))

Page 37: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Web Forms 可包含 …

程式碼區塊程式碼區塊靜態文字靜態文字控制標籤控制標籤HTML HTML 標籤標籤

指令碼指令碼

資料繫結運算式資料繫結運算式

Render blocks Render blocks <%= … %><%= … %> Client scriptClient script Server comments Server comments <%-- --%><%-- --%>

Page 38: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Server Controls

封裝 Server controls 的行為– 提供類似 Visual Basic 開發模式– 宣告 runat="server"

產生 HTML 並輸出到用戶端– 可支援多種類型用戶端– DHTML, HTML 3.2, WML, etc.

處理用戶端的輸入– 繫結資料於 Forms collection– 回應正確的事件

Page 39: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

標準 ASP.NET Controls

Html Controls– 與 HTML 標籤相對應

Web Controls– 轉換符合用戶端的 HTML 及 script– 一致且具強型別的物件模式– 轉換成單一的 HTML 標籤 , 如 :

Button, TextBox, DropDownList

– 轉換成複合的 HTML 標籤 + script , 如 : DataGrid, Calendar, Validators

Page 40: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

其他 Microsoft® Controls

Mobile Controls– 轉換 Web UI 適合於行動裝置 – WAP phones, Msft Mobile Explorer phones,

Win CE devices, etc. – Mobile Internet Toolkit

Internet Explorer Controls– 利用 Internet Explorer 5.5 DHTML Behaviors,

讓使用者的經驗更愉悅

Page 41: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

控制元件的事件處理程序

事件處理程序– 由用戶端觸發– 由伺服器端執行相對應的程式

需要回應到相同的網頁 ViewState 協助儲存控制元件之前的狀態

– 在 Form 標籤中利用 hidden field– 可以取消儲存狀態 : EnableViewState=false

提示 : 資料繫結會重置控制元件的狀態

Page 42: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Page/Control 事件生命週期

Page_LoadPage_Load

Page_UnloadPage_Unload

Textbox1_ChangedTextbox1_Changed

Button1_ClickButton1_Click

((on PostBack)on PostBack)

1. 1. Change EventsChange Events

2. 2. Action EventsAction Events

Page is loaded, control hierarchy initializedPage is loaded, control hierarchy initialized

Page is disposedPage is disposed

Control hierarchy is renderedControl hierarchy is rendered

Page 43: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Server Control Event Model(1)

Intrinsic Event Set—onclick 、 onchange….. Events that occur often , such as an onmouseover event,

are not supported for server controls.

Page 44: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Server Control Event Model(2)

Postback and Non-Postback Events in Web Server Controls觸發於用戶端每次呼叫網頁時

判斷 Page.IsPostBack 的值為真或偽 [Note] For the AutoPostBack property to work

properly, the user's browser must be set to allow scripting.

Page 45: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Server Control Event Model(3)

Event Delegates in Web Forms Pages private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); }

this.Button1.Click += new System.EventHander(this.myEventHandler);

If the AutoEventWireup attribute of the Page directive is set to true (or if it is missing, since by default it is true), the page framework calls page events automatically, specifically the Page_Init and Page_Load methods.

Page 46: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

ASP.NET Server Control Event Model(4)

Application and Session Events Application events are raised for all requests to an application. For

example, Application_BeginRequest is raised when any Web Forms page or XML Web service in your application is requested.

Session events are similar to application events (there is a Session_OnStart and a Session_OnEnd event), but are raised with each unique session within the application.

參考 -----Global.asax

Page 47: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Test01.aspx- Test02.aspx

<%@ Page Language="C#"%><html><body><div align="center"><form action=“test1.aspx" method="post"> <%

for (int i=1; i<8; i++) {%> <font size="<%=i%>"> Welcome to ASP.NET</font><br> <% }%> </form></div></body></html>---------------------------------------------------------------------------------------------------------------------------------<%@ Page Language="C#"%><html><body><div align="center"> <form action="test1.aspx" method="post"><%

for(int i=1;i<8;i++){

Response.Write("<font size="+i+">Welcome to ASP.NET</font><br>"); }%></form></div></body></html>

Page 48: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Test03.aspx

<%@ Page Language="C#" %><html><body><div align="center"><form action="test3.aspx"> <h5>Name:<input name="Name" type=text value="<%=Request.QueryString["Name"]%>"> Category:<select name="Category" size=1> <% String[] values={"HTML", "XML/XSL", "ASP.NET"}; for(int i=0; i<values.Length; i++){ %> <option <%if(Request.QueryString["Category"]==values[i]){ Response.Write("selected"); } %>> <%=values[i]%> </option> <%}%> </select> <input type=submit name="Lookup" value="Lookup"></h5> <p> <% if(Request.QueryString["Lookup"] != null) { %> Hi <%=Request.QueryString["Name"] %>,you selected<%=Request.QueryString["Category"]

%> <% } %></form></div></body></html>

Page 49: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Test04.aspx

<%@ Page Language="C#"%><html><head></head><body><!-- #Include File="header.inc" --><h3>Server-Side Includes</h3><p>範例 </p><!-- #Include File="footer.inc" --></body></html>

Page 50: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Introduction to Web Forms Controls

HTML controls and Web controls Validation Controls User controls(Pagelets-Beta1)

Page 51: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Test05.aspx

<html><head><script language="C#" runat="server">void SubmitBtn_Click(object Source,EventArgs e) { if(Password.Value == "ASP.NET") message.InnerHtml="Password 正確 "; else message.InnerHtml="password 不正確 ";}</script></head><body><form runat=server><table cellpadding=5><tr><td>Login: </td><td><input id="Name"

runat=server></td></tr><tr><td>Password: </td><td><input id="Password" type=password

runat=server></td></tr><tr> <td colspan=2 align="center"><input type=submit value="Enter"

OnServerClick="SubmitBtn_Click" runat=server></td></tr></table><p><span id="message" style="font-size:18; color:maroon" runat=server/></form></body></html>

Page 52: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Web controls offer the following advantages over HTML controls

They feature a typed object model, unlike HTML controls in which values are all strings.

They automatically generate the correct HTML for both downlevel (HTML 3.2) and later (HTML 4.0) browsers.

They can be used to easily build more complex functionality, such as accessing and manipulating data-bound controls.

Page 53: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Web Controls Declaration Syntax: <asp: . />

<asp:elementName attributes runat="server" />

Page 54: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Test06.aspx

<html> <script language="C#" runat=server> void SubmitBtn_Click(Object sender, EventArgs e) { Message.Text = "Hi " + Name.Text + ", you selected " +

Category.SelectedItem;} </script> <body> <div align="center"> <form action="webcontrolpostback.aspx" method="post"

runat="server"> <h5>Name: <asp:textbox id="Name" runat="server"/> Category: <asp:dropdownlist id="Category" runat=server>

<asp:listitem>HTML</asp:listitem> <asp:listitem>XML/XSL</asp:listitem> <asp:listitem>ASP.NET</asp:listitem> </asp:dropdownlist> <asp:button type=submit text="Post" OnClick="SubmitBtn_Click"

runat="server"/></h5> <p><asp:label id="Message" runat="server"/> </form> </div> </body> </html>

Page 55: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Databinding Syntax: <%# . %>-test07.aspx

<html><script language="C#" runat=server>string dept_name=""; private void Page_Load(Object sender, EventArgs e) { dept_name="sss"; DataBind(); }</script><body><div align="center"><form action="test7.aspx" method="post" runat="server"><asp:label runat="server" text='<%# dept_name %>'/></form></div></body></html>

Page 56: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Databinding-test08.aspx

<html><script language="C#" runat=server>private void Page_Load(Object sender, EventArgs e) { ArrayList dept=new ArrayList(); dept.Add("a"); dept.Add("b"); dept.Add("c"); dept.Add("d"); MyList.DataSource=dept; DataBind(); }</script> <body><div align="center"><form action="test7.aspx" method="post" runat="server"><asp:ListBox id="MyList" runat="server"/></form></div></body></html>

Page 57: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Text Display-Label(test09.aspx)

The Label control is used to display text at a set location on a page.

The text may be static, bound to a data source, or dynamically set through script.

<html> <head>

<script language="C#" runat="server">

void Page_Load(Object Src, EventArgs E) {

Message.Text=“ 最後存取時間 " + DateTime.Now;

}

</script>

</head> <body>

<p><asp:label id="Message" font-size="12pt" font-bold="true" runat="server"/> </body> </html>

Page 58: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Input Controls(TextBox, CheckBox, and RadioButton controls. )

The collection of web controls includes controls that accept user input on the page that is then processed on the server.

The TextBox control supports various modes, which allow it to be used for single-line, multi-line, and password input:

Include 於 <form runat=“server”></form> 建置 Web.Config 檔 ,debug

Page 59: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Input Controls(TextBox, CheckBox, and RadioButton controls. )-test10.aspx

<asp:TextBox runat="server" Text="TextBox1"/>

<asp:TextBox runat="server" TextMode="Multiline"Rows="3">TextBox1</asp:TextBox>

<asp:TextBox runat="server" TextMode="Password"/>

Page 60: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Input Controls(TextBox, CheckBox, and RadioButton controls. )-test11.aspx

The CheckBox control is used to generate a check box that can be toggled between selected and cleared states:

<asp:CheckBox runat="server" Text="CheckBox1" Checked="True"/> The RadioButton control is similar to a CheckBox

control, but is typically used along with other RadioButton controls in a group to provide a set of mutually exclusive choices:

<asp:RadioButton runat="server" Text="RadioButton1" GroupName="Group1" Checked="true"/>

<asp:RadioButton runat="server" Text="RadioButton2" GroupName="Group1"/>

Page 61: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Input Controls(TextBox, CheckBox, and RadioButton controls. )-test11.aspx

<html> <head> <script language="C#" runat="server"> void SubmitBtn_Click(Object Sender, EventArgs e) { msgText.Text=Text1.Text; if (Check1.Checked) msgCheck.Text="Checked"; else msgCheck.Text="Not checked"; if (Radio1.Checked) msgRadio.Text=Radio1.Text; else if (Radio2.Checked)

msgRadio.Text=Radio2.Text; } </script></head> <body> <h3>Input Controls</h3> <form runat="server"> <p><asp:textbox runat="server" id="Text1"/> <asp:label runat="server" id="msgText"/> <p><asp:checkbox runat="server" id="Check1" text="Check1"/> <asp:label runat="server" id="msgCheck"/> <p><asp:radiobutton runat="server" id="Radio1" text="Radio1" groupname="Group1" checked /> <asp:radiobutton runat="server" id="Radio2" text="Radio2" groupname="Group1"/> <asp:label runat="server" id="msgRadio"/> <p><asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/> </form> </body> </html>

Page 62: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Selection(DropDownList, ListBox, CheckBoxList, and RadioButtonList)

allow the user to select between choices or options they are presented with.

The DropDownList control is used to allow a single selection from a number of choices displayed as a drop-down list:

<asp:DropDownList runat="server"> <asp:ListItem Text="Choice1" Value="1" selected/> <asp:ListItem Text="Choice2" Value="2"/> </asp:DropDownList>

The ListBox control is used to generate a scrollable list of choices and allows either a single option or multiple options to be selected:

<asp:ListBox runat="server" SelectionMode="Multiple"> <asp:ListItem Text="Choice1" Value="1" selected/> <asp:ListItem Text="Choice2" Value="2"/> </asp:ListBox>

The CheckBoxList server control provides a multiple-selection checked list. Like other list controls,

The RadioButtonList control is very similar to the CheckBoxList control. It uses a group of RadioButton controls instead to create a mutually exclusive set of choices:

Page 63: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Selection(DropDownList, ListBox, CheckBoxList, and RadioButtonList)

Test12.aspx Test13.aspx

Page 64: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Form Submission and Post-Back

The following controls are used to submit the page, along with user-entered values, back to the server so they can be processed by the logic in your page. These controls generate a Click event on the server, which you can handle in code.

Button 、 LinkButton 、 ImageButton Test14.aspx

Page 65: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Navigation

The HyperLink control is used to generate a link that allows navigation to other URLs:

<asp:HyperLink runat="server" Text="Follow Me" NavigateUrl="navcontrol.aspx"/>

If the ImageURL property is specified, an image is rendered that users can click to navigate:

<asp:HyperLink runat="server" Text="Follow Me" ImageUrl="billg.gif"

NavigateUrl="http://www.tn.edu.tw"/> Test14.aspx

Page 66: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Image Display

The Image control can be used to display an image within the page:

<asp:Image runat="server" ImageUrl="net.gif" />

Test15.aspx

Page 67: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Layout Controls

The Panel control is typically used as a grouping control. This control is mostly useful as a container for dynamically generated controls or for showing and hiding a group of controls.

A Panel control is rendered as an HTML <DIV> element and, by default, does not have any visual appearance:

Test16.aspx

Page 68: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Layout Controls

The Table control, along with the associated TableRow and TableCell controls, allows you to create tables and tabular layouts programmatically:

Test17.aspx

Page 69: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Date Selection

The Calendar control provides various ways of date selection, including a range of dates by week or month. The control implements an OnSelectionChanged event, which is used to trigger a user-defined handler whenever the selected date changes.

Test18.aspx

Page 70: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

List-Bound Controls

The list-bound controls are used to render the contents of an associated or bound data source, which can be any data from a static array or dynamically retrieved from a database. They offer the ability to create a variety of custom and standard layouts. ASP.NET includes three list-bound controls: the Repeater, the DataList, and the DataGrid.

Repeater-----test22.aspx 、 test21.aspx DataList-----test20.aspx DataGrid-----test19.aspx

Page 71: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Miscellaneous

The AdRotator control is used to display banner advertisements on a Web page. AdRotator presents ad images that, when clicked, navigate to a new web location. Each time the page is loaded into the browser, an ad is selected based on a predefined rotation.

<asp:AdRotator runat="server" AdvertisementFile="AdsList.xml"></asp:AdRotator>

Test23.aspx

Page 72: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Validation Controls(1)

The RequiredFieldValidator control is used to ensure the user fills in the input controls that require an entry

The RangeValidator control is used to check that the user's entry falls into a valid range of values, that is, a user's entry is between specified lower and upper boundaries. Ranges can be within pairs of numbers, dates and alphabetic characters. Boundaries can be expressed as constants or as values derived from another control.

The CompareValidator control is used to compare a user's entry against a constant value or a property value of another control using a comparison operator (less than, equal, greater than, and so on).

Page 73: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Validation Controls(2)

The RegularExpressionValidator control is used to check that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.

The ValidationSummary control displays a summary of the validation errors for all of the validators on a page.

Validation controls will be the subject of a more detailed article later on in this series.

Page 74: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Xml Web Server Control

This control displays an XML document or the results of an XSL Transform

Test24.aspx

Page 75: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

User Controls

自行開發的 Server controls 內容類似網頁 , 但副檔名為 .ascx 完整的封裝應用

– 支援巢狀的控制元件– 分離的 Namespace– 分離的程式語言

良好的分工模式 提供不同網頁與 Web 應用程式可重複使用的控制元件

Page 76: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Creating a User Control

using a text or HTML editor user controls do not include the <html>,

<body>, and <form> elements around the content.

[Note] All properties and methods of any ASP.NET server controls contained in a user control are promoted to public properties and methods of the user control when it is included in a Web Forms page.

第一步 ( 建立 Script)-----test25.ascx第二步 ( 建立 UI)-----test25.ascx

Page 77: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

<%@ Register %> 指令

在網頁的 Web Forms 中註冊 User Control :

<%@ Register

TagPrefix="Acme"

TagName=“login"

Src=“test25.ascx" %>

<Acme:login runat="server"/>

Page 78: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

Including a User Control in a Web Forms Page

•參考 test25.aspx•第三步 (Manipulating User Control Properties) 參考 test26.aspx

Page 79: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

以程式的方式使用 User Controls

先自定 user control ClassName=“fromtest27” 載入 user control

Page.LoadControl(test27.ascx) 自訂執行個體 :

Control c1 = LoadControl("test27.ascx");

((fromtest27)c1).Text="test from reference"; 將 user control 加入適當的物件中 :

Page.Controls.Add(c1); 參考 test27.aspx

Page 80: NET 開發 (ASP.NET) 台南市教育局網路中心 在職教師校務行政系統程式 開發進階班 asp@mail.tn.edu.tw 行政網路組

VS .NET Code Behind

Demo 轉換成 Code Behind 型態