<%@ Register TagPrefix="ec" Namespace="ASPNETExpert.WebControls" Assembly="ASPNETExpert.WebControls" %>
<%@ Register TagPrefix="ecd" Namespace="ASPNETExpert.WebControls.DemoControls" Assembly="ASPNETExpert.WebControls.DemoControls" %>
<%@ Control Language="c#" Inherits="ASPNETExpert.WebControls.Demo.Tree.Advanced.ContextMenu" CodeFile="ContextMenu.ascx.cs" %>
<script language="javascript">
function OnRemoveHandler(menuItem, contextParam, treeNode) {
if(treeNode) {
if(menuItem.Value == 'Remove')
return confirm('The node ' + treeNode.Text + ' will be removed. Are you sure?');
if(menuItem.Value == 'RemoveChildren')
return confirm('All children of the node ' + treeNode.Text + ' will be removed. Are you sure?');
}
else
alert('No node!');
return true;
}
function MenuItemClicked(menuItem, contextParam, treeNode) {
if(!treeNode)
return;
if(menuItem.Value == 'RemoveOnClient') {
var parentNode = treeNode.GetParentNode();
treeNode.Remove();
parentNode.Update();
}
}
</script>
<table cellpadding="0" cellspacing="0" border="0" xmlns:ec="urn:http://aspnetexpert.com/ExpertControls.xsd">
<tr>
<td valign=top>
<ec:ExpertTree id="ContextMenuTree" runat="server" Skin="Classic" ContextMenuID="BuildTreeContextMenu" SelectOnClick="false" ExpandOnClick="true" CollapseOnClick="true">
<TreeLook Base="Classic" Width="250px" ShowLines="True" />
<Nodes>
<ec:TreeNode Text="Root 0" />
</Nodes>
</ec:ExpertTree>
<br>
<ec:ExpertTree id="ContextMenuTree2" runat="server" Skin="Classic" ContextMenuID="BuildTreeContextMenu" SelectOnClick="false" ExpandOnClick="true" CollapseOnClick="true">
<TreeLook Base="Classic" Width="250px" ShowLines="True" />
<Nodes>
<ec:TreeNode Text="Root 0" />
</Nodes>
</ec:ExpertTree>
</td>
<td valign=top width="100%" style="PADDING-LEFT:10px">
<ecd:DescriptionView runat="server" ID="Descriptionview1">
<ecd:Description id="Description1" runat="server">
Rigth-click on a node to pop up the context menu. </ecd:Description>
<ecd:Description id="Description2" runat="server">
Use power and flexibility of <b>ExpertContextMenu</b> control to implement
a context menu for any of the <b>ExpertTree</b> nodes.
</ecd:Description>
<ecd:Description id="Description3" runat="server">
Design and tune context menu(s) with <b>ExpertContextMenu</b> control and link it
to all tree nodes or any of the nodes by specifying <b>ContextMenu</b> property for the
tree or for an individual node.
</ecd:Description>
<ecd:Description id="Description4" runat="server">
Raise PostBack when clicking a menu item by setting <b>PostBackOnClick</b> property for
a menu and process <b>ContextMenuItemSelected</b> tree's event at the server side.
</ecd:Description>
<ecd:Description id="Description5" runat="server">
Process menu's events at the client side using client-side API.
</ecd:Description>
</ecd:DescriptionView>
</td>
</tr>
</table>
<div xmlns:ec="urn:http://aspnetexpert.com/ExpertControls.xsd">
<ec:ExpertContextMenu id="BuildTreeContextMenu" runat="server" Skin="DarkBlue" EnableViewState="false" PostBackOnClick="true">
<OnClientBefore PostBackHandler="OnRemoveHandler" />
<OnClientAfter ItemClickHandler="MenuItemClicked" />
<TopGroup>
<Look Orientation="Vertical" Expand-Effect="GlideTopLeftToBottomRight" Expand-Duration="300" Collapse-Effect="GlideBottomRightToTopLeft" Collapse-Duration="300" />
<Items>
<ec:MenuItem Value="NewSibling" Text="New Sibling" />
<ec:MenuItem Value="NewChild" Text="New Child" />
<ec:MenuItem Value="Remove" Text="Remove" />
<ec:MenuItem Value="RemoveOnClient" Text="Remove on client side" PostBackOnClick="False" />
</Items>
</TopGroup>
</ec:ExpertContextMenu>
<ec:ExpertContextMenu id="BuildTreeExtContextMenu" runat="server" Skin="DarkBlue" EnableViewState="false" PostBackOnClick="true">
<OnClientBefore PostBackHandler="OnRemoveHandler" />
<OnClientAfter ItemClickHandler="MenuItemClicked" />
<TopGroup>
<Look Orientation="Vertical" Expand-Effect="GlideTopLeftToBottomRight" Expand-Duration="300" Collapse-Effect="GlideBottomRightToTopLeft" Collapse-Duration="300" />
<Items>
<ec:MenuItem Value="NewSibling" Text="New Sibling" />
<ec:MenuItem Value="NewChild" Text="New Child" />
<ec:MenuItem Value="RemoveChildren" Text="Remove Children" />
<ec:MenuItem Value="Remove" Text="Remove" />
<ec:MenuItem Value="RemoveOnClient" Text="Remove on client side" PostBackOnClick="False" />
</Items>
</TopGroup>
</ec:ExpertContextMenu>
</div>
<ec:CodeViewTab id="CodeViewTab1" runat="server">
<ec:TabItem runat="server" Text="aspx" ID="Tabitem1">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="ASPX" OutputFile="Advanced/ContextMenu.ascx" ID="Syntaxhighlight1"/>
</pre>
</ec:TabItem>
<ec:TabItem runat="server" Text="C#" ID="Tabitem2">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="C#" OutputFile="Advanced/ContextMenu.ascx.cs" ID="Syntaxhighlight2"/>
</pre>
</ec:TabItem>
<ec:TabItem runat="server" Text="VB" ID="Tabitem3">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="VB" OutputFile="Advanced/ContextMenu.ascx.vb" ID="Syntaxhighlight3"/>
</pre>
</ec:TabItem>
</ec:CodeViewTab>
namespace ASPNETExpert.WebControls.Demo.Tree.Advanced
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ASPNETExpert.WebControls;
///
/// Summary description for ContextMenu.
///
public partial class ContextMenu : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.ContextMenuTree.ContextMenuItemSelected += new ASPNETExpert.WebControls.ContextMenuItemSelectedEventHandler(this.ContextMenuTree_ContextMenuItemSelected);
this.ContextMenuTree2.ContextMenuItemSelected += new ASPNETExpert.WebControls.ContextMenuItemSelectedEventHandler(this.ContextMenuTree_ContextMenuItemSelected);
}
#endregion
private void ContextMenuTree_ContextMenuItemSelected(object sender, ASPNETExpert.WebControls.ContextMenuItemSelectedEventArgs eventArgs)
{
if(eventArgs.Node == null)
return;
if(eventArgs.Item.Value == "NewSibling")
{
if(eventArgs.Node.Parent == null)
AddNewNode(eventArgs.Node.ParentTree.Nodes, "Root");
else
AddNewNode(eventArgs.Node.Parent.Nodes, "Node");
}
else
if(eventArgs.Item.Value == "NewChild")
{
AddNewNode(eventArgs.Node.Nodes, "Node");
eventArgs.Node.Expand();
eventArgs.Node.ContextMenuID = "BuildTreeExtContextMenu";
}
else
if(eventArgs.Item.Value == "RemoveChildren")
{
eventArgs.Node.Nodes.Clear();
eventArgs.Node.ContextMenuID = "BuildTreeContextMenu";
}
else
if(eventArgs.Item.Value == "Remove")
{
ASPNETExpert.WebControls.TreeNode parent = eventArgs.Node.Parent;
eventArgs.Node.Remove();
if(parent != null && parent.Nodes.Count == 0)
{
parent.ContextMenuID = "BuildTreeContextMenu";
parent.BindLook();
}
}
eventArgs.Node.BindLook();
}
private void AddNewNode(ASPNETExpert.WebControls.TreeNodeCollection treeNodeCollection, string text)
{
ASPNETExpert.WebControls.TreeNode treeNode = new ASPNETExpert.WebControls.TreeNode(text + " " + treeNodeCollection.Count.ToString());
treeNodeCollection.Add(treeNode);
treeNode.BindLook();
}
}
}