- •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
New Ways to Handle Data
Clicking the New hyperlink provides a table that enables you to enter a new customer after you add an INSERT command to the AccessDataSource control just as was done with the UPDATE command. This is shown in Figure 4-26.
Figure 4-26
Be sure that if you enable the inserting of data into your data store, that you define the <InsertParameters> using the <asp:Parameter> control — just as was done in the updating of the data.
XmlDataSource Control
So far, you have been looking at using the data source controls that work with traditional data stores such as Microsoft SQL Server and Microsoft Access. Today, however, a considerable amount of data is stored in XML format, so a specific data source control has been added to ASP.NET 2.0 just for retrieving and working with XML data.
The XmlDataSource control enables you to connect to your XML data and to use this data with any of the ASP.NET data-bound controls. Just like the SqlDataSource and the AccessDataSource controls, the XmlDataSource control also enables you to not only retrieve data, but also to insert, delete, and update data items. With the world turning more and more to XML data formats, such as Web services, RSS feeds, and more, this control is a valuable resource for your applications.
To show the XmlDataSource control in action, first create a simple XML file and include this file in your application. Listing 4-22 shows a simple XML file of Russian painters that we can use.
109
Chapter 4
Listing 4-22: Painters.xml
<?xml version=”1.0” encoding=”utf-8” ?> <Artists>
<Painter name=”Vasily Kandinsky”> <Painting>
<Title>Composition No. 218</Title> <Year>1919</Year>
</Painting>
</Painter>
<Painter name=”Pavel Filonov”> <Painting>
<Title>Formula of Spring</Title> <Year>1929</Year>
</Painting>
</Painter>
<Painter name=”Pyotr Konchalovsky”> <Painting>
<Title>Sorrento Garden</Title> <Year>1924</Year>
</Painting>
</Painter>
</Artists>
Now that the Painters.xml file is in place, the next step is to use a DataList control and connect this DataList control to an <asp:XmlDataSource> control. This is illustrated in Listing 4-23.
Listing 4-23: Using a DataList control to display XML content
<%@ Page Language=”VB”%>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>XmlDataSource</title>
</head>
<body>
<form id=”form1” runat=”server”>
<asp:DataList ID=”DataList1” Runat=”server” DataSourceID=”XmlDataSource1”> <ItemTemplate>
<p><b><%# XPath(“@name”) %></b><br />
<i><%# XPath(“Painting/Title”) %></i><br /> <%# XPath(“Painting/Year”) %></p>
</ItemTemplate>
</asp:DataList>
<asp:XmlDataSource ID=”XmlDataSource1” Runat=”server” DataFile=”~/Painters.xml” XPath=”Artists/Painter”> </asp:XmlDataSource>
</form>
</body>
</html>
110
New Ways to Handle Data
This is a simple example, but it shows you the power and ease of using the XmlDataSource control. You should pay attention to only two attributes in this example. The first is the DataFile attribute. This attribute points to the location of the XML file. Because the file resides in the root directory of the application, it is simply ~/Painters.xml. The next attribute included in the XmlDataSource control is the XPath attribute. The XmlDataSource control uses XPath for the filtering of XML data. In this case,
the XmlDataSource control is taking everything within the <Painter> set of elements. The value Artists/Painter means that the XmlDataSource control navigates to the <Artists> element and then to the <Painter> element within the specified XML file.
The DataList control next must specify the DataSourceID as the XmlDataSource control. In the <ItemTemplate> section of the DataList control, you can retrieve specific values from the XML file by using XPath commands. The XPath commands filter the data from the XML file. The first value retrieved is an element attribute (name) that is contained in the <Painter> element. If you are retrieving an attribute of an element, you preface the name of the attribute with an @ symbol. In this case then, you simply specify @name to get at the painter’s name. The next two XPath commands go deeper into the XML file and get the specific painting and the year of the painting. Remember to separate nodes with a /. When run in the browser, this code produces the results illustrated in Figure 4-27.
Figure 4-27
Besides working from static XML files like the Painters.xml file shown earlier, the XmlDataSource file has the capability to work from dynamic, URL-accessible XML files. One popular XML format that is pervasive on the Internet today is blogs or weblogs. Blogs, or personal diaries, can be viewed either in the browser, through an RSS-aggregator, or just as pure XML.
As you look at my blog in Figure 4-28, you can see the XML it produces directly in the browser. (You can find a lot of blogs to play with for this example at weblogs.asp.net.)
111
Chapter 4
Figure 4-28
Now that you know the location of the XML from the blog, you can use this XML with the XmlDataSource control and display some of the results in a DataList control. The code for this example is shown in Listing 4-24.
Listing 4-24: Working with an RSS feed
<%@ Page Language=”VB”%>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>XmlDataSource</title>
</head>
<body>
<form id=”form1” runat=”server”>
<asp:DataList ID=”DataList1” Runat=”server” DataSourceID=”XmlDataSource1”> <HeaderTemplate>
<table border=”1” cellpadding=”3”> </HeaderTemplate>
<ItemTemplate>
<tr><td><b><%# XPath(“title”) %></b><br /> <i><%# XPath(“pubDate”) %></i><br />
<%# XPath(“description”) %></td></tr> </ItemTemplate> <AlternatingItemTemplate>
<tr bgcolor=”LightGrey”><td><b><%# XPath(“title”) %></b><br /> <i><%# XPath(“pubDate”) %></i><br />
<%# XPath(“description”) %></td></tr>
112
New Ways to Handle Data
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<asp:XmlDataSource ID=”XmlDataSource1” Runat=”server” DataFile=”http://geekswithblogs.net/evjen/Rss.aspx” XPath=”rss/channel/item”>
</asp:XmlDataSource>
</form>
</body>
</html>
Looking at the code in Listing 4-24, you can see that the DataFile points to a URL where the XML is retrieved. The XPath property filters out all the <item> elements from the RSS feed. The DataList control creates an HTML table and pulls out specific data elements from the RSS feed, such as the <title>,
<pubDate>, and <description> elements.
Running this page in the browser, you get something similar to the results shown in Figure 4-29.
Figure 4-29
113
