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

Beginning Visual Basic 2005 (2006)

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

Chapter 18

To complete this exercise, make the following changes to the pages of the site. As you make these changes, type the HTML markup into the pages as highlighted. You will get firsthand experience working with the IntelliSense for ASP.NET 2.0. As you type, you will be able to set properties and attributes quickly. You may find it faster than dragging controls from the Toolbox onto the form.

1.Main.master:

<%@ Master Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”> </script>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>Untitled Page</title> </head>

<body bgcolor=”black”>

<form id=”form1” runat=”server”> <div>

<table cellpadding=”5” cellspacing=”0” width=”600” height=”400” bgcolor=”white” border=”1” bordercolor=”black”>

<tr>

<td width=”150” valign=”top”> <!-- Menu Column -->

<asp:Menu ID=”Menu1” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/Default.aspx” Text=”Home”></asp:MenuItem>

<asp:MenuItem Text=”Members”>

<asp:MenuItem NavigateUrl=”Members/ViewAuthors.aspx” Text=”View Authors”></asp:MenuItem>

<asp:MenuItem NavigateUrl=”Members/ViewTitles.aspx” Text=”View Titles”></asp:MenuItem>

</asp:MenuItem> <asp:MenuItem Text=”Admin”>

<asp:MenuItem NavigateUrl=”Admin/ViewUser.aspx” Text=”View Users”></asp:MenuItem>

</asp:MenuItem>

</Items>

</asp:Menu>

<br /><br /><br />

<asp:LoginView ID=”LoginView1” Runat=”server”> <AnonymousTemplate>

<asp:Menu ID=”Menu2” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/CreateNewUser.aspx” Text=”Create Account”></asp:MenuItem>

596

Forms Authentication

</Items>

</asp:Menu>

</AnonymousTemplate>

<LoggedInTemplate>

<asp:Menu ID=”Menu3” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/ChangePassword.aspx” Text=”Change Password”></asp:MenuItem>

</Items>

</asp:Menu>

</LoggedInTemplate>

</asp:LoginView>

</td>

<td valign=”top”>

<table cellpadding=”0” cellspacing=”0” width=”100%” border=”0”> <tr>

<td width=”85%”>

<asp:Label ID=”Label1” Runat=”server” Text=”My First Company Site” Font-Bold=”true” Font-Size=”24px”></asp:Label>

</td>

<td width=”15%”>

<!-- Login Status Area -->

<asp:LoginStatus ID=”LoginStatus1” Runat=”server” /> </td>

</tr>

<tr>

<td colspan=”2”>

<hr color=”black” size=”2” /> </td>

</tr>

<tr>

<td colspan=”2”> User:

<asp:LoginView ID=”LoginView2” Runat=”server”> <AnonymousTemplate>Guest, Please log in</AnonymousTemplate> <LoggedInTemplate><asp:LoginName ID=”LoginName1”

Runat=”server” /></LoggedInTemplate> </asp:LoginView>

</td>

</tr>

<tr>

<td colspan=”2”>

<hr color=”black” size=”2” /> </td>

</tr>

<tr>

<td colspan=”2” valign=”top” height=”100%”> <asp:contentplaceholder id=”cphMain” runat=”server”> </asp:contentplaceholder>

</td>

</tr>

</table>

597

Chapter 18

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

2.In Default.aspx (under root folder), delete the default code and insert the following code (see Figure 18-11):

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Home” %>

3.In Login.aspx (see Figure 18-12), delete the default code and add the following code:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Login” %> <asp:content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Login ID=”Login1” runat=”server”> </asp:Login>

</asp:content>

Figure 18-11

598

Forms Authentication

Figure 18-12

4.In ChangePassword.aspx (see Figure 18-13), delete the default code and add the following code:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Change Password” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:ChangePassword ID=”ChangePassword1” Runat=”server”> </asp:ChangePassword>

</asp:Content>

5.In CreateNewUser.aspx (see Figure 18-14), delete the default code and add the following code:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Create New Account” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:CreateUserWizard ID=”CreateUserWizard1” Runat=”server”> </asp:CreateUserWizard>

