Tải bản đầy đủ (.pdf) (81 trang)

Beginning ASP.NET 1.1 with Visual C# .NET 2003 phần 10 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.55 MB, 81 trang )

Figure D-38
Formatting Blocks of Code
I often find Visual Studio .NET a bit messy at times – after adding this table, you can see that the code
generated on my system was hardly tidy. In my default preferences (you can specify your own
preferences via Tools | Options from the main menu) I specified that I wanted all tags to be in lowercase
when editing HTML code, and I wanted to use two spaces indentation for all lines of code, yet the editor
added the code shown in Figure D-39:
782
Appendix D
Figure D-39
The solution to this problem is to select all the code and use the
Edit | Advanced | Format Document menu
option. After running this, my code was spaced out a bit better, and my preferred capitalization rules
were followed as can be seen in Figure D-40:
783
Web Application Development Using Visual Studio .NET
Figure D-40
You will find that the results of this will vary according to the default preferences you have on your
system, but once you have set up the defaults as you like them, you'll find that this feature can be very
handy!
Developing the User Control
In this table, set the row and cell attributes as shown in the code below. Leave the last cell empty for the
moment:
<tr style="VERTICAL-ALIGN: middle">
<td style="TEXT-ALIGN: left" width="200">
<a href="default.aspx"><img src="images/logo.gif" border="0" /></a>
</td>
<td style="TEXT-ALIGN: center" width="400">
<img src="images/teamlogo.gif" />
</td>
<td style="TEXT-ALIGN: right" width="200">


784
Appendix D
</td>
</tr>
If you switch back to Design view, you'll see that the page taking shape as shown in Figure D-41:
Figure D-41
Recall from the chapters that the header control had an
AdRotator control that displayed different
pictures of authors each time the page was requested. It's now time to add the
AdRotator to the page.
Either drag and drop this control into the right-most cell, or click to position your cursor in this cell and
double-click the
AdRotator control to add it to the page. This is shown in Figure D-42:
785
Web Application Development Using Visual Studio .NET
Figure D-42
By default, the control is created with standard width and height attributes. To fit our images, you
should change these attributes in the
Properties pane so that the width is 100 pixels and the height is
95 pixels. To select the source for the
AdRotator control, you need to first create the XML source file.
Creating an XML File
Right click on the WroxUnited project and create a new file. In the dialog that appears (shown in Figure
D-43), create a new XML file and call it
faces.xml:
786
Appendix D
Figure D-43
In the newly created file, enter the code that we used previously and save the file:
<?xml version="1.0" encoding="utf-8" ?>

<Advertisements>
<Ad>
<ImageUrl>images/chrish_s_t.gif</ImageUrl>
<NavigateUrl>players.aspx</NavigateUrl>
<AlternateText>Player: Chris Hart</AlternateText>
<Impressions>80</Impressions>
<Keyword>ChrisH</Keyword>
</Ad>
<Ad>
<ImageUrl>images/chrisu_s_t.gif</ImageUrl>
<NavigateUrl>players.aspx</NavigateUrl>
<AlternateText>Player: Chris Ullman</AlternateText>
<Impressions>80</Impressions>
<Keyword>ChrisU</Keyword>
</Ad>
<Ad>
<ImageUrl>images/dave_s_t.gif</ImageUrl>
<NavigateUrl>players.aspx</NavigateUrl>
<AlternateText>Player: Dave Sussman</AlternateText>
<Impressions>80</Impressions>
<Keyword>Dave</Keyword>
</Ad>
<Ad>
<ImageUrl>images/john_s_t.gif</ImageUrl>
<NavigateUrl>players.aspx</NavigateUrl>
<AlternateText>Player: John Kauffman</AlternateText>
<Impressions>80</Impressions>
787
Web Application Development Using Visual Studio .NET
<Keyword>John</Keyword>

