Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Chapter 4

This approach also works with XML Web services, even ones for which you can pass in parameters using HTTP-GET. You just set up the DataFile value in the following manner:

DataFile=”http://www.someserver.com/GetWeather.asmx/ZipWeather?zipcode=63301”

ObjectDataSource Control

The data source controls I have presented so far are really intended for applications working in a twotiered environment. In these cases, the presentation piece works with a data store that might reside on another server. The ObjectDataSource control enables you to use data source controls that work in a three-tiered environment. This data source control interacts with an object that you have established, and that object then interacts with a data store elsewhere.

The object needs to be structured properly in order to work with the ObjectDataSource control, meaning that the object can perform basic operations such as selects, updates, inserts, and deletes.

For a simple example of working with the ObjectDataSource control, you can create a class that gets its data from another XML file called Employee.xml. The Employee.xml file is shown in Listing 4-25.

Listing 4-25: Employee.xml

<?xml version=”1.0” encoding=”utf-8” ?> <Staff>

<Employee>

<FullName>Bill Evjen</FullName> <StartDate>02/2004</StartDate> <Salary>25,000</Salary>

</Employee>

<Employee>

<FullName>Fred Cotterell</FullName>

<StartDate>01/2000</StartDate>

<Salary>32,000</Salary>

</Employee>

<Employee>

<FullName>Tuija Pitkanen</FullName> <StartDate>04/2004</StartDate> <Salary>31,000</Salary>

</Employee>

</Staff>

The ObjectDataSource control then works with a business object that returns the information from the XML file. For this, create a class called Employees.vb or Employees.cs. This class utilizes only the selected aspect of the object because you are only interested in reading the data. This class is shown in Listing 4-26.

Listing 4-26: Employees.vb/.cs

VB

Imports System.Data

Public Class Employees

114

New Ways to Handle Data

Public Function GetEmployeeDetails() As DataSet Dim ds As New DataSet()

ds.ReadXml(HttpContext.Current.Server.MapPath(“Employee.xml”))

Return ds End Function

End Class

C#

using System; using System.Web; using System.Data;

public class Employees

{

public DataSet GetEmployeeDetails()

{

DataSet ds = new DataSet(); ds.ReadXml(HttpContext.Current.Server.MapPath(“~/Employee.xml”));

return ds;

}

}

In this object, the GetEmployeeDetails method is the select statement utilized by the ObjectDataSource control in the ASP.NET page. This method returns its result using the types of IEnumerable, DataSet, or DataTable. In this example, it returns a DataSet. Now that the Employees class is in place and it returns a list of employees from the XML file, you can create an .aspx page that makes use of this object via an ObjectDataSource control.

The final step is to create an .aspx page that displays the results that come from the Employees class. This is illustrated in Listing 4-27.

Listing 4-27: Using the ObjectDataSource control

<%@ Page Language=”VB”%>

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

<title>ObjectDataSource</title>

</head>

<body>

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

<asp:GridView ID=”GridView1” Runat=”server” DataSourceID=”ObjectDataSource1” BorderWidth=”1px” BackColor=”#DEBA84” CellPadding=”3” CellSpacing=”2” BorderStyle=”None” BorderColor=”#DEBA84”>

<FooterStyle ForeColor=”#8C4510” BackColor=”#F7DFB5”> </FooterStyle>

<PagerStyle HorizontalAlign=”Center” ForeColor=”#8C4510”> </PagerStyle>

<HeaderStyle ForeColor=”White” Font-Bold=”True” BackColor=”#A55129”> </HeaderStyle>

<SelectedRowStyle ForeColor=”White” Font-Bold=”True” BackColor=”#738A9C”>

(continued)

115

Chapter 4

Listing 4-27: (continued)

</SelectedRowStyle>

<RowStyle ForeColor=”#8C4510” BackColor=”#FFF7E7”> </RowStyle>

</asp:GridView>

<asp:ObjectDataSource ID=”ObjectDataSource1” Runat=”server”

TypeName=”Employees” SelectMethod=”GetEmployeeDetails”> </asp:ObjectDataSource>

</form>

</body>

</html>

Look first at the <asp:ObjectDataSource> control. Only two attributes within this control are doing all the work. The first is the TypeName property. The value used with this property points to the name of the class with which it works. In this case, it is working with the Employees class contained in the Employees.vb or Employees.cs file. The second important property is the SelectMethod property. This property points to the select function contained in the Employees class. In this case, it is the only method that the Employees class has — the GetEmployeeDetails method. After these items are in

place, you can bind this control to any of the data-bound controls. In this case, you use the GridView control and associate the two controls with the use of the DataSourceID property in the GridView control.

Putting all this together produces the results shown in Figure 4-30.

Figure 4-30

SiteMapDataSource Control

The SiteMapDataSource control is another new data source control that enables you to easily bind an application’s site navigation hierarchy data to some of ASP.NET 2.0’s latest navigation controls. In most cases, you bind the SiteMapDataSource control to an XML file that is referred to as a site map and has a

.sitemap extension. These site maps define the navigation hierarchy of your application and can then be utilized by controls such as the TreeView control.

The SiteMapDataSource control and the corresponding navigation controls are discussed in detail in Chapter 5.

116

New Ways to Handle Data

DataSetDataSource Control

The DataSetDataSource control enables you to bind ASP.NET controls to .NET-style DataSets. These might be DataSets that are produced from XML files, or from in-memory DataSets that you have created in your code. Using the DataSetDataSource is pretty straightforward because it is quite similar to working with the other data source controls. Listing 4-28 shows an example of using the DataSetDataSourceControl with the same Painters.xml file that was an example earlier in the chapter and binding it to a DropDownList control.

Listing 4-28: Using the DataSetDataSource control

<%@ Page Language=”VB”%>

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

<title>DataSetDataSource</title>

</head>

<body>

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

<asp:DropDownList ID=”Dropdownlist1” Runat=”server” DataTextField=”name”

DataSourceID=”DataSetDataSource1”>

</asp:DropDownList>

<asp:DataSetDataSource ID=”DataSetDataSource1” Runat=”server” DataFile=”~/Painters.xml”>

</asp:DataSetDataSource>

</form>

</body>

</html>

In this example, the DataSetDataSource control uses the DataFile attribute to get at the XML file, which is converted to a DataSet by the DataSetDataSource control. After it is converted, the DataSet can be used by the DropDownList control. The DropDownList control associates itself to the DataSetDataSource control via the use of the DataSourceID attribute. The field chosen to be displayed in the drop-down list is determined by using the DataTextField attribute. In this case, it points to the name attribute of the <Painters> element in the XML file. Running this page produces the results shown in Figure 4-31.

Figure 4-31

117