1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 2.0 DEMYSTIFIED phần 7 doc

28 319 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 1,85 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

When a visitor selects a radio button, the circle associated with that radio button is darkened and circles for the rest of the radio buttons within the group are left lightened, indicat

Trang 1

Figure 8-3 Your choice of item is displayed when you click Submit

Radio Buttons

A radio button is a circle that appears alongside text and must exist within a set of

radio buttons Only one radio button in the set can be selected The text, such as

Male or Female, can be selected by a visitor to your web site The circle darkens

when the visitor selects the radio button; otherwise, the circle isn't darkened

Related radio buttons are always organized into a group Only one radio button

within the group can be selected When a visitor selects a radio button, the circle

associated with that radio button is darkened and circles for the rest of the radio

buttons within the group are left lightened, indicating they are unselected

For example, we can insert two radio buttons on the form-Male and Female-and

then place them in a group called Gender If the visitor selects Male (darken circle),

then Female is automatically left unselected (light color circle) If the visitor then

decides to select Female, the Male radio button is automatically unselected

Trang 2

ASP.NET 2.0 Demystified

Creating a Radio Button

There are two ways to create a radio button You could use a single radio button or

a radio button list Both are found on the Toolbox Many ASP developers prefer to use the radio button list because radio buttons are automatically grouped together and are easy to maintain

Let's create a radio button using the RadioButtonList control:

1 Drag and drop the RadioButtonList from the Toolbox to the Design tab

2 Select the Items property and you'll notice three dots ( .) appear alongside the word Collection This is similar to what happens when you select the Items property for a drop-down list box

3 Select the three dots to display the ListItem Collection Editor dialog box This is where you enter information about the radio buttons, just as you did for the drop-down list box (Figure 8-4)

4 Click Add and then enter the text for the first radio button Repeat these steps for each radio button A new radio button is entered into the group each time you click Add You can remove a radio button by selecting the button from the list and then clicking Remove We'll create two radio buttons The first is Male and the second is Female (Figure 8-4)

5 You can set a default value by changing the Select value from False to True You should always make one radio button the default selection; otherwise, the application won't have a value should the visitor fail to select

a radio button

6 Click OK

Drag and drop a Button from the Toolbox onto the Design as you did with the drop-down list box example Be sure to set the Text property of the button to Submit Change the ID to SubmitButton

Accessing the Selected Radio Button

You insert code to respond to a radio button within the Submit button's event handler similar to the code that you used in the drop-down list box example

Trang 3

Figure 8-4 Click Add to enter information about each radio button

Here's what you need to do:

1 Double-click the Submit button to display the event handler

2 Enter the following code Notice that the value of the Case statement is the text of each radio button You'll recall from the section on the drop-down list box that the text is automatically assigned to the Value if you leave the Value blank

Select Case RadioButtonListl.SelectedItem.Value

Trang 4

ASPONET 2.0 Demystified

Figure 8-5 A message confirms that the Female radio button was selected

Press CTRL-FS to run the application Figure 8-5 shows what you'll see when you select the Female radio button and then click Submit

Check Boxes

A check box is similar to a radio button in that the visitor makes a choice by selecting

a check box However, a check box doesn't have to be within a set of check boxes You can have one check box And unlike with a radio button, selecting one check box has no effect on other check boxes A check mark appears in the check box if it

is selected; otherwise, the check box appears empty

However, check boxes aren't grouped the same way as radio buttons are grouped The status of one check box doesn't affect the statuses of the other check boxes That

is, the visitor can select two check boxes and both remain selected, as compared to

a radio button, where the selection of one radio button causes other radio buttons in the group to be unselected

Trang 5

Creating a Check Box

Dragging and dropping the check box icon from the Toolbox onto the Design tab creates a check box on your web page The Text property of the check box is used

to set the text that appears alongside the check box on the page

You should position related text boxes together because this makes it easy for the visitor to make selections without looking for check boxes all around your page Avoid using too many check boxes, since this tends to clutter your web page and confuse your visitor

You can check the check box by setting the Checked property to True This causes a check mark to appear in the check box when the check box is displayed The visitor must then uncheck the box; otherwise, the application treats this check box as if the visitor checked the box

Let's create a check box (Figure 8-6)

1 Drag and drop two check boxes from the Toolbox onto the Design tab

2 Select the first check box

Figure 8-6 Create two check boxes and a Submit button

Trang 6

ASPONET 2.0 Demystified

3 Set the ID property to Newcustomer and the Text property to "New

Customer."

4 Select the second check box

5 Set the ID property to YesNewsletter and the Text property to "Send me your newsletter." Also, change the Checked property from False to True

6 Drag and drop a button from the Toolbox to the Design tab and set the ID and label for the button

Your application determines whether or not a check box is selected from within the Submit button event handler You must examine the value of the Checked property for each check box that appears on the web page by using an If Then statement If the Checked property is true, then the check box was selected; otherwise, the visitor didn't select the check box

Here's how to code the event handler to determine the state of a check box:

1 Double-click the Submit button to display the event handler

2 Enter the following code

3 Press ~5 to run the application Figure 8-7 shows what you'll see if you

select the New Customer check box and then click Submit Remember

that "Send me your newsletter." is already checked by default

Selecting Check Boxes from Within Your Application

You can select or unselect a check box from within your application by changing the value of the Checked property

Here's what you do to check the check box:

1D.Checked = True

Trang 7

Drop-Down Lists, Radio Buttons, Check Boxes

Figure 8-7 The application detects which check boxes are checked

Here's how to uncheck the check box:

1D.Checked = False

Remember to replace the ID in the previous examples with the ID for the

check box

Designing your web page using drop-down lists, radio buttons, and check boxes is

one of the most efficient ways to gather information from the visitor to your web

site Visitors can make their choices quickly with a few clicks of the mouse and not

worry about typing information using the keyboard

Trang 8

ASP.NET 2.0 Demystified

A drop-down list box contains two or more items that are hidden from sight

(unless one has been selected) until the visitor selects the down arrow that is adjacent to the drop-down list box The visitor then selects an item from the list once the list is displayed Your application retrieves the selected item by examin- ing the Value property of the drop-down list box and comparing it to the Text of each item on the list

A radio button is grouped together with other related radio buttons in a RadioButtonList Each radio button presents the visitor with a choice Only one can

be selected Other radio buttons in the group are automatically unselected when one radio button within the group is chosen Your application detects which radio button was selected by examining the Value property of the RadioButtonList This is the same technique used to detect an item selected from a drop-down list box

A check box is also used to present an option to the visitor However, the status of

a check box doesn't affect the statuses of other check boxes, if any, on the web page Your application determines if the visitor checked a check box by examining the check box's Checked property If the property is True, then the visitor selected the check box; otherwise, the check box wasn't selected You can check or uncheck a check box from within your application by setting the Checked property in your code

d None of the above

3 What is assigned to the Value property of an item in a drop-down list box

if you don't assign anything to the Value property?

a Nothing is assigned to the Value property

b The value of the ID property

c The value of the Text property

d You must assign a value to the Value property

Trang 9

8 Drop-Down Lists, Radio Buttons, Check Boxes

4 Unless you use the UP m o w and o o m ARROW keys to change the order, in what order do items appear in a drop-down list box?

a The order in which they are entered

b Alphabetical order

c Numerical order

d Random order

5 An If ElseIf statement might be used to evaluate a check box because

a All check boxes within a group must be examined

b All check boxes must be examined, including those outside the group

c If one check box is true, you don't need to examine other radio buttons within the group

d None of the above

6 What happens when the Boolean value of an item is set to true in a drop- down list box?

a The item is selected by default if the visitor doesn't select an item from the list

b The item isn't displayed

c The name of the item is set to true

d The name of the item is set to false

7 The selection of a check box affects the statuses of other check boxes

Trang 10

ASP.NET 2.0 Demystified

Answers

1 a True

2 a A drop-down list box

3 c The value of the Text property

4 a The order in which they are entered

5 d None of the above

6 a The item is selected by default if the visitor doesn't select an item from the list

Trang 11

CHAPTER

Databases

You probably access your bank account records by logging into the bank’s web site The bank’s web application compares your ID and password against those stored in their database If they match, then your account information is retrieved from

a database and displayed on the screen