</Ad>
</Advertisements>
That's all you need to do to create an XML file. Notice that with each element, Visual Studio .NET assists
you by adding closing tags.
Back in the
header.ascx file, head to the Properties for the advertisement file again. Select the
AdvertisementFile attribute and click on the button that appears. In the popup dialog shown in
Figure D-44, select the
Faces.xml file and click OK:
Figure D-44
The header needs one last thing before it's finished, and that's the custom text. In
HTML view, add the
following code to the bottom of the control (below the last
</table> statement):
<h2><%= PageTitle %></h2>
788
Appendix D
Finally, right-click on Header.ascx and select View Code. You are now in the code-behind page for the
header control. Add the following line of code directly above the
Page_Load sub, as shown in Figure D-
45:
public string PageTitle = "";
Figure D-45
Time for a quick
File | Save All and that's the control done! Time to add it to the page!
Adding a User Control to a Page
Switch to the Design view for Default.aspx. Click and drag Header.ascx from the Solution Explorer
onto the page, right before the main header. This is shown in Figure D-46:
789
Web Application Development Using Visual Studio .NET

Figure D-46
You'll notice that this will add the control its reference to the HTML of the page. Also, notice that the
control appears as a gray box, with no design-time appearance (Web Matrix, you will recall, provided a
sample of the appearance of each user control as it was added to a page).
The newly added control is added with a default
TagName of Header and TagPrefix of UC1. You can
change this prefix to
WroxUnited (if you prefer) by editing the HTML of the page. Since we need to tidy
up this page in any case, let's do that now. Amend the highlighted lines of code (notice that you need to
remove the
<h1> tag and the default header text):
<%@ Register TagPrefix="WroxUnited" TagName="Header" Src="Header.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb"
debug="true" Inherits="WroxUnited.WebForm1"%>

<body>
<form id="Form1" method="post" runat="server">
<WroxUnited:header id="Header1" runat="server">
</WroxUnited:header>
790
Appendix D
<table id="Table1" cellspacing="0" cellpadding="0" width="800"
border="0">

The PageTitle attribute isn't used on this page, but it exists for use on other pages where the email
update registration box doesn't appear.
Running the page now will produce a more familiar screen, as shown in Figure D-47:
Figure D-47
Adding Custom Classes
It's time to look at adding a data access component. Right-click on the WroxUnited project and select

Add New Item In the dialog shown in Figure D-48, select the Class template, and name the file
DataAccessCode.cs.
Figure D-48
791
Web Application Development Using Visual Studio .NET
In the code, add some using statements at the top of the code:
using System;
using System.Data;
using System.Collections;
using System.Configuration;
Now cut and paste over the data access methods from the Default.aspx.cs code-behind page that we
produced earlier. Figure D-49 shows the code so far. Again, notice that the
+ and – icons on the left can
be clicked to expand or contract blocks of code to make it easier to see which methods exist in the code:
Figure D-49
Back in the code-behind for
Default.aspx, we've managed to break the code by removing the two
data access methods. If you select
Build | Build WroxUnited, you'll see Visual Studio attempt to compile
the project, but this process will now fail. The blue squiggly underlines in Figure D-50 indicate method
calls that VS.NET can no longer find:
792
Appendix D
Figure D-50
Add the following line of code directly above the
Page_Load method:
public DataAccessCode Data = new DataAccessCode();
Then prefix each underlined method with "Data.". The code should now be able to locate the methods
in question. Notice that as you do this, IntelliSense locates the list of available methods in this class and
gives you hints about what their signatures look like. This is shown in Figure D-51:

793
Web Application Development Using Visual Studio .NET
Figure D-51
Complete adding each method call with the
Data object prefix, and you will be able to run the page,
which should look and feel the same as before.
You can duplicate this methodology to create custom server controls in exactly the same way.
Professional and higher editions of Visual Studio .NET allow you to create entire custom class library
projects and custom server control projects, but this functionality isn't available in the Standard
language-specific editions.
Working with Databases Using the Server
Explorer
If you click the Server Explorer tab (at the bottom of the Toolbox), you can see the database connectivity
functionality of Visual Studio .NET. By default, this will look at little empty. Click on the
Connect to
Database
button to add a new connection, as shown in Figure D-52:
794
Appendix D
Figure D-52
In the dialog that appears, select your SQL Server database server, and then select the
WroxUnited
database from the list as shown in Figure D-53:
795
Web Application Development Using Visual Studio .NET
Figure D-53
In the
Server Explorer, you will now see the newly added database connection in the list. Expand this
connection, and double-click the Games table to view the contents of the table as shown in Figure D-54:
Figure D-54

