Tài liệu đào tạo ASP.NET
Mục lục
I. Nền tảng lập trình web 1
Web Programming 2
II. ASP.NET 3
III. Using JavaScript Along with ASP.NET 15
III.1 Adding JavaScript to a Server Control 15
III.2 Performing a Simple Button-rollover 17
III.3 Setting Control Focus 18
III.4 Changing the Control Focus Dynamically 18
III.5 Using Larger JavaScript Functions 18
III.5.1 RegisterStartupScript Method 19
III.5.2 RegisterClientScriptBlock Method 20
III.5.3 The Difference Between RegisterStartupScript and RegisterClientScriptBlock 22
III.6 Keeping JavaScript in a Separate File (.js) 22
IV. Data Access: 23
SqlDataReader 23
SqlParameter 24
Close Connection 24
When good connections go bad 24
IIS 6 on Windows 2003 Server 25
V. Microsoft Data Access Application Block 27
V.1 Introduction: 27
V.2 Using Microsoft .net Data Access Application Block: 27
V.3 Accessing data without Data Access Application Block: 27
V.4 Retrieving Multiple Rows using SqlDataReader and Data Access Application Block: 28
V.5 Retrieving Multiple Rows using DataSet: 28
V.6 Retrieving a Single Row 28
V.6.1 Stored Proc: 29
V.7 Explanation of the code: 29
V.8 Retrieving XML Data: 30
V.9 Explanation of the Code: 30
VI. Directory and File Access 30
turned to the shopping site. At the same time, the credit processing service normally returns confirmation of the approval (or rejection) of the order, along with billing and shipping information it might have collected from the customer. This information, then, can be used to create a permanent order record for that customer for future reference and to recreate the original order if necessary. The returned information can also be used to update the company's accounting and inventory systems. Although in this example you can't tie into the Federal banking system or link into an accounting or inventory system, the interchange with a credit processing company can be closely simulated by the CreditCheck.aspx page. Then, information returned from the CreditCheck.aspx page goes to the OrderCapture.aspx page, where an email confirmation is sent to the customer. The need exists, then, to transmit order information from the ShopCart.aspx page to the CreditCheck.aspx page, and then to transmit confirmation information from the CreditCheck.aspx page back to the OrderCapture.aspx page. You may recall from previous discussions that there are various ways of passing information from one page to another. Query strings, for example, have been used to transmit information between pages of this site. In this case, however, forms are used. One of the reasons for doing so is because forms are the standard method of transmitting order information to and from credit processing services. A second reason is that query strings risk revealing private information in the browser's address box. They are appended, in full view, in the browser window. So, using forms is the common way that private information is transmitted. HTML Forms HTML forms differ from "Web forms," or server forms, in that they can be sent to different pages from where they reside. They represent the historically standard way of submitting form information for script processing. HTML forms, contained also within tags, require use of two attributes to transmit their contents. ... The action attribute supplies the URL of the page to which form information is to be sent. This can be a virtual URL to a local page or a full URL to an external site. The method attribute indicates the manner in which form information is transmitted. Usually the POST method is used to transmit form contents in a separate data stream accompanying the URL request. The alternative method is GET (the default), which appends the contents to the URL as a query string, something to be avoided in this case. HTML forms include standard HTML "field" tags as containers for data. Normally the container is an tag with attributes shown in the following general format: An tag is the HTML form equivalent of an asp:TextBox control. It holds a text string value. Other data tags are available, but the tag is most widely used for transmitting information between Web sites and, in the current example, between pages. As many tags are coded as are needed to transmit the separate data items. When setting up an HTML form for passing information between Web sites, the tag normally uses the type="hidden" rather than type="text" attribute. It is common not to display the form fields since their purpose is not to show their values, but only to hold their values for submission. Accompanying the form fields is a special button for triggering form submission, as shown below in its general format. This tag has a type="submit" attribute that displays the tag as a button and causes the form to be submitted when it is clicked. A click on the button redirects to the URL in the form's action attribute, passing along the form's data in an accompanying data stream as directed by the form's method attribute. Accessing HTML Form Information The page receiving the submitted form can access the transmitted information in a way very similar to accessing query strings. The received information arrives as a set of name/value pairs. The "names" are the name attributes that are assigned to the tags; the "values" are the data values associated with their value attributes. For example, if the following form is created, then the transmitted data stream is composed of the names and values, Field1=first value&Field2=second value&Field3=third value At the receiving page these names and values are accessible through the Request.Form collection, in the same way that query strings are accessible through the Request.QueryString collection. Thus, the data values received through the previous form are accessible in a script as Request.Form("Field1")Request.Form("Field2")Request.Form("Field3") The receiving page captures the form information sent to it and uses the submitted values for whatever processing purposes it needs. Credit and Checkout Processing Receiving Form Information Notice the four asp:TextBox controls appearing at the beginning of the HTML coding: Sub Page_Load If Not Page.IsPostBack Then SavedReturnURL.Text = Request.Form("ReturnURL") SavedMerchantID.Text = Request.Form("MerchantID") SavedCustomerID.Text = Request.Form("CustomerID") SavedOrderAmount.Text = Request.Form("OrderAmount") FormCustomerID.Text = Request.Form("CustomerID") FormOrderAmount.Text = Request.Form("OrderAmount") End If End Sub Sub CheckOut( Src As Object, Args As EventArgs) ... End Sub Sub Cancel (Src As Object, Args As EventArgs) Response.Redirect("ShopCart.aspx") End Sub Checking Credit Returning Credit Approval Information Email Confirmations Sending Email Email is sent from a Web server through its SMTP (Simple Mail Transfer Protocol) service. This is, as the name implies, a limited email service; however, it is sufficient for generating automated emails. The SMTP service is included as a component of Microsoft Windows 2000 Server and XP. It may have to be installed and initiated if it were not part of a default installation. If your are running ASP.NET under XP Professional on your desktop computer (localhost), you may need to configure it to permit relay of email messages. Perform the following steps: Open IIS Administrative Tools. Stop Default SMTP Virtual Server service. Open properties window of Default SMTP Virtual Server. Click "Access" tab and click "Relay..." button. Click "Only the list below" button and add the single computer at IP address 127.0.0.1. Click "OK" buttons to close "Access" tabs and properties window. Restart Default SMTP Virtual Server service. The ASP.NET MailMessage class is used to compose and send emails. A new MailMessage object is created: Dim MyEmail As MailMessageMyEmail = New MailMessage Then properties of the object are set to supply common parts of the email message: MailObject.To Sets the recipient's email address. Multiple addresses are separated with semicolons. MailObject.From Sets the sender's email address MailObject.Cc Sets the "carbon copy" recipient's email address. Multiple addresses are separated with semicolons. MailObject.Bcc Sets the "blind carbon copy" recipient's email address. Multiple addresses are separated with semicolons. MailObject.Subject Sets the subject line of the email message. MailObject.Priority Sets the priority of the email message. Values are Priority.High, Priority.Normal, or Priority.Low. MailObject.BodyFormat Sets the format of the body of the message. Values are MailFormat.Text or MailFormat.HTML. MailObject.Body Sets the contents of the message. After the message is composed by setting these properties, the Send() method of the SmtpMail class delivers the email: SmtpMail.Send(MailObject) It can be very simple to automate an email, as shown in this example script: Dim MyEmail = New MailMessage MyEmail.To = "Your@address.com" MyEmail.From = "My@address.com" MyEmail.Subject = "My test email" MyEmail.Body = "This is my text message." SmtpMail.Send(MyEmail) It's a little more work, though, to compose the order confirmation message sent from the OrderCapture.aspx page. Composing an Order Confirmation Email The following script lines begin the process by created the Email object and setting its To, From, and Subject properties. Notice that the values supplied for the To and Subject properties are the EmailAddress and CustomerID variables that were captured from the returned form. This and the following code is a continuation of the Page_Load subroutine. '-- Send email confirmation of order Email = New MailMessage() Email.To = EmailAddress Email.From = "dadams@mail.maconstate.edu" Email.Subject = "Order " & CustomerID & " Confirmation" ... The major effort is in composing the body of the message. This is done by creating an EmailBody variable and concatenating lines of text to it. The body of the message will be formatted as HTML, rather than text, so HTML code needs to part of the message body. The following script lines prepare the beginning lines of the message, including the table header for displaying the shopping cart items. ... EmailBody = "" EmailBody &= "" EmailBody &= "" EmailBody &= "" EmailBody &= "softWarehouse.com" EmailBody &= "" EmailBody &= "" EmailBody &= "Order Confirmation" Emailbody &= "" EmailBody &= "Date: " & Today & "" EmailBody &= "Order No.: " & CustomerID & "" EmailBody &= "" EmailBody &= Name & "" EmailBody &= Address & "" EmailBody &= City & ", " & State & " " & Zip & "" EmailBody &= "" EmailBody &= "Thank you for your order. We appreciate your shopping at " EmailBody &= "softWarehouse.com. If you have any questions about your " EmailBody &= "order, please email us at " EmailBody &= " " EmailBody &= "orders@softWarehouse.com " EmailBody &= "and reference the order number listed above." EmailBody &= "" EmailBody &= "" EmailBody &= "" EmailBody &= " Number" EmailBody &= " Title" EmailBody &= " Quantity" EmailBody &= " Price" EmailBody &= " Amount" EmailBody &= "" ... Sales Order Display Clearing the Shopping Cart
File đính kèm:
- Tài liệu đào tạo ASP.NET.doc