一个iewebcontrol treeview右键菜单的例子,实现了添加、修改和删除功能,复制即可运行
更多treeview的客户端操作参见
http://www.csdn.net/develop/read_article.asp?id=22100
<%@ register tagprefix="iewc" namespace="microsoft.web.ui.webcontrols" assembly="microsoft.web.ui.webcontrols, version=1.0.2.226, culture=neutral, publickeytoken=31bf3856ad364e35" %>
<html>
<head>
<title>treeview控件右键菜单</title>
<style>
<!--
.skin
{
cursor:default;
font:menutext;
position:absolute;
text-align:left;
font-family: arial, helvetica, sans-serif;
font-size: 10pt;
width:120px;
background-color:menu;
border:1 solid buttonface;
visibility:hidden;
border:2 outset buttonhighlight;
}
.menuitems
{
padding-left:15px;
padding-right:10px;
}
-->
</style>
</head>
<body onclick="hidemenu()">
<form id="treeview" method="post" runat="server">
<iewc:treeview id="treeview1" runat="server" expandlevel="3" hoverstyle="color:blue;background:#ffff00;">
<iewc:treenode text="node0" expanded="true">
<iewc:treenode text="node3">
<iewc:treenode text="node5"></iewc:treenode>
<iewc:treenode text="node6"></iewc:treenode>
</iewc:treenode>
<iewc:treenode text="node4"></iewc:treenode>
</iewc:treenode>
<iewc:treenode text="node1" expanded="true">
<iewc:treenode text="node7">
<iewc:treenode text="node8"></iewc:treenode>
</iewc:treenode>
</iewc:treenode>
<iewc:treenode text="node2" expanded="true">
<iewc:treenode text="node9"></iewc:treenode>
<iewc:treenode text="node10">
<iewc:treenode text="node11"></iewc:treenode>
<iewc:treenode text="node12"></iewc:treenode>
</iewc:treenode>
</iewc:treenode>
</iewc:treeview>
<div id="popupmenu" class="skin" onmouseover="highlighitem()" onmouseout="lowlightitem()" onclick="clickitem()">
<div class="menuitems" func="add">添加</div>
<hr>
<div class="menuitems" func="delete">删除</div>
<div class="menuitems" func="modify">修改</div>
</div>
</form>
<script language="javascript">
var menuskin = "skin";
var node = null;
function hidemenu()
{
popupmenu.style.visibility = "hidden";
}
function highlighitem()
{
if (event.srcelement.classname == "menuitems")
{
event.srcelement.style.backgroundcolor = "highlight";
event.srcelement.style.color = "white";
}
}
function lowlightitem()
{
if (event.srcelement.classname == "menuitems")
{
event.srcelement.style.backgroundcolor = "";
event.srcelement.style.color = "black";
window.status = "";
}
}
function clickitem()
{
if (event.srcelement.classname == "menuitems")
{
if(event.srcelement.getattribute("func") == "add" && node != null)
{
var newnode=treeview1.createtreenode();
newnode.setattribute("text","new node");
node.add(newnode);
}
else if (event.srcelement.getattribute("func") == "delete" && node != null)
{
node.remove();
}
else if (event.srcelement.getattribute("func") == "modify" && node != null)
{
node.setattribute("text","hgknight");
}
}
}
function treeview1.oncontextmenu()
{
var nodeindex = event.treenodeindex;
if (typeof(nodeindex) == "undefined")
{
node = null;
return;
}
node = treeview1.gettreenode(nodeindex);
var rightedge = document.body.clientwidth-event.clientx;
var bottomedge = document.body.clientheight-event.clienty;
if (rightedge <popupmenu.offsetwidth)
{
popupmenu.style.left = document.body.scrollleft + event.clientx - popupmenu.offsetwidth;
}
else
{
popupmenu.style.left = document.body.scrollleft + event.clientx;
}
if (bottomedge <popupmenu.offsetheight)
{
popupmenu.style.top = document.body.scrolltop + event.clienty - popupmenu.offsetheight;
}
else
{
popupmenu.style.top = document.body.scrolltop + event.clienty;
}
popupmenu.style.visibility = "visible";
return false;
}
</script>
</body>
</html>
新闻热点
疑难解答