796
Appendix D
In Standard edition, you can edit the data contained in the table, but cannot edit the structure of an
existing table or create a new table. However, you can enter SQL as shown in Figure D-55, and if you
know how to write SQL, you can enter any valid SQL statement, including
CREATE TABLE, DROP TABLE,
or even
CREATE DATABASE statements:
Figure D-55
If you are using Professional or higher editions of Visual Studio .NET, you can simply right-click on a
table and create a new table, or alter the design of a table using the context menu. You can also create
stored procedures and views in this way.
Debugging in Visual Studio .NET
One of the most powerful and indispensable features of Visual Studio .NET is its ability to debug code
and fix errors. First, we'll look at break points and stepping through code line-by-line at run-time to fix
run-time errors or exceptions, then we'll look at fixing errors at compile time.
797
Web Application Development Using Visual Studio .NET
Using Breakpoints
In Default.aspx.cs (the code-behind for the web page), click in the margin next to one of the lines of
code, for example, the first line of code in the event-handler for the clicking of the button on the page
shown in Figure D-56:
Figure D-56
This has added a breakpoint to the code, which means that when that line of code is processed, you can
step inside and see what's happening. To demonstrate this, run the page – notice that when you click the
button on the page, you automatically switch back to the screen shown in Figure D-57:
You can only debug if the compilation mode of your solution is set to Debug (see the
drop down box next to the
Run button on the main toolbar). Once you finish
debugging and are ready to deploy your site, set this to

Release – you'll notice a
significant performance boost on higher-traffic sites because debug code is much
more processor intensive. If you're not debugging, turn it off!
798
Appendix D
Figure D-57
At the bottom left of the screen, you can see the value entered into the textbox when the button was
clicked. You can watch the values of controls on the page using this window. You can step through this
code line by line to see what happens when each line of code is run by pressing
F11. When you reach a
method call, you can either press
F11 to jump into the code where that method is defined and watch as
the code in that method is executed line by line (Step Into), or you can press
F10 to Step Over this method
and continue with the next line of code in the current code block.
Pressing the
Start button again will continue execution of the page, taking you back to the browser
window, as if no interruption had occurred. This feature is particularly useful when fixing broken code.
All you have to do is add a break point before the piece of code that is broken and step into the code,
watching to see where the code breaks. This will often give you a clear idea as to why the code is not
running as expected.
Fixing Design-Time Errors
You will often find that errors at design time will prevent a page from compiling correctly. For example,
if you forgot to prefix one of the methods on the page with the
Data. object reference, you would see an
error message when you tried to run the page, as shown in Figure D-58:
799
Web Application Development Using Visual Studio .NET
Figure D-58
If you click

No, you will see an entry in the Task list shown in Figure D-58. It will give you a hint as to
why the code would not compile:
800
Appendix D
Figure D-59
It's now fairly obvious that the compiler can't deduce where the
AddNewFanEmail method is declared,
but once you add the
Data., prefix, it'll soon find what it's looking for.
Suggested Exercises and Further Reading
After completing this appendix, you will probably feel a bit more confident with creating web
applications in Visual Studio .NET. The best way to increase your confidence further is to try out adding
more controls and pages yourself. You may want to try to implement the whole of the Wrox United
application in Visual Studio .NET – this would give you experience of working with many different
controls in the Visual Studio .NET environment, as well as programming with many different ASP.NET
techniques.
Visual Studio .NET is a large product, so if you really want to learn more about this tool, visit
/>801
Web Application Development Using Visual Studio .NET

E
Installing and Configuring
IIS
The installation processes for IIS on Windows 2000 Professional, Windows XP Professional, or
Windows Server 2003 don't differ significantly. The main difference is that Windows 2000 installs
IIS 5.0, while Windows XP Professional Edition installs IIS 5.1, and Windows Server 2003 installs
IIS 6.0. The options for installing are exactly the same; the only thing that might differ is the look
of the dialog boxes.
Before you install it though, it's worth noting that you might not have to do much in this initial
stage, as it's possible you're already running IIS. We'll describe a process for checking whether this

