Check the IsPostBack property to determine whether the response request is a form that is sent back to the server. If it is, create a new instance of the Guestresponse data model object and pass this instance to the TryUpdateModel method, which inherits from the basic Page class, TryUpdateModel is also a member method in the parent class of the default class, the Page class. )
The TryUpdateModel method executes a procedure called model binding, in which the properties of the data model object are populated with the data values from the browser request. Another parameter of the TryUpdateModel method is the object--system.web.modelbinding.formvalueprovider class that ASP. NET uses to get the desired value, which provides values from the form's data. The third section describes model bindings in detail, and the result of calling the TryUpdateModel method is to update the properties of the Guestresponse object to reflect the data values that the user submits in the form, and then store the Guestresponse object in the repository.
We want to provide some kind of feedback to the user when the user submits the form, and the Response.Redirect method can do this by redirecting the user's browser. If the Willattend property is true, indicating that the user will attend the party, the user is redirected to the seeyouthere.html file, or the user is redirected to the sorryyoucantcome.html file.
1. Create an HTML response file
Not all pages in an ASP. NET application must be generated from a Web Forms file, and can include regular static HTML files. To create a response file, right-click the partyinvites item in the Solution Explorer and select Add→new item from the pop-up menu. From the Add New Item dialog box, select the HTML page template and set the name to Seeyouthere.html. Finally, click the Add button to create the HTML file. The contents of the file are shown in Listing 1-11.
Code Listing 1-11 contents of the seeyouthere.html file
Repeat the procedure above to create the sorryyoucantcome.html file, as shown in listing 1-12 of the following code.
Code Listing 1-12 contents of the sorryyoucantcome.html file
2. Incorporating HTML elements into a scope
We have built the basic structure of the application, but it seems to be almost anything. We need to tell visual Studio which file should be loaded when the application starts. Before this, this is not a problem, because only default.aspx this one file, Visual Studio is smart enough to know that this is the required file. But now there are several other HTML files, so you need to send instructions to Visual Studio. Right-click the Default.aspx item in the Solution Explorer and select Set as Start Page from the pop-up menu (set as Start Page).
You can now choose Start Debugging (Start Debugging) from the Debug menu, or click the Internet Explorer toolbar button to launch the application. Fill out the form to ensure that the Yes option is selected in the SELECT element. When you commit, you see a response that is only visible when you select the no option, as shown in 1-10. Obviously, something went wrong.
This problem occurs because of ASP when the Web Forms file is being processed. NET only finds elements that have a runat attribute value of server. All other elements are ignored because the INPUT element and the select element in the Default.aspx file do not have this attribute/value combination, and the model binding process cannot find the value submitted in the HTML form. In code listing 1-13, we have fixed this problem.
Listing 1-13 Adding the runat attribute to the INPUT element and the SELECT element
Tip Except for server, the runat attribute has no other value. If you omit the runat attribute, or use a value other than server, ASP. NET will not find the HTML element. If the Web form does not exhibit the expected behavior, first you should check if the runat attribute is missing.
Start the application and re-fill the form. After you submit the form, you will see the correct response, as shown in 1-11.
"ASP 11" first ASP. NET application-processing form (2)