Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning ASP.NET 2

.0.pdf
Скачиваний:
23
Добавлен:
17.08.2013
Размер:
24.67 Mб
Скачать

Exercise Answers

If Context.User.IsInRole(“FanClubMember”) Then

SubTotalLabel.Text = String.Format(“Sub-Total:{0,35:C}”,

Profile.Cart.SubTotal)

MemberDiscount.Text = String.Format(“Member Discount:{0:C}”,

Profile.Cart.MemberDiscount)

DiscountPanel.Visible = True

End If

Chapter 14

Exercise 1

Convert the two shop pages, Shop.aspx and ShopItem.aspx, from using SQL statements to stored procedures.

Solution

For Shop.aspx you need to create the following stored procedure:

CREATE PROCEDCURE dbo.usp_Shop

AS

SELECT ProductID, Name, Description, Price, PictureURL

FROM Products

ORDER BY Name

Next, change the SelectCommand from the SQL to usp_Shop.

For ShopItem.aspx the stored procedure should be

CREATE PROCEDURE dbo.usp_ShopItem @ProductID int

AS

SELECT *

FROM Products

WHERE ProductID = @ProductID

Next, change the SelectCommand from the SQL to usp_ShopItem.

Exercise 2

Add caching to the ShopItem.aspx page, so that the page is cached. Note that you need to take into account that this page shows different products, so the cache needs to be varied by the product being shown.

Solution

For this you simply add the OutputCache directive to the page, but because the product being shown is defined in the querystring, you need to vary by the following querystring:

<%@ OutputCache Duration=”3600” VaryByParam=”ProductID” %>

649

Appendix A

Chapter 15

Exercise 1

Add defensive coding to the GenerateThumbnail method of the ImageHandling class stored in the App_Code directory.

Solution

Having learned what you have about defensive coding and exceptions, you might be tempted to change the code for GenerateThumbnail to that shown in the chapter, where both defensive coding and exception handling were used. However, during the chapter you used the Application_Error event to centrally log exceptions, so the explicit logging within GenerateThumbnail isn’t required. But you still need to code against a missing source file, so the code could be changed to the following:

Public Shared Sub GenerateThumbnail(ByVal SourceImagePath As String, _ ByVal TargetImagePath As String)

If Not File.Exists(sourceImagePath) Then

Return

End If

Dim newHeight As Short

Dim newWidth As Short

Using sourceImage As Image = Image.FromFile(SourceImagePath)

newHeight = CShort(sourceImage.Height * 0.25) newWidth = CShort(sourceImage.Width * 0.25)

Dim cb As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Using targetImage As Image = _

sourceImage.GetThumbnailImage(newWidth, newHeight, cb, IntPtr.Zero) targetImage.Save(TargetImagePath, Imaging.ImageFormat.Gif)

End Using End Using

End Sub

This does assume that a failure generating the thumbnail isn’t critical, which may not be a correct assumption. Thumbnails are generated when pictures are uploaded to the site, pictures such as products for the shop, match photos, and so on. The site can run quite happily when these images are missing, so it isn’t critical, and any exception would be logged by the central code so the site administrators would know about it.

Exercise 2

Add validation controls to the Checkout page, the part that accepts the delivery address. There is a checkbox to copy the address from the membership details of the user, but there is nothing to ensure that all of the fields are filled in.

650

Exercise Answers

Solution

To ensure data is entered into the address boxes, RequiredFieldValidator controls can be used. You can add a validator for each TextBox, adding it next to the field it validates:

<tr><td><asp:TextBox id=”txtName” runat=”server” /> <asp:RequiredFieldValidator id=”rfv1” runat=”server”

ControlToValidate=”txtName” Text=”*”

ErrorMessage=”You must enter a value for the delivery name” />

</td></tr>

You will also need a ValidationSummary, which can be put after the table containing the address fields:

</table>

<asp:ValidationSummary id=”vs” runat=”server” DisplayMode=”BulletList” /> </asp:WizardStep>

Exercise 3

Use the debugger. No really, use it.

Solution

This exercise is more important than you realize, because learning how to debug is critical, especially as a beginner. When learning, you tend to make more mistakes, or perhaps code less efficiently than you do with years of experience. That’s only natural, and learning how to quickly track down bugs will make you a better developer, not only because you’ll be good at finding bugs, but also because in finding those bugs you learn what the problem was and how to avoid it next time. So in finding bugs you become a better coder.

