The following code example shows how to extend the HierarchicalDataBoundControlAdapter class to render a hierarchical TreeView control that is bound to an XmlDataSource object.
This code example contains four objects:
An adapter derived from the HierarchicalDataBoundControlAdapter class.
The .aspx file that incorporates the TreeView control and device-specific content.
A browser file to link the adapter to a device type.
An XML file that contains a hierarchical list of employees.
This code example consists of four code segments. The first code segment demonstrates how to extend the HierarchicalDataBoundControlAdapter class.
Imports System
Imports System.Web
Imports System.Security.Permissions
Namespace Contoso
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class HierarchicalTreeViewAdapter
Inherits _
System.Web.UI.WebControls.Adapters.HierarchicalDataBoundControlAdapter
' Return a strongly-typed TreeView control for adapter.
Protected Overloads ReadOnly Property Control() As _
System.Web.UI.WebControls.TreeView
Get
Return CType( _
MyBase.Control, _
System.Web.UI.WebControls.TreeView)
End Get
End Property
' Verify the DataSourceID property is set prior to binding data.
Protected Overrides Sub PerformDataBinding()
If (Not Control.DataSourceID Is Nothing) Then
MyBase.PerformDataBinding()
End If
End Sub
End Class
End Namespace
using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
namespace Contoso
{
[AspNetHostingPermission(
SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(
SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class HierarchicalTreeViewAdapter :
System.Web.UI.WebControls.Adapters.HierarchicalDataBoundControlAdapter
{
// Return a strongly-typed TreeView control for adapter.
protected new System.Web.UI.WebControls.TreeView Control
{
get
{
return (System.Web.UI.WebControls.TreeView)base.Control;
}
}
// Verify the DataSourceID property is set prior to binding data.
protected override void PerformDataBinding()
{
if (Control.DataSourceID != null)
{
base.PerformDataBinding();
}
}
}
}
The second code segment demonstrates how to declare a TreeView to bind to an XML data source.
<%@ page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>HierarchicalDataBoundControl Adapter</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:TreeView ID="TreeView1"
Runat="server"
DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding
DataMember="employees" Text="Employees"/>
<asp:TreeNodeBinding
DataMember="employee" TextField="id" />
<asp:TreeNodeBinding
DataMember="name" TextField="fullname" />
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1"
Runat="server"
DataFile="employees.xml" />
<br />
</form>
</body>
</html>
<%@ page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>HierarchicalDataBoundControl Adapter</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:TreeView ID="TreeView1"
Runat="server"
DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding
DataMember="employees" Text="Employees"/>
<asp:TreeNodeBinding
DataMember="employee" TextField="id" />
<asp:TreeNodeBinding
DataMember="name" TextField="fullname" />
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1"
Runat="server"
DataFile="employees.xml" />
<br />
</form>
</body>
</html>
The third code segment shows how to link the TreeView control to the custom adapter for browsers running on Windows CE.
<browsers>
<browser refID="WinCE">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.TreeView"
adapterType="Contoso.HierarchicalTreeViewAdapter" />
</controlAdapters>
</browser>
<browser refID="IE">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.TreeView"
adapterType="Contoso.HierarchicalTreeViewAdapter" />
</controlAdapters>
</browser>
</browsers>
The final code segment provides the XML data that the TreeView control binds to.