</asp:Content>

599

Chapter 18

Figure 18-13

6.In ViewAuthors.aspx, delete the default code and add the following:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Authors” %> <asp:Content ID=”Content1” ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View Author Info Later”></asp:Label>

</asp:Content>

7.In ViewTitles.aspx, delete the default code and add the following:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Titles” %> <asp:Content ID=”Content1” ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View Title Info Later”></asp:Label>

</asp:Content>

8.In ViewUsers.aspx, delete the default code and add the following:

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Users” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View User Info Later”></asp:Label>

</asp:Content>

600

Forms Authentication

Figure 18-14

9.Now test the site. You can add a new account and then log in. Test the new pages to make sure they all work correctly. All of the authentication functionality will work. As you test the site using IIS and work with the configuration tool, you may see errors where the Access database is locked by another process. You can shut down the Web servers to free the lock.

How It Works

As you played with the new site, you should have been amazed. In older technologies, that level of functionality would have taken days to complete.

All you did for the site layout was on the master page. Let me explain these changes.

The first change sets the background color for the page to black:

<body bgcolor=”black”>

The next change involved the table layout for the page. Tables are a common layout tool in Web development. To set up the table layout, you added rows and cells and, in some cases, nested tables inside of cells. Table layout is an art you will learn with experience. First, take a look at the HTML tags used to format a table in the following table.

601

Chapter 18

Tag

Description

 

 

<table>

The root tag for a table.

<tr>

A row in a table.

<td>

A cell in a row in a table.

When you set up the table layout, the cells by default were spaced apart. Setting the cellpadding and cellspacing properties made it easier to lay out the page the way you wanted it. The colspan attribute of the cell tag allowed the cell to span 2 columns. Basically, both columns were merged into one in that row.

The main table was set to a size of 600 by 400 with a white background. This allowed for the table to maintain a standard size even when no content was present. The main table contained one row and two columns. The first column was used for the menu. The second column had a nested table with 5 rows that contained one or two columns each. This table was set to a width of 100% to force the table to fill up the second column. This allowed the HR controls to span the entire length of the parent column. The second table was for the site title, user name, and content place holder.

<table cellpadding=”5” cellspacing=”0” width=”600” height=”400” bgcolor=”white” border=”1” bordercolor=”black”>

<tr>

<td width=”150” valign=”top”> </td>

<td valign=”top”>

<table cellpadding=”0” cellspacing=”0” width=”100%” border=”0”> <tr>

<td width=”85%”> </td>

<td width=”15%”> </td>

</tr>

<tr>

<td colspan=”2”>

<hr color=”black” size=”2” /> </td>

</tr>

<tr>

<td colspan=”2”> </td>

</tr>

<tr>

<td colspan=”2”>

<hr color=”black” size=”2” /> </td>

</tr>

<tr>

<td colspan=”2” valign=”top” height=”100%”> </td>

602

Forms Authentication

</tr>

</table>

</td>

</tr>

</table>

The next part of the master page is the menu. Just as in Chapter 17, you add the menu control for site navigation. The difference is that you add multiple menu controls based on the login status. The LoginView control templates you worked with allows the user to see a validate menu based on the authentication status of the user.

<asp:Menu ID=”Menu1” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/Default.aspx” Text=”Home”></asp:MenuItem> <asp:MenuItem Text=”Members”>

<asp:MenuItem NavigateUrl=”Members/ViewAuthors.aspx” Text=”View Authors” /> <asp:MenuItem NavigateUrl=”Members/ViewTitles.aspx” Text=”View Titles”>

</asp:MenuItem> <asp:MenuItem Text=”Admin”>

<asp:MenuItem NavigateUrl=”Admin/ViewUser.aspx” Text=”View Users” /> </asp:MenuItem>

</Items>

</asp:Menu>

<br /><br /><br />

<asp:LoginView ID=”LoginView1” Runat=”server”> <AnonymousTemplate>

