锘??xml version="1.0" encoding="utf-8" standalone="yes"?>yy6080久久,久久精品夜夜夜夜夜久久,久久免费视频1http://www.shnenglu.com/mzty/category/1773.html<br/> <br/> <a href = "http://www.shnenglu.com/mzty/archive/2007/03/02/19109.html"><font size = 5 color ="#00FFFF">{C++ 鍩虹}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/08/13/29922.html"><font size = 5 color ="#00FFFF">{C++ 楂樼駭}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/04/16/22064.html"><font size = 5 color ="#00FFFF">{C#鐣岄潰錛孋++鏍稿績綆楁硶}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/03/04/19163.html"><font size = 5 color ="#00FFFF">{璁捐妯″紡}<font/></a> <a href = " http://www.shnenglu.com/mzty/archive/2007/03/04/19167.html"><font size = 5 color ="#FF0000">{C#鍩虹}<font/></a> zh-cnMon, 19 May 2008 15:13:50 GMTMon, 19 May 2008 15:13:50 GMT60緗戠粶緙栫▼鍩虹(web application primer (http,html(cgi,isapi),asp,asp.net))http://www.shnenglu.com/mzty/archive/2006/05/15/7212.html姊﹀湪澶╂動姊﹀湪澶╂動Mon, 15 May 2006 09:41:00 GMThttp://www.shnenglu.com/mzty/archive/2006/05/15/7212.htmlhttp://www.shnenglu.com/mzty/comments/7212.htmlhttp://www.shnenglu.com/mzty/archive/2006/05/15/7212.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/7212.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/7212.html The most proficient developers are those who possess an intimate understanding of the platforms they program and the tools they use to program them. Because it鈥檚 difficult to understand how Web forms work if you lack a more general understanding of Web applications and the protocols that drive them, the next several sections provide a working introduction to the operation of Web apps. They鈥檙e for developers who have little or no Web programming experience. If you鈥檙e already familiar with HTTP, HTML forms, and other Web-related technologies, feel free to skip ahead to the section entitled 鈥淵our First Web Form.鈥?If, however, Web apps are a new frontier for you, you鈥檒l find the following discussion helpful in building an in-depth understanding of the Web Forms programming model.

Hypertext Transfer Protocol

The Hypertext Transfer Protocol, better known as HTTP, is the protocol that drives the World Wide Web. Invented by Tim Berners-Lee ("father of the Web") and documented in RFC 2068, which is available online at www.w3.org/Protocols/rfc2068/rfc2068 , HTTP is arguably the most important network protocol ever invented, with the notable exception of TCP/IP.

HTTP defines how Web browsers and Web servers communicate with each other. It鈥檚 entirely text based, and it鈥檚 typically transmitted over TCP connections linking Web browsers to Web servers. Suppose the following HTML file is deployed on a Web server, that its name is Simple.html, and that its URL is www.wintellect.com/simple.html:
<html>
<body>
Hello,聽world
</body>
</html>

