Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# and the NET Platform, Second Edition - Andrew Troelsen.pdf
Скачиваний:
67
Добавлен:
24.05.2014
Размер:
22.43 Mб
Скачать

Submitting the Form Data (GET and POST)

C# and the .NET Platform, Second Edition

 

by Andrew Troelsen

ISBN:1590590554

Now that you have a simple HTML page, you need to examine how to transmit the form data back to the

Apress © 2003 (1200 pages)

Web server for processing. When you build an HTML form, you typically supply an action attribute on the

This comprehensive text starts with a brief overview of the

opening <form> tagC# tolanguagespecifyandthethenrecipientquicklyof themovesincomingto key formtechnicdatal and. Possible receivers include mail servers, other HTMLarchitecturalfiles, an issuesActiveforServer.NETPagedev lopers(classic. or .NET), and so forth. For this example, you use a classic ASP file named ClassicAspPage.asp. Update your default.html file by specifying the following attribute in the opening <form> tag, as shown here:

Table of Contents

C# and the .NET Platform, Second Edition

<form name="MainForm" ID="MainForm"

Introduction

action="http://localhost/Cars/ClassicAspPage.asp" method = "GET">

Part One - Introducing C# and the .NET Platform

...

Chapter 1 - The Philosophy of .NET

</form>

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

ChapterThis extra3 -attributeC# LanguagespecifiesFundamentalsthat when the Submit button for this form is clicked, the form data should be Chapsenterto4the-ClassicAspPageObject-Oriented .Programmingasp file locatedwithwithinC# the Cars virtual directory on the current machine (i.e.,

localhost). When you specify method = "GET" as the mode of transmission, the form data is appended to

Chapter 5 - Excepti ns and Object Lifetime

the query string as a set of name/value pairs separated by ampersands. For example:

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chttp://localhost/Cars/apter 8 - Advanced C# Type ClassicASPPageonstruction Techniques.asp?txtUserName=

PartAndrew&txtPassword=thisismypasswordThree - Programming with .NET Assemblies &btnSubmit=Submit

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

The other method of transmitting form data to the Web server is to specify method = "POST":

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

< form name="MainForm" ID="MainForm"

Chapter 12 - Object Serialization and the .NET Remoting Layer

action="http://localhost/Cars/ClassicAspPage.asp" method = "POST">

Chapter 13 - Building a Better Window (Introducing Windows Forms)

...

Chapter 14 - A Better Painting Framework (GDI+)

</form>

Chapter 15 - Programming with Windows Forms Controls Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

In this case, the form data is not appended to the query string, but instead is written to a separate line

Part Five - Web Applications and XML Web Services

within the HTTP header. Using POST, the form data is not directly visible to the outside world and is

Chapter 18 - ASP.NET Web Pages and(moreW b Controls

therefore a wee bit more secure importantly, POST data is not limited by character length). For the Chaptertime being,19 - makeASP.NETuseWebof HTTPApplicationsGET to send the form data to the receiving *.asp page.

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Chapter 19 - ASP.NET Web Applications
Chapter 18 - ASP.NET Web Pages and Web Controls
Part Five - Web Applications and XML Web Services
<%@ language="VBScript" %>
Part Two - The C# Programming Language
Chapter 2 - Building C# Applications

Building a Classic# nd theActive.NET Platform,ServerS condPageEdition

by Andrew Troelsen

ISBN:1590590554

A classic Active Server Page is a hodgepodge of HTML and server-side script code. If you have never

Apress © 2003 (1200 pages)

worked with classic ASP, understand that the goal of ASP is to dynamically build HTML on the fly using

This comprehensive text starts with a brief overview of the

server-side scriptC#logiclanguageand a smalland thensetquiof classickly movesCOMtoobjectskey technicaland relatedand COM libraries. For example, you may have a serverarchitectural-side VBScriptissues for(or.NETJavaScript)developersblock. that reads a table from a data source using classic ADO and returns the rows as a generic HTML table.

TableFor thisof Contentsexample, the ASP page uses the intrinsic ASP Request COM object to read the values of the C#incomingand theform.NET dataPlatform,(appendedSecondtoEditionthe query string) and echo them back to the caller (not terribly exciting,

but it makes the point). The server-side script logic will make use of VBScript (as denoted by the

Introduction

"language" directive).

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

To do so, create a new HTML file using Visual Studio .NET and save this file under the name ClassicAspPage.asp into the folder to which your virtual directory has been mapped. Next, update your

*.asp file with the following HTML and scripting logic:

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

<html>

Chapter 6 - Interfaces and Collections

<head>

Chapter 7 - Callback Interfaces, Delegates, and Events

<title>The Cars Page</title>

Chapter 8 - Advanced C# Type Construction Techniques

</head>

Part Three - Programming with .NET Assemblies

<body>

Chapter 9 - Understanding .NET Assemblies

<h1 align="center">Here is what you sent me:</h1>

Chapter 10 - Processes, AppDomains, Contexts, and Threads

<P align="center"> <b>User Name: </b>

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

<%= Request.QueryString("txtUserName") %> <br>

Part Four - Leveraging the .NET Libraries

<b>Password: </b>

Chapter 12 - Object Serialization and the .NET Remoting Layer

<%= Request.QueryString("txtPassword") %> <br>

Chapter</P>13 - Building a Better Window (Introducing Windows Forms)

Chapter</body>14 - A Better Painting Framework (GDI+)

Chapter</html>15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

