Programming Microsoft ASP.NET 4 - Dino Esposito [133]
You build a page with a list of check boxes and a button to let the user post back to the server when finished. Notice, in fact, that neither the HtmlInputCheckBox control, nor any other input control except buttons, post back to the server when clicked. For this reason, you must provide another control on the Web page that supports posting to the server—for example, an HtmlButton or HtmlInputButton control. The following code implements the page shown in Figure 6-3:
<%@ Page Language="C#" %>
Figure 6-3. The ServerChange event fires only if the status of the control has changed since the last time the control was processed on the server.
The ServerChange event is fired only if the state of the control results changed after two postbacks. To get the first screen shot, you select the element and then submit. Next, if you submit again without selecting or deselecting anything, you get the second screen shot.
As mentioned in Chapter 5, when you implement the IPostBackDataHandler interface, each server control gets a chance to update its current state with data posted by the client.
Uploading Files
The HtmlInputFile control is the HTML tool for uploading files from a browser to the Web server. To take advantage of the HtmlInputFile control, you should first ensure that the server form’s Enctype property is set to multipart/form-data. However, starting with ASP.NET 2.0, the proper EncType is automatically set, care of the HtmlInputFile control, before the control’s markup is rendered. The enctype attribute in the code shown next is therefore unnecessary:
The way in which the HtmlInputFile control is rendered to HTML is browser-specific, but it normally consists of a text box and a Browse button. The user selects a file from the local machine and then clicks the button to submit the page to the server. When this occurs, the browser uploads the selected file to the server, as shown in Figure 6-4.
Figure 6-4. A new file has been uploaded to the Web server and copied to the destination folder.
Note
Prior to ASP.NET, a server-side process—the posting acceptor—was required to run in the background to handle multipart/form-data submissions. In ASP.NET, the role of the posting acceptor is no longer necessary because it is carried out by the ASP.NET runtime itself.
On the server, the file is parked into an object of type HttpPostedFile and stays there until explicitly processed—for example, saved to disk or to a database. The HttpPostedFile object provides properties and methods to get information on an individual file and to read and save the file. The following code shows how to save a posted file to a particular folder to disk:
<%@ Page language="C#" %>
<%@ Import Namespace="System.IO" %>
-->