Responding to User Interaction

A good bit of the code responds to user selections, and this can be seen in Listing 23.6. The first method you'll see is the CreateNewFilename() method. This method creates a file name for the image that is going to be created next, and it has the system ticks as part of the file name. Here's why this is important: Suppose we use the same file name for a user's session, and the user makes changes in the certificate 10 times. The browser will not show these changes in the regenerated image because the original image will be cached by the browser. For this reason, each time the user makes a change in the certificate, we need to change the file name. That way, the browser won't cache the file, and the user will see the newly created file.

Following the CreateNewFilename() method are four similar methods: ul_SelectedIndexChanged(), ur_SelectedIndexChanged(), bl_SelectedIndexChanged(), and br_Selected IndexChanged(). These methods all change the selected corner image. Each one generates a new file name by calling CreateNewFilename(), and then calling OutputImage() to save the updated certificate.

The tile_SelectedIndexChanged() method responds to a user selection for the background tile. When a user changes his selection, this method is fired. It sets the session variable named Tile to the file name that the user selected (and adds a .gif extension). A call to CreateNewFilename() and a call to OutputImage() change the certificate that the user is viewing.

The next three methods, InsertNow_Click(), InsertLeft_Click(), and InsertRight_Click(), perform similar functions. Each takes a text string and places it into the appropriate Certificate class property. For each of these methods, the MainText, LeftText, and RightText session variables are set (depending on which method is being called). Then, a call to CreateNewFilename() and OutputImage() completes the process of changing the viewed certificate.

The last method in Listing 23.6 is named SendEmail_Click(), and this method e-mails a link to the saved certificate. The first thing that the method does is instantiate a Certificate object, call the CreateImage() method to populate all of the properties, call the SaveImage() method to save the image to disk, and then send the e-mail. Figure 23.11 shows the use of the e-mail functionality.

Figure 23.11. You Can Send the Certificates via E-Mail with the Email Certificate Button.

graphics/23fig11.jpg

Before you actually send the e-mail, though, the HTML must be formed so that the e-mail will have the correct link. Once this is done and contained in a string variable, an instance of the MailMessage class is created. The MailMessage Body, BodyFormat, From, and Subject properties are all set. Then, for each e-mail address that's in the Emails TextBox object, the SmtpServer and Send properties are set, followed by a call to the Send() method. These steps are all it takes to send the e-mail message to the recipient. The e-mail server that was specified is one through which this application has security clearance to send e-mails.

Listing 23.6 Responding to User Changes
 Sub CreateNewFilename()     Try         File.Delete(Request.MapPath("TempImages\") + _             Session("Filename"))     Catch     End Try     Session("Filename") = Session.SessionID + _         Convert.ToString(DateTime.Now.Ticks) + ".gif" End Sub Private Sub ul_SelectedIndexChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles ul.SelectedIndexChanged     CreateNewFilename()     OutputImage() End Sub Private Sub ur_SelectedIndexChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles ur.SelectedIndexChanged     CreateNewFilename()     OutputImage() End Sub Private Sub bl_SelectedIndexChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles bl.SelectedIndexChanged     CreateNewFilename()     OutputImage() End Sub Private Sub br_SelectedIndexChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles br.SelectedIndexChanged     CreateNewFilename()     OutputImage() End Sub Private Sub tile_SelectedIndexChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles tile.SelectedIndexChanged     Session("Tile") = tile.SelectedItem.Text + ".Gif"     CreateNewFilename()     OutputImage() End Sub Private Sub InsertNow_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles InsertNow.Click     Session("MainText") = MainText.Text     MainText.Text = ""     CreateNewFilename()     OutputImage() End Sub Private Sub InsertLeft_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles InsertLeft.Click     Session("LeftText") = LeftText.Text     LeftText.Text = ""     CreateNewFilename()     OutputImage() End Sub Private Sub InsertRight_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles InsertRight.Click     Session("RightText") = RightText.Text     RightText.Text = ""     CreateNewFilename()     OutputImage() End Sub Private Sub SendEmail_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles SendEmail.Click     Dim cert As New Certificate()     CreateImage(cert)     cert.SaveImage("EmailImages")     Status.Text = "The Email(s) have been sent!"     Dim strMessage As String = _         "<html><body bgcolor=cyan><p><h2>You have " + _         "been sent a certificate.</h2></p><p>" + _         "<a href=http://www.aspnet-solutions.com/" + _         "CertificatesVB/EmailImages/" + Session("Filename") + _         ">Click Here To See It</a></p></body><html>"     Dim message As New MailMessage()     message.Body = strMessage     message.BodyFormat = MailFormat.Html     message.From = Chr(34) + "ASPNET Solutions Certificate" + _         Chr(34) + "<Certificates@ASPNET-Solutions.com>"     message.Subject = "A Certificate To You"     Dim strEmailAddrs() As String = _         Emails.Text.Split(New Char() {Chr(56)})     Emails.Text = ""     Dim i As Integer     For i = 0 To strEmailAddrs.Length - 1         SmtpMail.SmtpServer = "mail.aspnet-solutions.com"         message.To = strEmailAddrs(i)         SmtpMail.Send(message)     Next End Sub 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net