The first thing to be aware of is that an *.asp file begins and ends with the standard <html>, <head>, and <body> tag pairs. Here, you use the classic ASP Request COM object, which like any COM type supports a number of properties, methods, and events. You call the Request.QueryString() method to examine the

values contained in each HTML widget submitted via method = "GET".

Chapter 20 - XML Web Services

IndexAlso note that the <%= ...%> notation is a shorthand way of saying "Insert the following directly into the

ListHTTPof Figuresresponse." To gain a finer level of flexibility, you could interact with the ASP Response COM object

Listwithinof Tablesfull script block (denoted using the <%, %> notation). You have no need to do so here; however, here is a simple example:

<!-- Send back the info they gave us -->

<center>

...

<%

Dim pwd

pwd = Request.QueryString("txtPassword")

Response.Write (pwd)

%>

</center>

Obviously, the Request and Response objects of classic ASP provide a number of additional members beyond Write() and QueryString(). Furthermore, classic ASP also defines a small number of additional

Chapter 12 - Object Serialization and the .NET Remoting Layer
Part Four - Leveraging the .NET Libraries

COM objects (Session, Server, Application, and so on) that you can use while constructing your Web

C# and the .NET Platform, Second Edition

application.

by Andrew Troelsen

ISBN:1590590554

Note Under ASPpress.NET,© 2003these(1200COMpages)objects are effectively dead. However, you will see that the SystemThis.Webcomprehensive.UI.Page basetextclasstartsdefineswith identicallya brief overviewnamedofpropertiesthe that provide similar functionalityC# language. and then quickly moves to key technical and

architectural issues for .NET developers.

To test the ASP logic, simply load the default.htm page from a browser and submit the form data. Once the script is processed on the Web server, you are returned a brand new (dynamically generated) HTML

Table of Contents

display (Figure 18-8).

C# and the .NET Platform, Second Edition

Introduction

Part

Chapter

Chapter

Part

Chapter

Chapter

Chapter

Chapter

Chapter

Events

Figure 18-8: The dynamically generated HTML

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Responding to POST Submissions

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Currently, your default.htm file specifies HTTP GET as the method of sending the form data to the target *.asp file. Using this approach, the values contained in the various GUI widgets are appended to the end of

the query string. It is important to note that the ASP Request.QueryString() method is only able to extract

Chapter 13 - Building a Better Window (Introducing Windows Forms) data submitted via the GET method.

Chapter 14 - A Better Painting Framework (GDI+)

ChapterIf you would15 - Programmingrather submitwithformWindowsdata to Formsthe WebControlsresource using HTTP POST, the Request.Form collection

Chcanpterbe16used- Theto readSystemthe.IOvaluesNamespaceon the server. For example:

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

<body>

Chapter 18 - ASP.NET Web Pages and Web Controls

<h1 align="center">Here is what you sent me:</h1>

Chapter<P19align="center">- ASP.NET Web Applications

Chapter 20 -<b>UserXMLW ServicesName: </b>

Index <%= Request.Form("txtUserName") %> <br>

<b>Password: </b>

List of Figures

List of Tables<%= Request.Form("txtPassword") %> <br>

</P>

</body>

That wraps up the Web development primer. Hopefully those of you who are new to Web development have a better understanding of what goes on behind the scenes. Now, before we check out how the .NET platform improves upon the current state of affairs, let's take a brief moment to bash (which is to say "critique") classic ASP.

SOURCE The default.html and ClassicAspPage.asp files are included under the ClassicAspCars

CODE subdirectory.

The Problem(s)C# andwiththe .NETClassicPlatform,ASPSecond Edition

by Andrew Troelsen

ISBN:1590590554

While many successful Web applications have been created using classic ASP, this architecture is not

Apress © 2003 (1200 pages)

without its downside. Perhaps the biggest downfall of classic ASP is the same point that makes it a

This comprehensive text starts with a brief overview of the

powerful platform:C#serverlanguage-sideandscriptingthen quicklylanguagesmoves. Scriptingto key techniclanguagesl andsuch as VBScript and JavaScript are interpreted, typelessarchitecturalentitiesis uesthatfordo.NETnot lenddevelopersthemselves. to robust OO programming techniques. For example, under classic ASP, there was no concept of classical inheritance, attributes, strongly typed data, or classical polymorphism (although the interface-driven nature of classic COM did allow for interface-

Table of Contents based polymorphism).

C# and the .NET Platform, Second Edition

Another problem with classic ASP is the fact that an *.asp page does not yield very modularized code.

Introduction

Given that ASP is a blend of HTML and script in a single page, most ASP Web applications are a

Part One - Introducing C# and the .NET Platform

confused mix of two very different programming techniques. While it is true that classic ASP allows you to

Chapter 1 - The Philosophy of .NET

partition reusable code into distinct include files, the underlying object model does not support true

Chapter 2 - Building C# Applications

separation of concerns. In an ideal world, a Web framework would allow the presentation logic (i.e., HTML

Part Two - The C# Programming Language

tags) to exist independently from the business logic (i.e., functional code).

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

A final issue to consider here is the fact that classic ASP demands a good deal of boiler-plate, redundant

Chapter 5 - Exceptions and Object Lifetime

script code that tends to repeat between projects. Almost all Web applications need to validate user input,

Chapter 6 - Interfaces and Collections

repopulate the state of HTML widgets before emitting the HTTP response, generate an HTML table of

Chdata,pter 7 - Callback Interfaces, Delegates, and Events

and so forth.

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Соседние файлы в предмете Программирование