<asp:Menu ID=”Menu2” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/CreateNewUser.aspx” Text=”Create Account” /> </Items>

</asp:Menu>

</AnonymousTemplate>

<LoggedInTemplate>

<asp:Menu ID=”Menu3” Runat=”server”> <Items>

<asp:MenuItem NavigateUrl=”~/ChangePassword.aspx” Text=”Change Password” /> </Items>

</asp:Menu>

</LoggedInTemplate>

</asp:LoginView>

The Web site title was added with a Label control:

<asp:Label ID=”Label1” Runat=”server” Text=”My First Company Site” Font-Bold=”true” Font-Size=”24px”></asp:Label>

Under the title, you added a section to display the user name. Again, you used the LoginView control and displayed the name when the user was logged in. For anonymous users, you displayed “Guest, Please log in.”

<!-- Login Status Area -->

<asp:LoginStatus ID=”LoginStatus1” Runat=”server” /> User:

603

Chapter 18

<asp:LoginView ID=”LoginView2” Runat=”server”> <AnonymousTemplate>Guest, Please log in</AnonymousTemplate> <LoggedInTemplate>

<asp:LoginName ID=”LoginName1” Runat=”server” /> </LoggedInTemplate>

</asp:LoginView>

Finally, to validate the site layout, you added a contentplaceholder control. This control is where the actual content will appear on other pages.

<asp:contentplaceholder id=”cphMain” runat=”server”></asp:contentplaceholder>

The change you made to the default page was to add the hookup to the master page and change the title to Home.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Home” %>

For the Login page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page. This is where you allow content to be added throughout the site.

Inside the Content control, you placed a Login control, and it was displayed in the appropriate location on the page. The Login control has all of the logic you need built in.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Login” %> <asp:content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Login ID=”Login1” runat=”server”> </asp:Login>

</asp:content>

For the Change Password page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page.

Inside the Content control, you placed the ChangePassword control, and it was displayed in the appropriate location on the page. The ChangePassword control has all of the logic you need built in.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Change Password” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:ChangePassword ID=”ChangePassword1” Runat=”server”> </asp:ChangePassword>

</asp:Content>

For the Create New User page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page.

Inside the Content control, you placed the CreateUserWizard control, and it was displayed in the appropriate location on the page. The CreateUserWizard control has all of the logic you need built in.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”Create New Account” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:CreateUserWizard ID=”CreateUserWizard1” Runat=”server”>

604

Forms Authentication

</asp:CreateUserWizard>

</asp:Content>

For the View Authors page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page.

Inside the Content control you placed the Label control, and it was displayed in the appropriate location on the page. This page was left without functionality for this project.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Authors” %> <asp:Content ID=”Content1” ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View Author Info Later”></asp:Label>

</asp:Content>

For the View Titles page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page.

You placed the Label control inside of the Content control, and it was displayed in the appropriate location on the page. This page was left without functionality for this project.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Titles” %> <asp:Content ID=”Content1” ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View Title Info Later”></asp:Label>

</asp:Content>

For the View Users page, you changed the title, added the Content control, and hooked it up with the contentplaceholder you added on the master page.

Inside the Content control, you placed the Label control, and it was displayed in the appropriate location on the page. This page was left without functionality for this project.

<%@ Page Language=”VB” MasterPageFile=”~/Main.master” Title=”View Users” %> <asp:Content ContentPlaceHolderID=”cphMain” Runat=”server”>

<asp:Label ID=”Label1” Runat=”server” Text=”Add Code to View User Info Later”></asp:Label>

</asp:Content>

You cannot access the View Users page yet. When you try, you will be sent to the Login.aspx page, because you are not a member of the Admin role. You will add the Admin user to the Admin role in the next Try It Out.

Try It Out

Managing Roles

The Web Site Administration Tool has an interface to mange roles.

1.To open the tool, click Website ASP.NET Configuration. From the home page, choose the Security tab. On the bottom half of the screen, you will see a table of options for Roles. Click the link to Create or Manage Roles. (See Figure 18-15.)

605