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

Professional ASP.NET 3.5 in C# and Visual Basic Part 28 pptx

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 (504.46 KB, 10 trang )

Evjen c04.tex V2 - 01/28/2008 12:45pm Page 224
Chapter 4: Validation Server Controls
end user clicks a button on the page. Listing 4-25 shows an example of separating the validation controls
on a user group page into different buckets.
Listing 4-25: Using the ValidationGroup property
<
%@ Page Language="VB" %
>
<
html xmlns=" />>
<
head runat="server"
>
<
title
>
Validation Groups
<
/title
>
<
/head
>
<
body
>
<
form id="form1" runat="server"
>
<
div


>
<
h1
>
St. Louis .NET User Group
<
/h1
>
<
p
>
Username:
<
asp:TextBox ID="TextBox1" Runat="server"
><
/asp:TextBox
>
&nbsp; Password:
<
asp:TextBox ID="TextBox2" Runat="server"
TextMode="Password"
><
/asp:TextBox
>
&nbsp;
<
asp:Button ID="Button1" Runat="server" Text="Login"
ValidationGroup="Login" /
>
<

br /
>
<
asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server"
Text="* You must submit a username!"
ControlToValidate="TextBox1" ValidationGroup="Login"
>
<
/asp:RequiredFieldValidator
>
<
br /
>
<
asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server"
Text="* You must submit a password!"
ControlToValidate="TextBox2" ValidationGroup="Login"
>
<
/asp:RequiredFieldValidator
>
<
p
>
Our main meeting is almost always held on the last Monday of the month.
Sometimes due to holidays or other extreme circumstances,
we move it to another night but that is very rare. Check the home page
of the web site for details. The special
interest groups meet at other times during the month. Check the SIG
page and visit their individual sites for more information.

You can also check our calendar page for a summary of events.
<
br /
>
<
/p
>
<
h2
>
Sign-up for the newsletter!
<
/h2
>
<
p
>
Email:
<
asp:TextBox ID="TextBox3" Runat="server"
><
/asp:TextBox
>
&nbsp;
<
asp:Button ID="Button2" Runat="server" Text="Sign-up"
ValidationGroup="Newsletter" /
>
&nbsp;
<

br /
>
<
asp:RegularExpressionValidator ID="RegularExpressionValidator1"
Runat="server"
Text="* You must submit a correctly formatted email address!"
ControlToValidate="TextBox3" ValidationGroup="Newsletter"
ValidationExpression="
\
w+([-+.]
\
w+)*@
\
w+([ ]
\
w+)*
\
.
\
w+([ ]
\
w+)*"
>
<
/asp:RegularExpressionValidator
>
<
br /
>
224

Evjen c04.tex V2 - 01/28/2008 12:45pm Page 225
Chapter 4: Validation Server Controls
<
asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server"
Text="* You forgot your email address!"
ControlToValidate="TextBox3" ValidationGroup="Newsletter"
>
<
/asp:RequiredFieldValidator
>
<
/p
>
<
/div
>
<
/form
>
<
/body
>
<
/html
>
The
ValidationGroup
property on this page is shown in bold. You can see that this property takes a
String
value. Also note that not only validation controls have this new property. The core server

controls also have the
ValidationGroup
property because things like button clicks must be associated
with specific validation groups.
In this example, each of the buttons has a distinct validation group assignment. The first button on the
form uses
Login
as a value, and the second button on the form uses
Newsletter
as a value. Then each of
the validation controls is associated with one of these validation groups. Because of this, when the end
user clicks the Login button on the page, ASP.NET recognizes that it should work only with
the validation server controls that have the same validation group name. ASP.NET ignores the validation
controls assigned to other validation groups.
Using this enhancement, you can now have multiple sets of validation rules that fire only when you want
them to fire (see Figure 4-13).
Figure 4-13
225
Evjen c04.tex V2 - 01/28/2008 12:45pm Page 226
Chapter 4: Validation Server Controls
Another great feature that has been added to validation controls is a property called
SetFocusOnError
.
This property takes a
Boolean
value and, if a validation error is thrown when the form is submitted,
the property places the page focus on the form element that receives the error. The
SetFocusOnError
property is used in the following example:
<

asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server"
Text="* You must submit a username!"
ControlToValidate="TextBox1" ValidationGroup="Login" SetFocusOnError="True"
>
<
/asp:RequiredFieldValidator
>
If
RequiredFieldValidator1
throws an error because the end user didn’t place a value in
TextBox1
,the
page is redrawn with the focus on
TextBox1
, as shown in Figure 4-14.
Figure 4-14
Note that if you have multiple validation controls on your page with the
SetFocusOnError
property set to
True
, and more than one validation error occurs, the uppermost form element that has a validation error
gets the focus. In the previous example, if both the username text box (
TextBox1
) and the password text
box (
TextBox2
) have validation errors associated with them, the page focus is assigned to the username
text box because it is the first control on the form with an error.
226
Evjen c04.tex V2 - 01/28/2008 12:45pm Page 227