This is referred to as a data-driven web application because the application cen- ters on providing you with data that is stored in a database accessible by the ASPNET engine Think of a database as a sophisticated electronic filing cabinet and the ASPNET engine as the file clerk

There are three components to a data-driven web application: the client, the server, and the database Throughout this book you have learned how to build the client and the server components of a web application In this chapter you begin to learn how to build that database component We’ll start by exploring the concept of a database and

how to design a database

Trang 12

ASP.NET 2.0 Demystified

Before wading knee-deep into learning about databases, it is important to clarify

a common misconception about databases You've probably read about popular data- base software packages such as MySQL, Microsoft Access, Oracle, DB2, and Microsoft SQL Server Sometimes these are referred to as databases-but they're not

As you'll learn in this chapter, a database is a collection of data organized so that

it can be quickly retrieved much like a filing cabinet A developer then writes code that inserts and removes information from the database This is a lot of work, but fortunately much of this code is already written in the form of a database manage- ment system, which is commonly referred to as a DBMS

The DBMS handles all the dirty details of how to store and retrieve information

in a database All a developer needs to do is to send the DBMS a query written in the Structured Query Language (SQL) Think of this as asking a file clerk to get you

an invoice from the filing cabinet You give the file clerk enough information to find the invoice, and the file clerk handles the details of locating and retrieving the invoice Just like a file clerk, the DMBS is responsible for maintaining the informa- tion that is stored in the database and responding to your queries

MySQL, Microsoft Access, Oracle, DB2, Microsoft SQL Server, and other pop- ular "databases" are DBMSs-not databases

Starting with this chapter you'll learn how to link your ASP.NET application to

a DBMS and write SQL queries to communicate with the DBMS Now that you have an overview, let's begin with a look at data

Data, Database, and Tables

An item of data is the smallest piece of information, such as a person's first name,

a person's last name, a street address, a city, a state, and a ZIP code Notice that we didn't say a person's name or a person's address is data, because they are not the smallest piece of information A person's name can be broken down to first name and last name-sometimes middle name These are data Likewise, a person's address can be subdivided into street, city, state, and ZIP code These too are data This subtle difference is important to keep in mind because many times you'll be respon- sible for identifying the data that will be used in a database

A database is a collection of data that is identified by a unique name to distin- guish the database from other databases A collection of people's first names, last

Trang 13

Databases

names, street addresses, cities, states, and ZIP codes form a table in a database

A DBMS is the software you use to

A group is called a table A table is like a table of a spreadsheet in that both have columns and rows A table is also given a unique name to distinguish it from other tables in the database Each column represents a piece of data and is identified by

a unique name, called a column name For example, the table that contains a per- son's name will have the following columns (see Figure 9-1) Each row (or record) represents one set of data, such as a person

Each column is defined by attributes that describe the characteristics of the data that is stored in the column Later in this chapter we'll take a look at these attri- butes; however, here are three commonly defined attributes:

Column name Name of the column

Data type The kind of data that can be stored in the column

Size The number of characters that can be stored in the column

Customer Table

Figure 9-1 A table within a database

Customer First Name

Trang 14

ASP.NET 2.0 Demystified

TIP: The database name, table name, column name, and its attributes are referred

to as metadata Metadata is data that describes other data

Relating Tables

You might be wondering how you access information that appears in two or more tables For example, if one table has customer information (i.e., customer name) and another table has order information, how to you link the customer information

to the customer's order information?

This is done by using a column that is common to both tables In this case,

a column called customer number is the common column Figure 9-2 shows two tables First is the customer table, and the other is the order table

The customer table is similar to Figure 9- 1 except we inserted the customer num- ber column The customer number is a unique number assigned to each customer Customer numbers are preferred over customer names to identify customers, be- cause two customers might have the same name

The order table consists of information that is typically associated with an order

We limited this to the order number, customer number, product number, and quan- tity ordered to show how tables are linked together We'll insert additional columns

in this table later in this chapter

Customer Table

1 :::;

Figure 9-2

Order Table Number 1 Product Number AlS67 BR8765 1 1 Quantity Customer Number

l

The customer table is linked to the order table using the customer number

Customer Middle Name Allen

Ngày đăng: 12/08/2014, 08:22