Datasheet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If IsPostBack Then
Dim currentContact As New
Contact(CLng(Request.QueryString(“ContactId”)))
currentContact.FirstName = firstNameTextBox.Text
currentContact.LastName = lastNameTextBox.Text
currentContact.AddressLine1 = addressLine1TextBox.Text
currentContact.City = cityTextBox.Text
currentContact.PostalCode = postalCodeTextBox.Text
currentContact.State = stateTextBox.Text
currentContact.Telephone = telephoneTextBox.Text
currentContact.MobilePhone = mobilePhoneTextBox.Text
currentContact.Email = emailTextBox.Text
currentContact.SaveContact()
currentContact = Nothing
Response.Redirect(“YourContacts.aspx”)
Else
Dim currentContact As New
Contact(CLng(Request.QueryString(“ContactId”)))
firstNameTextBox.Text = currentContact.FirstName
lastNameTextBox.Text = currentContact.LastName
addressLine1TextBox.Text = currentContact.AddressLine1
cityTextBox.Text = currentContact.City
postalCodeTextBox.Text = currentContact.PostalCode
stateTextBox.Text = currentContact.State
telephoneTextBox.Text = currentContact.Telephone
mobilePhoneTextBox.Text = currentContact.MobilePhone
emailTextBox.Text = currentContact.Email
currentContact = Nothing
End If
End Sub
The If statement determines whether this is a postback (the form has been submitted to itself) or
whether the page has just been loaded. If it’s a postback, you need to save the data and then move back
to the main contacts section. If it’s a new page load, it’s necessary to create a new
Contact object, and
use the data from that to populate the form fields with the contact information.
The AddContact.aspx page is identical except there’s no need to populate with existing contact data,
because a new contact has no prior data!
Setting up the Online Diary
One of the great things about ASP.NET 2.0 is how easy it is to set up web applications created on one
machine onto another. To install the application on your PC, simply copy the entire directory and files
from the accompanying CD-ROM (or download it from
www.wrox.com) onto a directory on your PC
(for example,
C:\Websites). In VWD, all you have to do is choose File➪Open Web Site and browse to
the folder where you copied the files. Then press F5 to run it.
34
Chapter 1
04_749516 ch01.qxp 2/10/06 9:11 PM Page 34