Chapter 4: Validation Server Controls
Summary
Validation controls are a welcome addition for those developers moving from Active Server Pages to
ASP.NET. They bring a lot of functionality in a simple-to-use package and, like most things in the .NET
world, you can easily get them to look and behave exactly as you want them to.
Remember that the purpose of having forms in your applications is to collect data, but this data collection
has no meaning if the data is not valid. This means that you must establish validation rules that can be
implemented in your forms through a series of different controls — the validation server controls.
This chapter covered various validation controls in detail, including
❑ RequiredFieldValidator
❑ CompareValidator
❑ RangeValidator
❑ RegularExpressionValidator
❑ CustomValidator
❑ ValidationSummary
In addition to looking at the base validation controls, this chapter also discussed how to apply client-side
and server-side validations.
227
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 229
Working with Master Pages
Visual inheritance is a great new enhancement to your Web pages provided by ASP.NET 3.5. This
feature was introduced to ASP.NET in version 2.0. In effect, you can create a single template page
that can be used as a foundation for any number of ASP.NET content pages in your application.
These templates, called master pages, increase your productivity by making your applications easier
to build and easier to manage after they are built. Visual Studio 2008 includes full designer support
for master pages, making the developer experience richer than ever before. This chapter takes a
close look at how to utilize master pages to the fullest extent in your applications and begins by
explaining the advantages of master pages.
Why Do You Need Master Pages?
Most Web sites today have common elements used throughout the entire application or on a major-

ity of the pages within the application. For instance, if you look at the main page o f the Reuters
News Web site (found at
www.reuters.com
), you see common elements that are used throughout
the entire Web site. T hese common areas are labeled in Figure 5-1.
In this screen shot, notice the header section, the navigation section, and the footer section on the
page. In fact, nearly every page within the entire application uses these same elements. Even before
master pages, you had ways to put these elements into every page through a variety of means; but
in most cases, doing so posed difficulties.
Some developers simply copy and paste the code for these common sections to each and every
page that requires them. This works, but it’s rather labor intensive. However, if you use the
copy-and-paste method, whenever a change is required to one of these common sections of the
application, you have to go into each and every page and duplicate the change. That’s not much fun
and an ineffective use of your time!
In the days of Classic Active Server Pages, one popular o ption was to put all the common sections
into what was called an include file. You could then place this file within your page like this:
<
! #include virtual="/myIncludes/header.asp"
>
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 230
Chapter 5: Working with Master Pages
Figure 5-1
The problem with using
include
files was that you had to take into account the newly opened HTML
tags in the header
include
file. These tags had to be closed in the main document or in the footer
include
file. It was usually difficult to keep all the HTML tags in order, especially if multiple people worked on

a project. Web pages sometimes displayed strange results because of inappropriate or nonexistent tag
closings or openings. It was also difficult to work with
include
files in a visual designer. Using
include
files didn’t allow the developer to see the entire page as it would appear in a browser. The developer
ended up developing the page in sections and hoping that the pieces would come together as planned.
Many hours were wasted ‘‘chasing tables’’ opened in an include file and possibly closed later!
230
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 231
Chapter 5: Working with Master Pages
With the introduction of ASP.NET 1.0 in 2000, developers started using user controls to encapsulate
common sections of their Web pages. For instance, you could build a Web page that included header,
navigation, and footer sections by simply dragging and dropping these sections of code onto each page
that required them.
This technique worked, but it also raised some issues. Before Visual Studio 2005 and ASP.NET 2.0, user
controls caused problems similar to those related to
include
files. When you worked in the design view
of your Web page, the common areas of the page displayed only as gray boxes in Visual Studio .NET
2002 and 2003. This made it harder to build a page. You could not visualize what the page you were
building actually looked like until you compiled and ran the completed page in a browser. User controls
also suffered from the same problem as
include
files — you had to match up the opening and closing
of your HTML tags in two separate files. Personally, we prefer user controls over
include
files, but user
controls aren’t perfect template pieces for use throughout an application. You will find t hat Visual Studio
corrects some of the problems by rendering user-control content in the design view. User controls are

ideal if you are including only small sections on a Web page; they are still rather cumbersome, however,
when working with larger page templates.
In light of the issue s with
include
files and user controls, the ASP.NET team developed the idea of master
pages — an outstanding new way of applying templates to your applications. They inverted the way the
developer attacks the problem. Master pages live outside the pages you develop, while user controls live
within your pages and are doomed to duplication. These master pages draw a more distinct line between
the common areas that you carry over from page to page and the content areas that are unique on each
page. You will find that working with master pages is easy and fun. The next section of this chapter looks
at some of the basics of master pages in ASP.NET.
The Basics of Master Pages
Master pages are an easy way to provide a template that can be used by any number of ASP.NET pages in
your application. In working with master pages, you create a master file that is the template referenced by
a subpage or content page. Master pages use a
.master
file extension, whereas content pages use the
.aspx
file extension you’re used to; but content pages are declared as such within the file’s
Page
directive.
You can place anything you want to be included as part of the template in the
.master
file. This can
include the header, navigation, and footer sections used across the Web application. The content page
then contains all the page content except for the master page’s elements. At runtime, the ASP.NET engine
combines these elements into a single page for the end user. Figure 5-2 shows a diagram of how this
process works.
One of the nice things about working with master pages is that you can visually see the template in the
IDE when you are creating the content pages. Because you can see the entire page while you are working

on it, it is much easier to develop content pages that use a template. While you are working on the content
page, all the templated items are shaded gray and are not editable. The only items that are alterable are
clearly shown in the template. These workable areas, called content areas, originally are defined in the
master page itself. Within the master page, you specify the areas of the page that the content pages can
use. You can have more than one content area in your master page if you want. Figure 5-3 shows the
master page with a couple of content areas shown.
231
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 232
Chapter 5: Working with Master Pages
Figure 5-2
Figure 5-3
232
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 233
Chapter 5: Working with Master Pages
If you look at the screenshot from Figure 5-3, you can sort of see two defined areas on the page — these
are content areas. The content area is represented in the design view of the page by a light dotted box
that represents the ContentPlaceHolder control. Also, if you hover your mouse over the content area, you
will see the name of the control appear above the control (although lightly). This hovering is also shown
in action in Figure 5-3.
Companies and organizations will find using master pages ideal, as the technology closely models their
typical business requirements. Many companies have a common look and feel that they apply across
their intranet. They can now provide the divisions of their company with a
.master
file to use when
creating a department’s section of the intranet. This process makes it quite easy for the company to keep
a consistent look and feel across its entire intranet.
Coding a Master Page
Now look at building the master page shown previously in Figure 5-3. You can create one in any
text-based editor, such as Notepad or Visual Web Developer Express Edition, or you can use the new
Visual Studio 2008. In this chapter, you see how it is done with Visual Studio 2008.

Master pages are added to your projects in the same way as regular
.aspx
pages — choose the Master
Page option when you add a new file to your application, as shown in Figure 5-4.
Figure 5-4
Because it’s quite similar to any other
.aspx
page, the Add New Item dialog enables you to choose from
a master page using the inline coding model or a master page that places its code in a separate file. Not
placing your server code in a separate file means that you use the inline code model for the page you are
creating. This option creates a single
.master
page. Choosing the option to place your code in a separate
file means that you use the new code-behind model with the page you are creating. Selecting the check
box ‘‘Place code in separate file’’ creates a single
.master
page, along with an associated
.master.vb
or
233
Evjen c05.tex V2 - 01/28/2008 12:47pm Page 234
Chapter 5: Working with Master Pages
.master.cs
file. You also have the option of nesting your master page within another master page by
selecting the Select master page option, but this is covered later in this chapter.
A sample master page that uses the inline-coding model is shown in Listing 5-1.
Listing 5-1: A sample master page
<
%@ Master Language="VB" %
>

<
script runat="server"
>
<
/script
>
<
html xmlns=" />>
<
head runat="server"
>
<
title
>
My Company Master Page
<
/title
>
<
asp:ContentPlaceHolder id="head" runat="server"
>
<
/asp:ContentPlaceHolder
>
<
/head
>
<
body
>

<
form id="form1" runat="server"
>
<
table cellpadding="3" border="1"
>
<
tr bgcolor="silver"
>
<
td colspan="2"
>
<
h1
>
My Company Home Page
<
/h1
>
<
/td
>
<
/tr
>
<
tr
>
<
td

>
<
asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server"
>
<
/asp:ContentPlaceHolder
>
<
/td
>
<
td
>
<
asp:ContentPlaceHolder ID="ContentPlaceHolder2"
runat="server"
>
<
/asp:ContentPlaceHolder
>
<
/td
>
<
/tr
>
<
tr
>

<
td colspan="2"
>
Copyright 2008 - My Company
<
/td
>
<
/tr
>
<
/table
>
<
/form
>
<
/body
>
<
/html
>
This is a simple master page. The great thing about creating master pages in Visual Studio 2008 is that
you can work with the master page in code view, but you can also switch over to design view to create
your master pages just as any other ASP.NET page.
234

×