⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet13-07.htm

📁 tutorialssss.... for everybody!!!!!!!!!!!!!!!!!!!
💻 HTM
📖 第 1 页 / 共 2 页
字号:
</pre>
<div class="listing"><b>Listing 13-39.</b> Code for <span class="code">Page_Load</span> subprogram to capture received form 
information.</div>
<p><span class="code">Request.Form</span> values are available to the page only 
the <i>first</i> time the page loads. If the page is posted back to the server, 
as is done when confirming and submitting order information, the <span class="code">Request.Form</span> collection is lost. It does not take part in the 
page's View State. Therefore, the four received fields are saved to Label 
controls when the page loads. These Label values are display on the form except 
for the passed <span class="code">ReturnURL</span> value. It is not intended to be 
displayed, but is needed later to redirect back to the eCommerce site. Its Label 
control is made invisible.</p>
<p>Normally, TextBox controls on the form would be blank for customers to enter 
their personal information. These controls have been prefilled with example 
data. You will need, however, to <i>enter your real email address if you wish to 
receive an email confirmation for your example order</i>. No other use is made 
of this entered address.</p>
<p class="head2">Checking Credit</p>
<p>After the form is filled out by the customer, it is submitted for approval by 
clicking the "Continue Purchase" button. This button calls the <span class="code">Check_Credit</span> subprogram. In this example, the customer's 
credit isn't actually checked. The order is simply approved and confirmation is 
sent back to the eCommerce site. Changes that the subprogram causes in the page 
display are shown in the following illustration.</p><img src="aspnet13-07_data/CreditCheck2.gif"> 
<div class="figure"><b>Figure 13-12.</b> Layout of <span class="code">CreditCheck.aspx</span> page after order confirmation.</div>
<p>The <span class="code">Credit_Check</span> subprogram that processes the form 
and produces this output is shown below. A modest check is made for a valid 
email address. Then all form controls are disabled and a different set of 
buttons are provided to finalize the order. The <span class="code">Approved 
</span>Label is set to <span class="code">"Approved."</span></p><pre class="divscript">Sub Check_Credit (Src As Object, Args As EventArgs)

  Dim ValidEmail As Boolean = True
  If Email.Text &lt;&gt; "" Then
    If InStr(Email.Text, "@") = 0 Then
      ValidEmail = False
      Message.Text = "Invalid email address"
    ElseIf Mid(Email.Text, Len(Email.Text) - 3, 1) &lt;&gt; "." Then
      ValidEmail = False
      Message.Text = "Invalid email address"
    End If
  Else
    ValidEmail = False
    Message.Text = "Missing email address"
  End If
  
  If ValidEmail = True Then
    CreditCard.Enabled = False
    Account.Enabled = False
    ExpMonth.Enabled = False
    ExpYear.Enabled = False
    Name.Enabled = False
    Address.Enabled = False
    City.Enabled = False
    State.Enabled = False
    Zip.Enabled = False
    Email.Enabled = False

    Instructions.Text = "Click the ""Complete Order"" button to " &amp; _
                        "complete your order with "
    Approved.Text = "Approved"
    ContinuePanel.Visible = False
    CompletePanel.Visible = True
  End If

End Sub
</pre>
<div class="listing"><b>Listing 13-40.</b> Code for <span class="code">Check_Credit</span> subprogram to validate email address and disable 
text controls.</div>
<p class="head2">Returning Credit Approval Information</p>
<p>Clicking the "Complete Order" button returns order approval and customer 
information back to the merchant site鈥攖o the return URL address provided by the 
received form. In this example, return is to the <span class="code">OrderCapture.aspx</span> page.</p>
<p>As is typical, the credit card processing service returns information as a 
form submission to the return URL. In this case, though, it is not necessary to 
use a separate form page to collect and submit form information as was done 
previously when submitting a form to this page. Since the <span class="code">CreditCheck.aspx</span> page is <i>not</i> embedded inside a master 
page, a standard HTML form <i>can</i> appear on this page and submit directly 
back to the return URL.</p>
<p>It is not entirely true that an ASP.NET page cannot contain more than one 
form. Certainly, it can contain only a single <span class="code">&lt;form 
Runat="Server"&gt;</span> form. However, it can contain a second form if (1) the 
form is a standard HTML form, and (2) this HTML form appears <i>outside</i> the 
server form.</p>
<p>The following listing shows the code added to the <span class="code">CreditCheck.aspx</span> page in order to create and submit an HTML 
form for return of order confirmation and customer information to the merchant 
site.</p><pre class="divcode"><span class="script">Dim SubmitForm As Boolean = False

Sub Submit_Order (Src As Object, Args As EventArgs)
  SubmitForm = True
End Sub

Sub Cancel_Order (Src As Object, Args As EventArgs)
  Approved.Text = "Cancel"
  Submit_Order(Nothing, Nothing)
End Sub
</span>
...
&lt;body&gt;
&lt;form Runat="Server"&gt;

  ...

&lt;/form&gt;

&lt;form name="ReturnOrder" action='<span class="script">&lt;%= ReturnURL.Text %&gt;</span>' method="post"&gt;

