- •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
Site Navigation
As you can see, if you use these built-in styles, it isn’t too difficult to completely change the look and feel of the TreeView control. When this bit of code is run, you get the results shown in Figure 5-10.
Figure 5-10
Examining the parts of the TreeView control
To master working with the TreeView control, you must understand the terminology used for each part of the hierarchical tree that is created by the control.
First, every element or entry in the TreeView control is called a node. The uppermost node in the hierarchy of nodes is the root node. It is possible for a TreeView control to have multiple root nodes. Any node, including the root node, is also considered a parent node if it has any nodes that are directly under it in the hierarchy of nodes. The nodes directly under this parent node are referred to as child nodes. Each parent node can have one or more child nodes. Finally, if a node contains no child nodes, it is referred to as a leaf node.
The following listing, based on the site map shown earlier, details the use of this terminology:
Home – Root node, parent node
News – Parent node, child node
U.S. – |
Child node, leaf node |
World – |
Child node, leaf node |
Technology – Child node, leaf node |
|
Sports |
– Child node, leaf node |
Finance – |
Parent node, child node |
Quotes |
- Child node, leaf node |
Markets |
– Parent node, child node |
U.S. |
Market Report – Child node, leaf node |
NYSE |
– Child node, leaf node |
Funds – |
Child node, leaf node |
Weather – |
Child node, leaf node |
139
Chapter 5
From this listing, you can see what each node is and how it is referred in the hierarchy of nodes. For instance, the U.S. Market Report node is a leaf node — meaning that it doesn’t have any child nodes associated with it. However, it is also a child node to the Markets node, which is a parent node to the U.S. Market Report node. If you are working with the Markets node directly, it is also a child node to the Finance node, which is its parent node. The main point to take away from all this is that each node in the site map hierarchy has a relationship to the other nodes in the hierarchy. You must understand these relationships because you can programmatically work with these nodes (as will be demonstrated later in this chapter) and the methods used for working with them includes terms like RootNode,
CurrentNode and ParentNode.
Binding the TreeView control to an XML file
You are not limited to working with just a .sitemap file in order to populate the nodes of your TreeView controls. You have many ways to get this done. One cool way is to use the XmlDataSource control (instead of the SiteMapDataSource control) to populate your TreeView controls from your XML files.
For an example of this, suppose that you created a hierarchical list of items in an XML file called Hardware.xml. An example of this is shown in Listing 5-8.
Listing 5-8: Hardware.xml
<?xml version=”1.0” encoding=”utf-8”?> <Hardware>
<Item Category=”Motherboards”> <Option Choice=”Asus” /> <Option Choice=”Abit” />
</Item>
<Item Category=”Memory”> <Option Choice=”128mb” /> <Option Choice=”256mb” /> <Option Choice=”512mb” />
</Item>
<Item Category=”HardDrives”> <Option Choice=”40GB” /> <Option Choice=”80GB” /> <Option Choice=”100GB” />
</Item>
<Item Category=”Drives”> <Option Choice=”CD” /> <Option Choice=”DVD” />
<Option Choice=”DVD Burner” /> </Item>
</Hardware>
As you can see, this list is not meant to be used for site navigation purposes, but instead for allowing the end user to make a selection from a hierarchical list of options. This XML file is divided into four categories of available options: motherboards, memory, harddrives, and drives. To bind your TreeView control to this XML file, use an XmlDataSource control that specifies the location of the XML file you are going to use. Then within the TreeView control itself, use the <asp:TreeNodeBinding> element to specify which elements to bind in the XML file to populate the nodes of the TreeView control. This is illustrated in Listing 5-9:
140
Site Navigation
Listing 5-9: Binding a TreeView control to the Hardware.xml file
<%@ Page Language=”VB” %>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>Latest Hardware</title> </head>
<body>
<form id=”form1” runat=”server”>
<asp:TreeView ID=”Treeview1” Runat=”server” DataSourceID=”Xmldatasource1”> <DataBindings>
<asp:TreeNodeBinding DataMember=”Hardware”
Text=”Computer Hardware” />
<asp:TreeNodeBinding DataMember=”Item” TextField=”Category” /> <asp:TreeNodeBinding DataMember=”Option” TextField=”Choice” />
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID=”Xmldatasource1” Runat=”server” DataFile=”Hardware.xml”>
</asp:XmlDataSource>
</form>
</body>
</html>
The first item to look at is the <asp:XmlDataSource> control. It is just as simple as the previous
<asp:SiteMapDataSourceSiteMapDataSource> control, but it points at the Hardware.xml file using the DataFile property.
The next step is to create a TreeView control that binds to this particular XML file. You can bind a default TreeView control directly to the XmlDataSource control like this:
<asp:TreeViewTreeView ID=”TreeView1” Runat=”server”
DataSourceId=”XmlDataSource1” />
Doing this, you get the incorrect result shown in Figure 5-11.
Figure 5-11
141
Chapter 5
As you can see, the TreeView control binds just fine to the Hardware.xml file, but looking at the nodes within the TreeView control, you can see that it is simply displaying the names of the actual XML elements from the file itself. Because this isn’t what you want, you specify how to bind to the XML file with the use of the <DataBindings> element within the TreeView control.
The <DataBindings> element encapsulates one or more TreeNodeBinding objects. Two of the more important available properties of a TreeNodeBinding object are the DataMember and TextField properties. The DataMember property points to the name of the XML element that the TreeView control should look for. The TextField property specifies the XML attribute that the TreeView should look for in that particular XML element. If you do this correctly with the use of the <DataBindings> construct, you get the result shown in Figure 5-12.
Figure 5-12
You can also see from Listing 5-9 that you can override the text value of the root node from the XML file, <Hardware>, and have it appear as Computer Hardware in the TreeView control.
<asp:TreeNodeBinding DataMember=”Hardware” Text=”Computer Hardware” />
Selecting multiple options in a TreeView
As I stated earlier, the TreeView control is not meant to be used primarily for navigation purposes. Instead, you can use it for all sorts of things. In many cases, you can present a hierarchical list from which you want the end user to select one or more items.
One great built-in feature of the TreeView control is the capability to put check boxes next to nodes within the hierarchical items in the list. These boxes allow end users to make multiple selections. The TreeView control contains a property called ShowCheckBoxes, which can be used create check boxes next to many different types of nodes within a list of items.
The available values for the ShowCheckBoxes property are discussed in the following table.
142
|
|
Site Navigation |
|
|
|
|
Value |
Description |
|
|
|
|
All |
Applies check boxes to each and every node within the TreeView control. |
|
Leaf |
Applies check boxes to only the nodes that have no additional child elements. |
|
None |
Applies no check boxes to any node within the TreeView control. |
|
Parent |
Applies check boxes to only the nodes considered parent nodes within the |
|
|
TreeView control. A parent node has at least one child node associated with it. |
|
Root |
Applies a check box to any root node contained within the TreeView control. |
|
|
|
When working with the ShowCheckBoxes property, you can set it declaratively in the control itself:
<asp:TreeView ID=”Treeview1” Runat=”server” Font-Underline=”false” DataSourceID=”Xmldatasource1” ShowCheckBoxes=”leaf”>
...
</asp:TreeViewTreeView>
Or you can set it programmatically by using the following code:
VB
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf
C#
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
For an example of using check boxes with the TreeView control, let’s continue to expand on the computer hardware example from Listing 5-9. Create a hierarchical list that enables people to select multiple items from the list in order to receive additional information about the selected items. Listing 5-10 shows an example of this.
Listing 5-10: Applying check boxes next to the leaf nodes within the hierarchical list of nodes
VB
<%@ Page Language=”VB” %>
<script runat=”server”>
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If TreeView1.CheckedNodes.Count > 0 Then
Label1.Text = “We are sending you information on:<p>”
For Each node As TreeNode In TreeView1.CheckedNodes Label1.Text += node.Text & “ “ & node.Parent.Text & “<br>”
Next
Else
Label1.Text = “You didn’t select anything. Sorry!” End If
End Sub </script>
(continued)
143
Chapter 5
Listing 5-10: (continued)
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>Latest Hardware</title> </head>
<body>
<form runat=”server”>
Please select the items you are interested in: <p>
<asp:TreeView ID=”Treeview1” Runat=”server” Font-Underline=”false” DataSourceID=”Xmldatasource1” ShowCheckBoxes=”leaf”>
<DataBindings>
<asp:TreeNodeBinding DataMember=”Hardware” Text=”Computer Hardware” />
<asp:TreeNodeBinding DataMember=”Item” TextField=”Category” /> <asp:TreeNodeBinding DataMember=”Option” TextField=”Choice” />
</DataBindings>
</asp:TreeView>
<p>
<asp:ButtonButton ID=”Button1” Runat=”server” Text=”Submit Choices” OnClick=”Button1_Click” />
</p>
<asp:XmlDataSource ID=”XmlDataSource1” Runat=”server” DataFile=”Hardware.xml”>
</asp:XmlDataSource>
</p>
<asp:Label ID=”Label1” Runat=”Server” /> </form>
</body>
</html>
C#
<%@ Page Language=”C#” %>
<script runat=”server”>
void Button1_Click(object sender, System.EventArgs e)
{
if (TreeView1.CheckedNodes.Count > 0)
{
Label1.Text = “We are sending you information on:<p>”; foreach (TreeNode node in TreeView1.CheckedNodes)
{
Label1.Text += node.Text + “ “ + node.Parent.Text + “<br>”;
}
}
else
{
Label1.Text = “You didn’t select anything. Sorry!”;
}
}
</script>
144
