- •Contents
- •Introduction
- •Acknowledgments
- •The Goals of ASP.NET 2.0
- •Developer productivity
- •Administration and management
- •Performance and scalability
- •Device-specific code generation
- •Additional New Features of ASP.NET 2.0
- •New developer infrastructures
- •New compilation system
- •Additions to the page framework
- •New objects for accessing data
- •New server controls
- •A New IDE for Building ASP.NET 2.0 Pages
- •The Document Window
- •Views in the Document Window
- •The tag navigator
- •Page tabs
- •Code change status notifications
- •Error notifications and assistance
- •The Toolbox
- •The Solution Explorer
- •Lost Windows
- •Other Common Visual Studio Activities
- •Creating new projects
- •Making references to other objects
- •Using smart tags
- •Saving and importing Visual Studio settings
- •Application Location Options
- •Built-in Web server
- •Web site requiring FrontPage Extensions
- •The ASP.NET Page Structure Options
- •Inline coding
- •New code-behind model
- •New Page Directives
- •New attributes
- •New directives
- •New Page Events
- •Cross-Page Posting
- •New Application Folders
- •\Code folder
- •\Themes folder
- •\Resources folder
- •Compilation
- •The New Data Source Controls
- •The SqlDataSource and GridView Controls
- •Reading data
- •Applying paging in the GridView
- •Sorting rows in the GridView control
- •Defining bound columns in the GridView control
- •Enabling the editing of rows in the GridView control
- •Deleting data from the GridView
- •Dealing with other column types in the GridView
- •Selecting which fields to display in the DetailsView control
- •Using the GridView and DetailsView together
- •Updating, inserting, and deleting rows
- •XmlDataSource Control
- •ObjectDataSource Control
- •SiteMapDataSource Control
- •DataSetDataSource Control
- •Visual Studio 2005
- •Connection Strings
- •Site Maps
- •The PathSeparator property
- •The PathDirection property
- •The ParentLevelsDisplayed property
- •The ShowToolTips property
- •Examining the parts of the TreeView control
- •Binding the TreeView control to an XML file
- •Selecting multiple options in a TreeView
- •Specifying custom icons in the TreeView control
- •Specifying lines used to connect nodes
- •Working with the TreeView control programmatically
- •Applying different styles to the Menu control
- •Menu Events
- •Binding the Menu control to an XML file
- •SiteMap Data Provider
- •SiteMapViewType
- •StartingNodeType
- •SiteMap API
- •Why Do You Need Master Pages?
- •The Basics of Master Pages
- •Coding a Master Page
- •Coding a Content Page
- •Mixing page types and languages
- •Specifying which master page to use
- •Working with the page title
- •Working with controls and properties from the master page
- •Nesting Master Pages
- •Container-Specific Master Pages
- •Event Ordering
- •Caching with Master Pages
- •Using ASP.NET 2.0 Packaged Themes
- •Applying a theme to a single ASP.NET page
- •Applying a theme to an entire application
- •Applying a theme to all applications on a server
- •Removing themes from server controls
- •Removing themes from Web pages
- •Removing themes from applications
- •Creating Your Own Themes
- •Creating the proper folder structure
- •Creating a skin
- •Including CSS files in your themes
- •Having your themes include images
- •Defining Multiple Skin Options
- •Programmatically Working with Themes
- •Themes and Custom Controls
- •Authentication
- •Authorization
- •ASP.NET 2.0 Authentication
- •Setting up your Web site for membership
- •Adding users
- •Asking for credentials
- •Working with authenticated users
- •Showing the number of users online
- •Dealing with passwords
- •ASP.NET 2.0 Authorization
- •Using the LoginView server control
- •Setting up your Web site for role management
- •Adding and retrieving application roles
- •Deleting roles
- •Adding users to roles
- •Getting all the users of a particular role
- •Getting all the roles of a particular user
- •Removing users from roles
- •Checking users in roles
- •Using the Web Site Administration Tool
- •The Personalization Model
- •Adding a simple personalization property
- •Using personalization properties
- •Adding a group of personalization properties
- •Using grouped personalization properties
- •Defining types for personalization properties
- •Using custom types
- •Providing default values
- •Making personalization properties read-only
- •Anonymous Personalization
- •Enabling anonymous identification of the end user
- •Working with anonymous identification events
- •Anonymous options for personalization properties
- •Migrating Anonymous Users
- •Personalization Providers
- •Working with the Access personalization provider
- •Working with the SQL Server personalization provider
- •Using multiple providers
- •Building Dynamic and Modular Web Sites
- •Introducing the WebPartManager control
- •Working with zone layouts
- •Understanding the WebPartZone control
- •Explaining the WebPartPageMenu control
- •Modifying zones
- •Caching in ASP.NET 1.0/1.1
- •Output caching
- •Partial page caching
- •Data caching using the Cache object
- •Cache dependencies
- •ASP.NET 2.0 unseals the CacheDependency class
- •Enabling databases for SQL Server cache invalidation
- •Enabling tables for SQL Server cache invalidation
- •Looking at SQL Server
- •Looking at the tables that are enabled
- •Disabling a table for SQL Server cache invalidation
- •Disabling a database for SQL Server cache invalidation
- •Configuring your ASP.NET Application
- •Adding more than one table to a page
- •Attaching SQL Server cache dependencies to the Request object
- •Attaching SQL Server cache dependencies to the Cache object
- •Customizing the side navigation
- •Examining the AllowReturn attribute
- •Working with the StepType attribute
- •Adding a header to the Wizard control
- •Utilizing Wizard control events
- •Working with images from disk
- •Resizing images
- •Displaying images from streams
- •The MMC ASP.NET Snap-In
- •General
- •Custom Errors
- •Authorization
- •Authentication
- •Application
- •State Management
- •Advanced
- •ASP.NET Web Site Administration Tool
- •Home
- •Security
- •Profile
- •Application
- •Provider
- •Managing the Site Counter System
- •Generics
- •Iterators
- •Anonymous Methods
- •Operator Overloading
- •Visual Basic XML Documentation
- •New Visual Basic Keywords
- •Continue
- •Using
- •Global
- •Index
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