&lt;input type="hidden" name="ReturnApproved" value='<span class="script">&lt;%= Approved.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnMerchantID" value='<span class="script">&lt;%= MerchantID.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnCustomerID" value='<span class="script">&lt;%= CustomerID.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnOrderTotal" value='<span class="script">&lt;%= OrderTotal.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnName" value='<span class="script">&lt;%= Name.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnAddress" value='<span class="script">&lt;%= Address.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnCity" value='<span class="script">&lt;%= City.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnState" value='<span class="script">&lt;%= State.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnZip" value='<span class="script">&lt;%= Zip.Text %&gt;</span>'/&gt;
&lt;input type="hidden" name="ReturnEmail" value='<span class="script">&lt;%= Email.Text %&gt;</span>'/&gt;

&lt;/form&gt;

<span class="script">&lt;% If SubmitForm = True Then %&gt;</span>
&lt;script type="text/javascript"&gt;
  ReturnOrder.submit()
&lt;/script&gt;
<span class="script">&lt;% End If %&gt;</span>

&lt;/body&gt;
&lt;/html&gt;
</pre>
<div class="listing"><b>Listing 13-41.</b> Code to add an HTML form and processing 
routines to the <span class="code">CreditCheck.aspx</span> page.</div>
<p>Pay particular attention to placement of the HTML form. It physically follows 
the server form, appearing after its closing <span class="code">&lt;/form&gt;</span> tag, and it appears before the closing <span class="code">&lt;/body&gt;</span> and <span class="code">&lt;/html&gt;</span> tags 
for the page.</p>
<p>All form <span class="code">&lt;input&gt;</span> tags that hold values to be 
returned to the merchant site are defined as "hidden" fields (textboxes). There 
is no reason these fields need to be visible on the page. Form submission takes 
places behind the scenes without user involvement; there is no "submit" button. 
Also, embedded scripts supply the values for these fields. These values, along 
with the return URL for the form's <span class="code">action</span> attribute, 
come from corresponding server controls on the page. When the page loads, these 
values automatically appear in the form ready for submission.</p>
<p>HTML form submission cannot be triggered by a server script. It must come 
either through a click on a "submit" button (missing in this case) or through a 
browser script written in JavaScript. Recall in the previous form submission 
that it came through a script that was run when the page loaded: <span class="code">&lt;body onLoad="SubmitForm.submit()"&gt;</span>. Here, though, this 
technique cannot be used; i.e., the form cannot be submitted immediately when 
the <span class="code">CreditCheck.aspx</span> page loads. It is submitted 
<i>after</i> the customer enters personal information onto the page and clicks 
the "Complete Order" button to finalize the purchase.</p>
<p>The trigger for submitting the form, then, is the "Complete Order" button 
click. This event calls subprogram <span class="code">"Submit_Order"</span>. As 
shown in the above listing, this subprogram includes a single statement, <span class="code">SubmitForm = True</span>, which sets the value of a previously 
declared global variable. Setting this variable sets the condition for 
submitting the form.</p>
<p>Notice that the HTML form is accompanied by a JavaScript that is enclosed 
inside an in-line server script. This code is repeated below.</p><pre class="divcode"><span class="script">&lt;% If SubmitForm = True Then %&gt;</span>
&lt;script type="text/javascript"&gt;
  ReturnOrder.submit()
&lt;/script&gt;
<span class="script">&lt;% End If %&gt;</span>
</pre>
<div class="listing"><b>Listing 13-42.</b> A JavaScript enclosed inside an in-line 
server script to submit an HTML form.</div>
<p>The JavaScript contains a single statement to submit the HTML form named 
<span class="code">ReturnOrder</span>. This script runs only if server variable 
<span class="code">SubmitForm = True</span>, which happens when the "Complete 
Order" button is clicked and the <span class="code">Submit_Form</span> subprogram 
is run. Thus, through a combination of server and browser scripts the HTML form 
is submitted to the return URL, in this case to the <span class="code">OrderCapture.aspx</span> page.</p>
<p class="head2">Cancelling an Order</p>
<p>At any time during the credit approval process the customer can click the 
"Return to Shopping" button to exit this process. This button calls the <span class="code">Cancel_Order</span> subprogram (repeated below). Here, the <span class="code">Approved</span> Label is given the value <span class="code">"Cancel"</span> prior to calling the <span class="code">Submit_Order</span> subprogram and submitting the form.</p><pre class="divscript">Sub Cancel_Order (Src As Object, Args As EventArgs)
  Approved.Text = "Cancel"
  Submit_Order(Nothing, Nothing)
End Sub
</pre>
<div class="listing"><b>Listing 13-43.</b> Code to cancel order processing.</div>
<p>The <span class="code">Submit_Order</span> subprogram is normally called on a 
click of the "Complete Order" button. Therefore, it has the normal signature and 
arguments of a subprogram called in this fashion. When calling this subprogram 
from the <span class="code">Cancel_Order</span> subprogram, however, there are no 
button arguments involved. Still, the subprogram expects passed arguments. Here, 
null arguments (<span class="code">Nothing, Nothing</span>) are passed to satisfy 
signature expectations. 
</p><p>At this point, with HTML form submission, control returns to the local <span class="code">OrderCapture.aspx</span> page in order to receive and process order 
confirmation and customer information returned from the credit card processing 
service through its submitted form.</p><br>
<div><input id="__EVENTVALIDATION" value="/wEWAgKl8O7PCwLbhM+CAvmKuzBQhwVOQfkdpjckKh/5WJjj" name="__EVENTVALIDATION" type="hidden"> 
</div></form></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -