Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Summary

categoryList.SelectedItem.Value; comm.Parameters.Add("@SubjectID", System.Data.SqlDbType.Int); comm.Parameters["@SubjectID"].Value =

subjectList.SelectedItem.Value;

comm.Parameters.Add("@Description", System.Data.SqlDbType.NVarChar, 50);

comm.Parameters["@Description"].Value = descriptionTextBox.Text; comm.Parameters.Add("@StatusID", System.Data.SqlDbType.Int); comm.Parameters["@StatusID"].Value = 1;

If you now load the Help Desk page, you’ll see that it works just as it used to, but behind the scenes, it’s making use of a stored procedure. You can verify that this approach works by adding a new help desk request through the web form, then opening the HelpDesk table and checking for your new help desk request.

As you can see, using stored procedures is very easy. Apart for specifying the procedure’s name, you also need to set the CommandType of the SqlCommand object to StoredProcedure. That’s it! Everything else is the same as when working with a parameterized query.

Summary

In this chapter, you learned how to create simple web applications that interact with databases. First, you learned about the various classes included with ADO.NET, such as SqlConnection, SqlCommand, and SqlDataReader. Then, you learned how to use these classes to create simple applications that query the database, insert records into a database, update records within a database, and delete records from a database. You also learned important techniques for querying database data, including using parameters and control binding. Later in the chapter, you learned how to improve application performance through the use of stored procedures.

The next chapter will expand on what we learned here, and introduce a new control that’s often used to display data from a database: the DataList.

399

400

10

Displaying Content Using Data

 

 

Lists

Similar to the Repeater control, the DataList control allows you to bind and customize the presentation of database data. The fundamental difference is that while the Repeater requires you to build the template from scratch (allowing you to customize the generated HTML output in any way you like), the DataList control automatically generates a single-column HTML table for you, like the one shown below:

<table>

<tr>

<td>

<p>Employee ID: 1</p> <p>Name: Zak Ruvalcaba</p> <p>Username: zak</p>

</td>

</tr>

<tr>

<td>

<p>Employee ID: 2</p>

<p>Name: Jessica Ruvalcaba</p> <p>Username: jessica</p>

</td>

</tr>

<tr>

<td>

<p>Employee ID: 3</p>

Chapter 10: Displaying Content Using Data Lists

<p>Name: Ted Lindsey</p> <p>Username: ted</p>

</td>

</tr>

</table>

As you can see, DataList has, as the name implies, been designed to display lists of data, and while it’s less flexible than the Repeater, it contains more built-in functionality that can help make the implementation of certain features faster and easier. In the following pages, you’ll learn:

the basics of the DataList control

how to handle DataList events

how to edit DataList items

how to handle the controls inside the DataList templates

how to use Visual Web Developer to edit the DataList

Let’s get started!

DataList Basics

To learn how to use the DataList, we’ll update the Dorknozzle Employee Directory page to use a DataList control instead of a Repeater control. This update will be particularly easy to do because the Employee Directory already has a listlike format.

If you now open EmployeeDirectory.aspx, you’ll see the Repeater control is used like this:

File: EmployeeDirectory.aspx (excerpt)

<asp:Repeater id="employeesRepeater" runat="server"> <ItemTemplate>

Employee ID: <strong><%#Eval("EmployeeID")%></strong><br /> Name: <strong><%#Eval("Name")%></strong><br /> Username: <strong><%#Eval("Username")%></strong>

</ItemTemplate>

<SeparatorTemplate>

402

DataList Basics

<hr /> </SeparatorTemplate>

</asp:Repeater>

You can see the output of this code in Figure 9.9 in Chapter 9. Now, let’s update the employee directory page to use a DataList instead of a Repeater. We can do this simply by replacing the <asp:Repeater> and </asp:Repeater> tags with the tags for a DataList:

File: EmployeeDirectory.aspx (excerpt)

<asp:DataList id="employeesList" runat="server">

<ItemTemplate> Employee ID:

<strong><%#Eval("EmployeeID")%></strong><br /> Name: <strong><%#Eval("Name")%></strong><br /> Username: <strong><%#Eval("Username")%></strong>

</ItemTemplate>

<SeparatorTemplate> <hr />

</SeparatorTemplate>

</asp:DataList>

As we’ve changed the ID for this control, we’ll need to change the name of the control in the code-behind file as well. Locate the following lines of code and change employeesRepeater to employeesList, as shown:

Visual Basic

File: EmployeeDirectory.aspx.vb (excerpt)

'Open the connection conn.Open()

'Execute the command

reader = comm.ExecuteReader()

'Bind the reader to the DataList employeesList.DataSource = reader employeesList.DataBind()

'Close the reader

reader.Close()

C#

File: EmployeeDirectory.aspx.cs (excerpt

//Open the connection conn.Open();

//Execute the command

reader = comm.ExecuteReader();

// Bind the reader to the DataList employeesList.DataSource = reader; employeesList.DataBind();

403

Chapter 10: Displaying Content Using Data Lists

// Close the reader reader.Close();

As you can see, the changes required to use DataList instead of Repeater are minimal in this case. That’s largely because the Repeater was displaying a basic list of data anyway.

As with the Repeater control, we can feed data into the a DataList control by binding it to a data source. Both Repeater and DataList support the ItemTemplate and SeparatorTemplate templates, but in case of the DataList, the templates specify the content that is to be inserted in the td elements of the table.

At the moment, the output appears very similar to the output we generated using the Repeater, as Figure 10.1 illustrates.

Figure 10.1. The Dorknozzle Employee Directory page

Repeater vs DataList

As a rule of thumb, you’ll use the Repeater when you need total control over the HTML output, and when you don’t need features such as editing, sorting, formatting, or paging for the data you’re displaying. Depending on

404

DataList Basics

the extra features you need, you can use either the DataList control (covered in this chapter), or the GridView or DetailsView controls (which you’ll learn about in Chapter 12).

In this example, we’ve used the ItemTemplate of our DataList. The DataList offers a number of templates:

ItemTemplate

This template is replicated for each record that’s read from the data source. The contents of the ItemTemplate are repeated for each record, and placed inside td elements.

AlternatingItemTemplate

If this template is defined, it will be used instead of ItemTemplate to display every second element.

SelectedItemTemplate

This template is used to display the selected item of the list. The DataList control doesn’t automatically give the user a way to select an item in the list, but you can mark an item as selected by setting the DataLists control’s SelectedIndex property. Setting this property to 0 will mark the first item as selected; setting SelectedIndex to 1 will mark the second item as selected; and so on. Setting SelectedIndex to -1 unselects any selected item.

EditItemTemplate

Similar to SelectedItemTemplate, this template applies to an item that’s being edited. We can set the item being edited using the EditItemIndex property of the DataList, which operates in the same way as the SelectedIndex property. Later in this chapter, you’ll learn how to edit your

DataList using the EditItemTemplate.

HeaderTemplate

This template specifies the content to be used for the list header.

FooterTemplate

This template defines the list footer.

SeparatorTemplate

This template specifies the content to be inserted between two consecutive data items. This content will appear inside its own table cell.

405