Chapter 16

Exercise 1

You are now developer and maintainer of the Wrox United web site. What else would you add to the Wrox United site? Make a list of possible add-ons, and prioritize them with the simplest to do first. Implement some of these add-ons.

Solution

There is no hard and fast solution to this exercise, but to get some ideas for add-ons to your site, or to compare notes with other developers and the authors of this book, head over to http://p2p.wrox.com and look for this book’s discussion board. Note that this forum is purely for this topic and not for customer feedback or book problems.

651

B

Setup

The ASP.NET 2.0 runtime comes as part of the Visual Web Developer Express download. ASP has undergone an evolution in terms of setup, each version being simpler (although often larger and lengthier) to install than the previous one. In classic ASP 2.0, ASP was separate even from the IIS web server itself, both of which had to be downloaded and attached manually to IIS. By version 3.0 of classic ASP, ASP had been integrated with the web server. With ASP.NET 1.x you had to download the .NET Framework separately to get ASP, and enable IIS, which by then was part of the operating system. However, throughout each of these iterations you still had to download and install a database solution separately.

With ASP.NET 2.0, all four parts come together in one easy-to-install package — Visual Web Developer Express 2005. ASP.NET 2.0, web server, development tools, and database are all installed at the same time. The web server is a test-development server nicknamed Cassini (after the discoverer of the gaps and particular matter in the rings of Saturn) and can run without IIS, alongside IIS, or you can choose to use IIS in place of Cassini instead.

System Requirements

Before you install VWD Express, you should check that your system meets the minimum requirements. We have taken the requirements from the Microsoft web site. They are as follows:

Processor

Minimum: 600 megahertz (MHz) Pentium processor.

Recommended: 1 gigahertz (GHz) Pentium processor.

Appendix B

Operating System

VWD Express can be installed on any of the following systems:

Microsoft Windows 2003 Server.

Windows XP (Home Edition and Professional), Service Pack 2.

Windows 2000 (Professional and Server), Service Pack 4.

RAM

Minimum: 128 megabytes (MB).

Recommended: 256 MB.

Hard Disk

Up to 1.3 GB of available space may be required.

CD or DVD Drive

Not required.

Display

Minimum: 800×600 256 colors.

Recommended: 1024×768 High Color — 16-bit.

Mouse

Microsoft mouse or compatible pointing device.

As with plenty of Microsoft technologies, you’ll find that it may well work on other older setups; however, we cannot recommend this and we won’t answer questions about it either.

Visual Web Developer Express Installation

1.Go to the following URL: http://lab.msdn.microsoft.com/express/vwd/.

2.Click the link to download from here, and you will see the page shown in Figure B-1 asking you whether you want to run or save the file.

654

Setup

If you’re using a dialup connection, please remain connected after you have downloaded this file, because you will still need an active connection later during the installation process.

Figure B-1

3.Click Run and you will see the dialog shown in Figure B-2 once the vwdsetup.exe file has fully downloaded.

Figure B-2

4.Next, you will be asked whether you want to submit anonymous information about your setup experiences (see Figure B-3). If you want to send information about your setup experience, check the box. If not, click Next.

655

Appendix B

Figure B-3

5.In the next screen, shown in Figure B-4, you will see the end-user license agreement (EULA). You have to agree to this to continue, so check the box and click Next:

Figure B-4

656

Setup

6.After accepting the terms of the EULA, you then specify which installation options you want to install in addition to .NET 2.0 and Visual Web Developer Express. The only compulsory option for this book is SQL Server 2005 Express. As shown in Figure B-5, select that option and click Next.

Figure B-5

7.Next, you have to specify where to install VWD (see Figure B-6). I suggest selecting the default, which is C:\Program Files\Microsoft Visual Studio 8, and clicking Install to begin the setup.

Figure B-6

657

Appendix B

Unfortunately, downloading of all the components must now commence (see Figure B-7), and with 102MB this can take quite a while.

Figure B-7

Don’t be surprised to see Microsoft .NET Framework 2.0 continue to download for quite a while after reaching the 102MB; this isn’t unusual. Once it has completed this stage, it will begin installation of the downloaded components, shown in Figure B-8.

Figure B-8

658