Apply to
Menu, SlideMenu, TabStrip and TreeView.
For ASP.NET 2.0 only
Overview
ASP.NET 2.0 defines a new set of data source controls to support declarative
data binding. EO.Web navigation controls can bind to any data source controls
that inherit from HierarchicalDataSourceControl
class. The most commonly used hierarchical data source controls are
XmlDataSource and
SiteMapDataSource. To perform the binding, simply set EO.Web controls'
DataSourceID to the ID of the supported data source control. No extra
code is required since declarative data binding is automatic.
Mapping data source attribute to item's property
When binding to XmlDataSource or SiteMapDataSource,
several XML attributes are automatically mapped to certain property of the
corresponding item:
For example, when binding to the following XML code via XmlDataSource:
<xml version="1.0" encoding="utf-8" ?>
<items>
<category title="Hardware">
<category title="Desktop" url="~/desktop.aspx">
<category title="Complete Systems" url="~/systems.aspx" />
<category title="Barebone Systems" url="~/barebones.aspx" />
<category title="Accessories" url="~/accessories.aspx" />
</category>
<category title="Notebooks" url="~/notebook.aspx" />
</category>
<category title="Software">
<product title="Windows" url="~/windows.aspx" />
<product title="Office" url="~/office.aspx" />
</category>
</items>
EO.Web controls create an item for each XML element and take the value of the title
attribute of each element as item text and the value of the url attribute
of each element as the target Url:
Note: When binding to XmlDataSource, you
should set the XPath
property of the XmlDataSource control to "/*/*"
if you want to skip the root node.
<asp:XmlDataSource ID="XmlDataSource1"
runat="server" DataFile="~/test.xml" XPath="/*/*">
</asp:XmlDataSource>
Using DataBinding object
You can also use DataBinding object to
explicitly define the mapping. When using DataBinding with XML
data, the DataField property
should specify a valid XPath expression instead of the data field name. For
example, the following code defines a DataBinding object that
maps the title attribute to item's
Value property:
<eo:Menu Runat="server" DataSourceID="XmlDataSource1">
<TopGroup>
<Bindings>
<eo:DataBinding Property="Value" DataField="@title">
</eo:DataBinding>
</Bindings>
</TopGroup>
</eo:Menu>
The following code concatenates the title and price as navigation item text:
<eo:Menu Runat="server" DataSourceID="XmlDataSource1">
<TopGroup>
<Bindings>
<eo:DataBinding
Property="Text-Html"
DataField="concat(@title, ' - ', @price)">
</eo:DataBinding>
</Bindings>
</TopGroup>
</eo:Menu>
EO.Web navigatin controls evaluate the XPath expression the same way as
xsl:value-of XSLT element. For more information about XPath expression
syntax, please refer to MSDN Library.
You can use DataBinding object to explicitly override the
automatic mappings. The easiest way to define DataBinding object
is to use Control Builder. You can define Databinding on both the control
or on a specific NavigationItemGroup
and add the DataBinding into the control's
Bindings or the group's Bindings
property.
Built-in role Support
When binding with XmlDataSource or SiteMapDataSource, EO.Web
controls automatically populate AllowRoles
from the roles attribute. For example, if the control is bound to the
following sitemap file:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode url="Default.aspx" title="Default" description="">
<siteMapNode url="Page1.aspx" title="Page 1" description="" roles="role1"/>
<siteMapNode url="Page2.aspx" title="Page 2" description="" roles="role2"/>
<siteMapNode url="Page3.aspx" title="Page 3" description=""/>
</siteMapNode>
</siteMap>
Item "Page 1" will have an AllowRoles value of "role1", item "Page 2"
will have an AllowRoles value of "role2" and item "Page 3" will have an
empty AllowRoles value.
Depending on the value of
HideRoleDisabledItems, certain items will be disabled or hidden based
on the role of the current user. In the above example, if the current user
belongs to "role1" but not "role2", then item "Page 2" will be disabled or
hidden, but both item "Page 1" and "Page 3" will be enabled - Item "Page 1" is
enabled because its AllowRoles property includes the current user, item "Page
3" is enabled because its AllowRoles property is empty.
Role support security
Unlike the standard ASP.NET 2.0 navigation controls, EO.Web navigation controls
do not require you to include any <authorization> elements in your
web.config. While this is a great convenience, you may still need to consider
including <authorization> in your web.config for security reasons. In the
above example, even if item "Page 2" is disabled or hidden by the control,
without proper <authorization> elements in the web.config file, user will
still be able to access Page2.aspx by directly typing "Page2.aspx" in their
browser's address bar thus completely bypassing the control.
If you do not wish to maintain <authorization> elements in web.config
file, you can handle Application_AuthorizeRequest events in your Global class
or Global.asax:
[C#]
protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
SiteMapNode node = SiteMap.CurrentNode;
//Let it pass if there is no corresponding siteMapNode
//or the node has no roles associated with it
if ((node == null) || (node.Roles == null) || (node.Roles.Count == 0))
return;
//Let it pass if the current user belongs to the node's role
HttpContext context = HttpContext.Current;
foreach (string role in node.Roles)
{
if ((role == "*") ||
((context.User != null) && context.User.IsInRole(role)))
return;
}
throw new Exception("Restricted");
}
[Visual Basic]
Protected Sub Application_AuthorizeRequest( _
ByVal sender As Object, ByVal e As System.EventArgs)
Dim node As SiteMapNode = SiteMap.CurrentNode
'Let it pass if there is no corresponding siteMapNode
'or the node has no roles associated with it
If ((node Is Nothing) Or (node.Roles Is Nothing) Or (node.Roles.Count = 0)) Then
Return
End If
'Let it pass if the current user belongs to the node's role
Dim context As HttpContext = HttpContext.Current
For Each role As String In node.Roles
If (role = "*") Or _
((Not (context.User Is Nothing)) And context.User.IsInRole(role)) Then
Return
End If
Next
Throw New Exception("Restricted")
End Sub