If a user types http://www.wintellect.com/simple.html into Internet Explorer鈥檚 address bar, Internet Explorer (IE) uses the Internet鈥檚 Domain Name System (DNS) to convert www.wintellect.com into an IP address (for example, 66.45.26.25). Then IE opens a socket connection to the server at that address using a well-known port number (port 80) and transmits an HTTP request similar to this one:
GET聽/simple.html聽HTTP/1.1
Accept:聽*/*
Accept-Language:聽en-us
Accept-Encoding:聽gzip,聽deflate
If-Modified-Since:聽Wed,聽24聽Oct聽2001聽14:12:36聽GMT
If-None-Match: "50b0d3ee955cc11:a78"
User-Agent:聽Mozilla/4.0.(compatible;聽MSIE.6.0;聽Windows聽NT聽5.1)
Host:聽www.wintellect.com
Connection:聽Keep-Alive
[blank聽line]

The first line of the request is called the start line. It consists of a method name (GET), the name of the resource being requested (simple.html), and an HTTP version number (1.1). GET is one of seven methods defined in HTTP 1.1; it requests a resource from a Web server. The next eight lines make up the message header. Each line, or header, contains additional information about the request, including information about the browser that originated the request (User-Agent). A blank line (a simple carriage return/line feed pair) marks the end of the message header and also the end of the request.

How does the Web server respond to the GET command? Assuming /simple.html is a valid resource identifier and security settings don鈥檛 prevent the file from being returned, the server transmits an HTTP response like this one:
HTTP/1.1聽200聽OK
Server:聽Microsoft-IIS/5.0
Date:聽Wed,聽24聽Oct聽2001聽14:12:37聽GMT
Content-Type:聽text/html
Accept-Ranges:聽bytes
Last-Modified:聽Wed,聽24聽Oct聽2001聽14:00:53聽GMT
ETag: "d02acf81975cc11:a78"
Content-Length:聽46
[blank line]
<html>
<body>
Hello,聽world
</body>
</html>

Upon receiving the response, the browser parses the HTML returned by the Web server and displays the resulting Web page. The Content-Type header identifies the returned data as HTML, while Content-Length tells the browser how much HTML was returned. The 鈥?00鈥?in the first line of the response is an HTTP status code signifying that the server fulfilled the browser鈥檚 request. The HTTP specification defines about 40 different status codes, including the infamous 401 (鈥淯nauthorized鈥? code indicating that the user isn鈥檛 authorized to view this resource.

Conversations such as these form the backbone for communications over the Web. As you surf the Web by typing URLs and clicking hyperlinks, your browser issues one GET command after another. Tools such as NetMon鈥攖he network packet-sniffing utility that comes with server editions of Windows鈥攍et you spy on the HTTP traffic flying back and forth. You don鈥檛 have to be an HTTP guru to write ASP.NET applications, but a knowledge of basic HTTP semantics and a familiarity with commonly used request and response headers are a big help in understanding the ASP.NET object model.

HTML Forms

Simple.html is a far cry from a full-blown Web application. It鈥檚 a static HTML file that solicits no user input. Real Web applications like the ones located at www.amazon.com and www.ebay.com accept input from their users, and they vary the HTML that they return to Web browsers based on the contents of that input.

At the heart of almost every genuine Web application is an HTML form. An HTML form is the portion of an HTML document that appears between <form> and </form> tags. The HTML in Figure 5-1 describes a simple form representing a Web-based adding machine. The form contains two text input fields for the user to type numbers into and an equals button that submits the form back to the server. Figure 5-2 shows how the form appears in Internet Explorer. As you can see, the browser renders an <input type=鈥渢ext鈥?gt; tag as a text input field and an <input type=鈥渟ubmit鈥?gt; tag as a push button. Similar tags can be used to create check boxes, radio buttons, list boxes, and other basic control types.

Calc.html
<html>
聽聽<body>
聽聽聽聽<form>
聽聽聽聽聽聽<input聽type="text" name="op1" />
聽聽聽聽聽聽+
聽聽聽聽聽聽<input聽type="text" name="op2" />
聽聽聽聽聽聽<input聽type="submit" value=" 聽=聽 " />
聽聽聽聽</form>
聽聽</body>
</html>

Figure 5-1
A simple HTML form.
Figure 5-2
Calc.html displayed in Internet Explorer.

A submit button (<input type=鈥渟ubmit鈥?gt;) plays a special role in an HTML form. When clicked, it submits the form to a Web server. To be more precise, the browser submits the form along with any input in the form鈥檚 controls. How the form is submitted depends on whether the <form> tag includes a Method attribute and the value of that attribute, if present. If the <form> tag lacks a Method attribute or includes a method=鈥済et鈥?attribute, the browser sends an HTTP GET command to the server with the user鈥檚 input appended to the URL in the form of a query string:
GET聽/calc.html?op1=2&op2=2聽HTTP/1.1
聽聽.
聽聽.
聽聽.
Connection:聽Keep-Alive
[blank聽line]

If, on the other hand, the <form> tag includes a method=鈥減ost鈥?attribute, the form is submitted to the server using an HTTP POST command. Rather than transmit user input in the URL, with a POST command the browser passes it in the body of the HTTP request:
POST聽/calc.html聽HTTP/1.1
聽聽.
聽聽.
聽聽.
Content-Type:聽application/x-www-form-urlencoded
Content-Length:聽11
[blank line]
op1=2&op2=2

Regardless of whether a GET or a POST command is used, when input from an HTML form is submitted back to the server, we say that a 鈥減ostback鈥?/font> has occurred. Remember that term because you鈥檒l encounter it repeatedly in this and the next several chapters.

For a first-hand look at HTML forms in action, copy Calc.html to your PC鈥檚 \Inetpub\wwwroot directory and call it up in Internet Explorer by typing the following URL:
http://localhost/calc.html

Now type 2 into each of the form鈥檚 text boxes and click the = button. As evidence that a postback occurred, observe what appears in the browser鈥檚 address bar (shown in Figure 5-3). If you change the <form> tag to

<form聽method="post">

and repeat the experiment, you won鈥檛 see any change in the URL. But the postback occurs just the same, and the Web server can access the user鈥檚 input by examining the body of the request.

Figure 5-3
Calc.html following a postback.
Server-Side Processing(cgi &&isipi)

So far, so good. As Calc.html demonstrates, building the client half of a Web application is easy. After all, it鈥檚 just HTML. The hard part is building the code that runs on the Web server. Something has to be running there to extract the user input from the URL (or from the body of the HTTP request if the postback was performed with POST instead of GET) and generate a new Web page that displays the sum of the inputs next to the = button. In other words, if the user enters 2 and 2 and clicks the = button, we鈥檇 like the Web server to respond by returning the following HTML:

<html>
聽聽<body>
聽聽聽聽<form>
聽聽聽聽聽聽<input聽type="text" name="op1" value="2" />
聽聽聽聽聽聽+
聽聽聽聽聽聽<input聽type="text" name="op2" value="2" />
聽聽聽聽聽聽<input聽type="submit" value=" 聽=聽 " />
聽聽聽聽聽聽4
聽聽聽聽</form>
聽聽</body>
</html>

Note the Value attributes added to the <input type=鈥渢ext鈥?gt; tags. Including the inputs in the page returned from the Web server following a postback perpetuates the illusion that the user is seeing one Web page when in fact he or she is seeing two in succession.

There are many ways to write applications that process input from HTML forms. One solution is an application that uses the Common Gateway Interface (CGI). CGI defines a low-level programmatic interface between Web servers and applications that run on Web servers. Applications that use it are typically written in a programming language called Perl, but they can be written in other languages as well. CGI applications read the input accompanying postbacks through server environment variables and standard input (stdin), and they write HTTP responses to standard output (stdout).CGI has a reputation for being slow because many implementations of it launch a new process to handle each incoming request. Despite this, CGI enjoys widespread use on UNIX-based Web servers. It鈥檚 rarely used on the Windows platform.

Another solution鈥攐ne that鈥檚 more likely to find favor among Windows developers鈥攊s an ISAPI extension DLL. ISAPI stands for Internet Server Application Programming Interface. ISAPI extensions are Windows DLLs that are hosted by Internet Information Services. They鈥檙e referenced by URL just like HTML files (for example, http://www.wintellect.com/calc.dll). IIS forwards HTTP requests to an ISAPI DLL by calling a special function exported from the DLL. The DLL, in turn, generates HTTP responses.ISAPI DLLs are faster than CGI applications because they (typically) run in the same process as IIS. And once loaded, they remain in memory awaiting subsequent requests. The downside to ISAPI DLLs is that they鈥檙e difficult to write. An ISAPI developer must be comfortable with the architecture of Windows DLLs and also be willing to deal with HTTP messages at a very low level.

Curious to know what an ISAPI DLL looks like? Figure 5-4 shows the C++ source code for an ISAPI DLL that implements a Web calculator identical to the one shown in Figure 5-2. The heart of the DLL is the HttpExtensionProc function, which IIS calls on each and every request. The pECB parameter points to a structure containing information about the request, including a pointer to the query string (if any) accompanying the request. If the query string is empty, this implementation of HttpExtensionProc returns an HTML page depicting an empty calculator. Following a postback, however, it parses the op1 and op2 parameters from the query string and returns an HTML page that includes the sum of the inputs. In other words, it returns precisely the HTML we set as our goal a moment ago.

ISIPI瀹炵幇:
Calc.cpp


#include聽<windows.h>
#include聽<httpext.h>
#include聽<string.h>
#include聽<stdlib.h>


int聽GetParameter聽(LPSTR聽pszQueryString,聽LPSTR聽pszParameterName);

BOOL聽WINAPI聽DllMain聽(HINSTANCE聽hInstance,聽DWORD聽fdwReason,
聽聽聽聽LPVOID聽lpvReserved)
{
聽聽聽聽return聽(TRUE);
}

BOOL聽WINAPI聽GetExtensionVersion聽(HSE_VERSION_INFO*聽pVer)
{
聽聽聽聽pVer->dwExtensionVersion聽=聽
聽聽聽聽聽聽聽聽MAKELONG聽(HSE_VERSION_MINOR,聽HSE_VERSION_MAJOR);
聽聽聽聽lstrcpy聽(pVer->lpszExtensionDesc, "Calc聽ISAPI聽Extension");
聽聽聽聽return聽(TRUE);
}

DWORD聽WINAPI聽HttpExtensionProc聽(EXTENSION_CONTROL_BLOCK*聽pECB)
{
聽聽聽聽static聽char*聽szPrePostbackMessage聽=聽
聽聽聽聽聽聽聽 "<html>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<body>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<form>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<input聽type=\"text\" name=\"op1\" />\r\n" 聽聽聽聽聽\
聽聽聽聽聽聽聽 "+\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<input聽type=\"text\" name=\"op2\" />\r\n" 聽聽聽聽聽\
聽聽聽聽聽聽聽 "<input聽type=\"submit\" value=\" 聽=聽聽\" />\r\n" \
聽聽聽聽聽聽聽 "</form>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "</body>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "</html>";

聽聽聽聽static聽char*聽szPostPostbackMessage聽=聽
聽聽聽聽聽聽聽 "<html>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<body>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<form>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<input聽type=\"text\" name=\"op1\" value=\"%d\" />\r\n" \
聽聽聽聽聽聽聽 "+\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "<input聽type=\"text\" name=\"op2\" value=\"%d\" />\r\n" \
聽聽聽聽聽聽聽 "<input聽type=\"submit\" value=\" 聽=聽聽\" />\r\n" 聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "%d\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "</form>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "</body>\r\n" 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽\
聽聽聽聽聽聽聽 "</html>";

聽聽聽聽//
聽聽聽聽//聽Build聽the聽response聽message聽body.
聽聽聽聽//
聽聽聽聽char聽szMessage[512];

聽聽聽聽if聽(lstrlen聽(pECB->lpszQueryString)聽==聽0)聽{
聽聽聽聽聽聽聽聽//聽If聽the聽query聽string聽is聽empty,聽return聽a聽page聽that聽shows
聽聽聽聽聽聽聽聽//聽an聽empty聽calculator.
聽聽聽聽聽聽聽聽lstrcpy聽(szMessage,聽szPrePostbackMessage);
聽聽聽聽}
聽聽聽聽else聽{
聽聽聽聽聽聽聽聽//聽If聽the聽query聽string聽is聽not聽empty,聽extract聽the聽user聽input,
聽聽聽聽聽聽聽聽//聽process聽it,聽and聽return聽a聽page聽that聽shows聽inputs聽and聽outputs.
聽聽聽聽聽聽聽聽int聽a聽=聽GetParameter聽(pECB->lpszQueryString, "op1");
聽聽聽聽聽聽聽聽int聽b聽=聽GetParameter聽(pECB->lpszQueryString, "op2");
聽聽聽聽聽聽聽聽wsprintf聽(szMessage,聽szPostPostbackMessage,聽a,聽b,聽a聽+聽b);
聽聽聽聽}

聽聽聽聽//
聽聽聽聽//聽Build聽the聽response聽message聽header.
聽聽聽聽//
聽聽聽聽char聽szHeader[128];
聽聽聽聽DWORD聽dwCount聽=聽lstrlen聽(szMessage);

聽聽聽聽wsprintf聽(szHeader, "Content-type:聽text/html\r\n" \
聽聽聽聽聽聽聽 "Content-Length:聽%lu\r\n\r\n",聽dwCount);

聽聽聽聽//
聽聽聽聽//聽Output聽the聽response聽message聽header.
聽聽聽聽//
聽聽聽聽HSE_SEND_HEADER_EX_INFO聽shei;
聽聽聽聽shei.pszStatus聽= "200聽OK";
聽聽聽聽shei.pszHeader聽=聽szHeader;
聽聽聽聽shei.cchStatus聽=聽lstrlen聽(shei.pszStatus);
聽聽聽聽shei.cchHeader聽=聽lstrlen聽(shei.pszHeader);
聽聽聽聽shei.fKeepConn聽=聽FALSE;

聽聽聽聽pECB->ServerSupportFunction聽(pECB->ConnID,
聽聽聽聽聽聽聽聽HSE_REQ_SEND_RESPONSE_HEADER_EX,聽&shei,聽NULL,聽NULL);聽聽

聽聽聽聽//
聽聽聽聽//聽Output聽the聽response聽message聽body.
聽聽聽聽//
聽聽聽聽pECB->WriteClient聽(pECB->ConnID,聽szMessage,聽&dwCount,聽0);聽

聽聽聽聽//
聽聽聽聽//聽Indicate聽that聽the聽request聽was聽processed聽successfully.
聽聽聽聽//
聽聽聽聽return聽HSE_STATUS_SUCCESS;
}

int聽GetParameter聽(LPSTR聽pszQueryString,聽LPSTR聽pszParameterName)
{
聽聽聽聽char*聽p聽=聽strstr聽(pszQueryString,聽pszParameterName);

聽聽聽聽if聽(p聽!=聽NULL)聽{
聽聽聽聽聽聽聽聽p聽+=聽strlen聽(pszParameterName)聽+聽1;
聽聽聽聽聽聽聽聽for聽(char*聽tmp聽=聽p;聽*tmp聽!=聽'&'聽&&聽*tmp聽!=聽0;聽tmp++)
聽聽聽聽聽聽聽聽聽聽聽聽;
聽聽聽聽聽聽聽聽int聽len聽=聽tmp聽-聽p;
聽聽聽聽聽聽聽聽char*聽buffer聽=聽new聽char[len聽+聽1];
聽聽聽聽聽聽聽聽strncpy聽(buffer,聽p,聽len);
聽聽聽聽聽聽聽聽int聽val聽=聽atoi聽(buffer);
聽聽聽聽聽聽聽聽delete聽buffer;
聽聽聽聽聽聽聽聽return聽val;
聽聽聽聽}
聽聽聽聽return聽0;
}

Figure 5-4
Source code for an ISAPI DLL.

The Active Server Pages Solution

A third solution to the problem of processing input from HTML forms on Web servers, and the one that made Windows a popular platform for Web applications in the second half of the 1990s, is Active Server Pages (ASP). Active Server Pages lower the barrier to entry for Web developers by allowing HTML and server-side script to be freely mixed in ASP files. Scripts are typically written in JScript (Microsoft鈥檚 version of JavaScript) or VBScript, but they can be written in other languages as well. Intrinsic objects available to those scripts abstract the low-level details of HTTP and make it exceedingly easy to write code that generates HTML content dynamically. Just how easy is ASP? Compare the code in Figures 5-4 and 5-5 and judge for yourself.

When an Active Server Page is requested, ASP parses the page and executes any scripts contained inside it. Scripts access the input accompanying the request by using the ASP Request object, and they write HTML to the HTTP response using the ASP Response object. Figure 5-5 shows the ASP version of Calc.html. The VBScript between <% and %> tags checks the incoming request for inputs named op1 and op2. If the inputs aren鈥檛 present, an empty calculator is rendered back to the client. If the inputs are present鈥攖hat is, if Request (鈥渙p1鈥? and Request (鈥渙p2鈥? evaluate to non-null strings鈥攖he server-side script converts the inputs to integers, adds them together, converts the result to a string, and writes the string to the HTTP response using Response.Write.

To prevent the numbers typed into the text boxes from disappearing following a postback, Calc.asp uses ASP鈥檚 inline output syntax (<%= 鈥?%>) to initialize the Value attributes returned in the <input type=鈥渢ext鈥?gt; tags. When the page is first requested from the server, Request (鈥渙p1鈥? and Request (鈥渙p2鈥? return empty strings, so the tags output to the client produce empty text boxes: <input聽type="text" name="op1" value=""/>
<input聽type="text" name="op2" value=""/>

But when the form is rendered again following a postback, Request (鈥渙p1鈥? and Request (鈥渙p2鈥? return the values input by the user and are echoed to the client in the tags鈥?Value attributes:
<input聽type="text" name="op1" value="2"/>
<input聽type="text" name="op2" value="2"/>

To verify that this is the case, drop Calc.asp into \Inetpub\wwwroot and bring it up by typing http://localhost/calc.asp. Then enter a couple of numbers, click the = button, and use the View/Source command in Internet Explorer to view the HTML returned by ASP.

The appeal of ASP鈥攁nd the reason it caught on so quickly after its introduction in 1996鈥攊s that it provides an easy-to-use model for dynamically generating HTML on Web servers. ASP provides a higher level of abstraction than either CGI or ISAPI, which means a flatter learning curve and faster time to market. And ASP integrates seamlessly with ActiveX Data Objects (ADO), which makes it a great solution for writing Web apps that interact with back-end databases.

Calc.asp

<%@聽Language="VBScript" %>

<html>
聽聽<body>
聽聽聽聽<form>
聽聽聽聽聽聽<input聽type="text" name="op1" value="<%=聽Request聽("op1")聽%>"/>
聽聽聽聽聽聽+
聽聽聽聽聽聽<input聽type="text" name="op2" value="<%=聽Request聽("op2")聽%>" />
聽聽聽聽聽聽<input聽type="submit" value=" 聽=聽 " />
聽聽聽聽聽聽<%
聽聽聽聽聽聽聽聽If聽Request聽("op1")聽<> "" And聽Request聽("op2")聽<> "" Then
聽聽聽聽聽聽聽聽聽聽聽聽a聽=聽CInt聽(Request聽("op1"))
聽聽聽聽聽聽聽聽聽聽聽聽b聽=聽CInt聽(Request聽("op2"))
聽聽聽聽聽聽聽聽聽聽聽聽Response.Write聽(CStr聽(a聽+聽b))
聽聽聽聽聽聽聽聽End聽If
聽聽聽聽聽聽%>

聽聽聽聽</form>
聽聽</body>
</html>

Figure 5-5
ASP calculator applet.

Your First Web Form

ASP is a fine solution for performing server-side processing of HTML form input and dynamically generating HTML, but despite its youth, ASP has already grown long in the tooth. What鈥檚 wrong with ASP? For starters, it鈥檚 slow. ASP scripts are interpreted rather than compiled, so you incur the cost of recompiling your scripts on each and every page access. Another problem is that ASP lacks a true encapsulation model. It鈥檚 not possible, for example, to build reusable ASP controls that encapsulate complex rendering and behavioral logic without resorting to COM.

Enter ASP.NET Web forms. Web forms bring object-oriented programming to the Web. They also combine ASP鈥檚 ease of use with the speed of compiled code. Figure 5-6 holds the source code for the Web Forms version of Calc.asp. The .aspx file name extension identifies the file as an ASP.NET resource. Figure 5-7 shows how Calc.aspx appears in Internet Explorer. Here鈥檚 how to run it on your PC:

  1. Copy Calc.aspx to your PC鈥檚 \Inetpub\wwwroot directory.

  2. Start Internet Explorer or the browser of your choice and type http://localhost/calc.aspx in the browser鈥檚 address bar. The Web form will appear in the browser window.

  3. Type 2 and 2 into the input fields and click the = button. The number 4 should appear to the right of the button.

The \Inetpub\wwwroot directory is an IIS virtual directory; it鈥檚 created automatically when you install IIS. If you鈥檇 prefer not to clutter \Inetpub\wwwroot, you can set up virtual directories of your own using the Internet Services Manager applet found under Administrative Tools. You could, for example, put Calc.aspx in a directory named Samples and make Samples a virtual directory. If you assign the Samples directory the logical name 鈥淪amples鈥?(virtual directory names don鈥檛 have to equal physical directory names, although they often do), you鈥檇 run Calc by typing http://localhost/samples/calc.aspx in the browser鈥檚 address bar. The same goes for other ASPX files presented in this chapter and throughout the remainder of the book.

Calc.aspx

<html>
聽聽<body>
聽聽聽聽<form聽runat="server">
聽聽聽聽聽聽<asp:TextBox聽ID="op1" RunAt="server" />
聽聽聽聽聽聽+
聽聽聽聽聽聽<asp:TextBox聽ID="op2" RunAt="server" />
聽聽聽聽聽聽<asp:Button聽Text=" 聽=聽 " OnClick="OnAdd" RunAt="server" />
聽聽聽聽聽聽<asp:Label聽ID="Sum" RunAt="server" />
聽聽聽聽</form>
聽聽</body>
</html>

<script聽language="C#" runat="server">
聽聽void聽OnAdd聽(Object聽sender,聽EventArgs聽e)
聽聽{
聽聽聽聽聽聽int聽a聽=聽Convert.ToInt32聽(op1.Text);
聽聽聽聽聽聽int聽b聽=聽Convert.ToInt32聽(op2.Text);
聽聽聽聽聽聽Sum.Text聽=聽(a聽+聽b).ToString聽();
聽聽}
</script>

Figure 5-6
ASP.NET Web form calculator.
Figure 5-7
Calc.aspx in action.

Web forms are built from a combination of HTML and server controls. Calc.aspx contains four server controls: two TextBox controls, a Button control, and a Label control. TextBox, Button, and Label are classes defined in the System.Web.UI.WebControls namespace in the .NET Framework class library (FCL). Each time Calc.aspx is requested, ASP.NET instantiates TextBox, Button, and Label objects and asks each object to render itself into HTML. The HTML returned by the controls is included in the HTTP response. Execute a View/Source command while Calc.aspx is displayed in Internet Explorer and you鈥檒l see the following HTML: <html>
聽聽<body>
聽聽聽聽<form聽name="_ctl0" method="post" action="calc.aspx" id="_ctl0">
聽聽聽聽聽聽<input聽type="hidden" name="__VIEWSTATE" value="dDwxOTE0NDY4ODE2Ozs+" />

聽聽聽聽聽聽<input聽name="op1" type="text" id="op1" />
聽聽聽聽聽聽+
聽聽聽聽聽聽<input聽name="op2" type="text" id="op2" />
聽聽聽聽聽聽<input聽type="submit" name="_ctl1" value=" 聽=聽 " />
聽聽聽聽聽聽<span聽id="Sum"></span>
聽聽聽聽</form>
聽聽</body>
</html>

The TextBox controls turned into <input type=鈥渢ext鈥?gt; tags, the Button control turned into an <input type=鈥渟ubmit鈥?gt; tag, and the Label control turned into a <span> tag. In effect, these controls 鈥減roject鈥?a user interface to the browser by rendering themselves into HTML.

What about the <input> tag named __VIEWSTATE in the HTML returned by Calc.aspx? That鈥檚 the mechanism ASP.NET uses to round-trip data from the server to the client and back to the server again. You鈥檒l learn all about it in Chapter 8.

Control Properties

Server controls do more than render HTML. They also implement methods, properties, and events that make them highly programmable. For example, TextBox, Button, and Label controls each expose text through a read/write property named Text. If you wanted 鈥?鈥?to appear in the TextBox controls by default, you could modify the control tags as follows:
<asp:TextBox聽Text="2" ID="op1" RunAt="server" />
<asp:TextBox聽Text="2" ID="op2" RunAt="server" />

Any public property that a control implements and that can be represented as a name/value pair can be initialized by using the property name as an attribute in the tag that declares the control.

Properties can also be accessed from server-side scripts. In Calc.aspx, the server-side script is the code that appears between the <script> and </script> tags. The statements

int聽a聽=聽Convert.ToInt32聽(op1.Text);
int聽b聽=聽Convert.ToInt32聽(op2.Text);

extract user input from the TextBox controls by reading their Text properties, while the statement
Sum.Text聽=聽(a聽+聽b).ToString聽();

displays the sum of the inputs by writing to the Label control鈥檚 Text property. The names op1, op2, and Sum are the controls鈥?programmatic IDs. Control IDs are defined by including ID attributes in control tags. In Calc.aspx, the Label control serves as a placeholder for the Web form鈥檚 output. Because the default value of a Label control鈥檚 Text property is an empty string, nothing appears in the form where the Label control is positioned until the server-side script assigns a string to the control鈥檚 Text property.

Control Events

The ability to encapsulate complex rendering and behavioral logic in reusable control classes is one of the fundamental tenets of the Web Forms programming model. Another is the use of events and event handling. Most server controls fire events in response to user input. Button controls, for example, fire Click events when they鈥檙e clicked. Wiring an event to an event handler is accomplished by prefixing the event name with 鈥淥n鈥?and using the resulting text as an attribute in the tag that declares the control. In Calc.aspx, the statement

<asp:Button聽Text=" 聽=聽 " OnClick="OnAdd" RunAt="server" />

serves the dual purpose of declaring a Button control and designating OnAdd as the handler for the Button control鈥檚 Click events. That鈥檚 why the code in OnAdd executed when you clicked the = button. Knowing this, it鈥檚 a simple matter to consult the documentation for the list of events a control is capable of firing and connecting handlers to the events that interest you.

What happens under the hood to support the Web Forms event model is a little more complex. Look again at the HTML returned by Calc.aspx. Notice that it contains an HTML form and a submit button. Clicking the button posts the form back to the server using an HTTP POST. Recognizing that the POST command represents a postback that occurred because the user clicked the = button, ASP.NET notifies the Button object and the Button responds by firing a Click event on the server. ASP.NET subsequently calls OnAdd and then renders the page again into HTML. Because the Label control鈥檚 Text property now has a non-null string assigned to it, this time the HTML output by the Label control includes a text string between the <span> and </span> tags.

Implementation Notes

Calc.aspx contains no code to prevent the numbers typed into the TextBox controls from disappearing following a postback. The <asp:TextBox> tags in Figure 5-6 lack Value attributes such as the ones in Figure 5-5鈥檚 <input type= 鈥渢ext鈥?gt; tags. Yet the inputs don鈥檛 disappear when you click the = button. Why? Because TextBox controls automatically persist their contents across postbacks. Check the HTML returned to the browser following the postback and you鈥檒l find that <input type=鈥渢ext鈥?gt; tags rendered by the TextBox controls have Value attributes that equal the text typed by the user.

To make Calc.aspx as simple as possible, I purposely omitted error checking code. To see what I mean, type something other than a simple integer value (say, 鈥渉ello鈥? into one of the text boxes and click the = button. The page you see is ASP.NET鈥檚 way of responding to unhandled exceptions. To prevent this error, rewrite Calc.aspx鈥檚 OnAdd method as follows:

void聽OnAdd聽(Object聽sender,聽EventArgs聽e)
{
聽聽聽聽try聽{
聽聽聽聽聽聽聽聽int聽a聽=聽Convert.ToInt32聽(op1.Text);
聽聽聽聽聽聽聽聽int聽b聽=聽Convert.ToInt32聽(op2.Text);
聽聽聽聽聽聽聽聽Sum.Text聽=聽(a聽+聽b).ToString聽();
聽聽聽聽}
聽聽聽聽catch聽(FormatException)聽{
聽聽聽聽聽聽聽聽Sum.Text聽= "Error";
聽聽聽聽}
}

This version of OnAdd catches the exception thrown when Convert.ToInt32 is unable to convert the input to an integer and responds by displaying the word 鈥淓rror鈥?to the right of the push button.



]]>
99久久亚洲综合精品网站| 激情伊人五月天久久综合| 久久久精品久久久久特色影视| av无码久久久久久不卡网站| A狠狠久久蜜臀婷色中文网| 久久久久久狠狠丁香| 亚洲国产婷婷香蕉久久久久久 | 久久最新免费视频| 无码人妻久久一区二区三区蜜桃| 国产成人精品综合久久久| 情人伊人久久综合亚洲| 亚洲性久久久影院| 久久精品一区二区| 亚洲AV无一区二区三区久久| 成人久久精品一区二区三区 | 久久夜色撩人精品国产小说| 色综合久久久久久久久五月| 国产精品99久久99久久久| 国产精品成人无码久久久久久| 国产精品亚洲综合久久| 久久国产精品99精品国产987| 国产精品久久久久久久app | 日本三级久久网| 久久青青草原精品国产| 久久这里有精品| 久久中文精品无码中文字幕| 99久久精品这里只有精品| 国产精品无码久久久久久| 亚洲精品乱码久久久久久久久久久久 | 香蕉久久永久视频| 久久久精品国产亚洲成人满18免费网站 | 99久久无码一区人妻| 999久久久无码国产精品| 亚洲国产美女精品久久久久∴ | 国产日产久久高清欧美一区| 久久国产精品77777| 久久久久久午夜成人影院| 亚洲中文字幕无码一久久区| 亚洲欧美久久久久9999| 久久精品国产精品亚洲精品| 久久精品国产99久久久古代|