is the case as part of the installation process. Note that to install anything (not just ASP.NET, but
literally anything) on Windows 2000, XP, and 2003 you need to be logged in as a user with
administrative rights. If you're uncertain about doing this, please consult your Windows
documentation. Right, let's get started!
Try It Out Creating a Virtual Directory and Setting Up Permissions
1. Go to the control panel (Start | Settings | Control Panel) and select the Add/Remove Programs
icon. The dialog will appear, displaying a list of your currently installed programs as
shown in Figure E-1:
You cannot install IIS on Windows XP Home Edition. It will only work on Windows
XP Professional.
Figure E-1
2. Select the Add/Remove Windows Components icon on the left side of the dialog to get to the
screen that allows you to install new windows components:
Figure E-2
3. Locate the Internet Information Services (IIS) entry in the dialog, and note the checkbox that
appears to its left. Unless you installed Windows 2000 or XP via a custom install and specifically
requested IIS, it's most likely that the checkbox will be unchecked.
804
Appendix E
4. If the checkbox is unchecked, check it and click Next on the screen shown in Figure E-2 to load
Internet Information Services. You might be prompted to place your Windows 2000 or XP
installation disk into your CD-ROM drive. It will take a few minutes to complete. Then go to
Step 5.
OR
If the checkbox is checked, you won't need to install the IIS component – it's already present on
your machine. Go to the Working With IIS section instead.
5. Click on the Details button – this will take you to the dialog shown in Figure E-3.
Figure E-3
6. There are a few options here for the installation of various optional bits of functionality. For
example, if the

World Wide Web Server option is checked then your IIS installation will be able to
serve and manage Web pages and applications. If you're planning to use FrontPage 2000 or
Visual Studio.NET to write your Web page code, then you'll need to ensure that the
FrontPage
2000 Ser ver Extensions
checkbox is checked. The Internet Information Services Snap-In is also very
helpful, as you'll see later in the chapter, so ensure that this is checked too; the other options
(although checked here) aren't necessary for this book:
How It Works
IIS starts up automatically as soon as its installation is complete, and thereafter whenever you boot up
Windows. Thus you don't need to run any startup programs or click on any short cuts.
IIS installs most of its components on your hard drive, under the
\WinNT\system32\inetsrv
directory; however, we are more interested in the \InetPub directory that is also created at this time.
This directory contains subdirectories that will provide the home for the Web page files that we create.
If you expand the
InetPub directory, you'll find that it contains several subdirectories:

\iissamples\homepage contains some examples of classic ASP pages.
805
Installing and Configuring IIS
❑ \iissamples\sdk contains a set of subdirectories that hold classic ASP pages which
demonstrate the various classic ASP objects and components.

\scripts is an empty directory, where ASP.NET programs can be stored.

\webpub is also empty. This is a special virtual directory, used for publishing files via the Publish
wizard. Note that this directory only exists if you are using Windows 2000 Professional Edition.

\wwwroot is the top of the tree for your Web site. This should be your default Web directory. It

also contains a number of subdirectories that contain various bits and pieces of IIS. This
directory is generally used to contain subdirectories that hold the pages that make up our Web
site – although, in fact, there's no reason why you can't store your pages elsewhere. The
relationship between physical and virtual directories is discussed later in this appendix.

\ftproot, \mailroot and \nntproot should form the top of the tree for any sites that use
FTP, mail or news services, if installed.
❑ In some versions of Windows, you will find a
\AdminScripts folder that contains various
script files for performing some common 'housekeeping' tasks on the Web server, allowing you
to stop and start services.
Working with IIS
Having installed IIS Web server software, you'll need some means of administering its contents and
settings. In this section, we see the user interface that is provided by IIS.
In fact, some versions of IIS provide two user interfaces, the Microsoft Management Console (MMC) and
the Personal Web Server (PWS) interface (which is just included for those people familiar with PWS from
Windows 98 and looking to migrate to IIS). Let's look at MMC, as the other interface is now obsolete.
The Microsoft Management Console (MMC)
The best part of MMC is that it provides a central interface for administrating all sorts of services that
are installed on your machine. We can use it to administer IIS. In fact, when we use it to administer other
services, the interface looks roughly the same. The MMC is provided as part of the Windows 2000
operating system – in fact, the MMC also comes with older Windows server operating systems.
The MMC itself is just a shell – on its own, it doesn't do much at all. If you want to use it to administer a
service, you have to add a snap-in for that service. The good news is that IIS has its own snap-in.
Whenever you need to administer IIS, simply call up the Internet Services Manager MMC console by
selecting
Start | Control Panel | Administrative Tools | Internet Services Manager:
Figure E-4
806
Appendix E

×