Asp.net 2.0 中的TreeView的右鍵菜單

來源:互聯網
上載者:User

轉:http://hi.baidu.com/wqeast/blog/item/c085db2a5907cc9b033bf6b9.html

A few ASP.NET 2.0 TreeView coders were asking for a how to have context menus - per node - on the TreeView control. Well I spent a small amount of time on this and wanted to post one solution. A few caveats:

a. IE Specific
b. I never optimized the javascript to use parameters

Hope this helps soeone achieve what they need. If you find a better solution please feel free to let me know!

 

 

<%@ Page Language="C#" %>

<html>
<head>
    <style>
    <!--
        .skin1 {
            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>

    <script type="text/javascript">

<!-- Begin
var menuskin = "skin1"; 
var display_url = 1; // Show URLs in status bar?
function showmenuie5() {
    if (event.srcElement.type != undefined)
    {
            var s  = event.srcElement.id;
            var ind = s.replace("TreeView1t", "");
            s = s.replace("TreeView1t", "TreeView1n"); 

            document.getElementById('one').url = event.srcElement.href;
            document.getElementById('one').innerText = "Select";
            var ch = document.getElementById(s);
            if (ch != null){
                document.getElementById('two').url = "javascript:TreeView_ToggleNode(TreeView1_Data," + ind + "," + s + ",' '," + s + "Nodes)";    
                document.getElementById('two').innerText = "Expand//Collapse";
            }
            else        
                document.getElementById('two').outerHTML="<div id=two class=menuitems url=''></div>"

            var rightedge = document.body.clientWidth-event.clientX;
            var bottomedge = document.body.clientHeight-event.clientY;
            if (rightedge < ie5menu.offsetWidth)
                ie5menu.style.left = document.body.scrollLeft + event.clientX - ie5menu.offsetWidth;
            else
                ie5menu.style.left = document.body.scrollLeft + event.clientX;
            if (bottomedge < ie5menu.offsetHeight)
                ie5menu.style.top = document.body.scrollTop + event.clientY - ie5menu.offsetHeight;
            else
                ie5menu.style.top = document.body.scrollTop + event.clientY;
                ie5menu.style.visibility = "visible";
                }
                else
                {
                   ie5menu.style.visibility = "hidden";
                }
            return false;
    }
    function hidemenuie5() {
        ie5menu.style.visibility = "hidden";
    }
    function highlightie5() {
        if (event.srcElement.className == "menuitems") {
            event.srcElement.style.backgroundColor = "highlight";
            event.srcElement.style.color = "white";
        if (display_url)
            window.status = event.srcElement.url;
        }
    }
    function lowlightie5() {
        if (event.srcElement.className == "menuitems") {
            event.srcElement.style.backgroundColor = "";
            event.srcElement.style.color = "black";
            window.status = "";
        }
    }
    function jumptoie5() {
        if (event.srcElement.className == "menuitems") {
            if (event.srcElement.getAttribute("target") != null)
                window.open(event.srcElement.url, event.srcElement.getAttribute("target"));
            else
                window.location = event.srcElement.url;
        }
    }
//  End -->
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div id="ie5menu" class="skin0" onmouseover="highlightie5()" onmouseout="lowlightie5()"
            onclick="jumptoie5();">
            <div id="one" class="menuitems" url="">
            </div>
            <div id="two" class="menuitems" url="">
            </div>
        </div>
        <asp:TreeView ID="TreeView1" runat="server">
            <Nodes>
                <asp:TreeNode Text="a" Value="a">
                    <asp:TreeNode Text="b" Value="b">
                        <asp:TreeNode Text="c" Value="c"></asp:TreeNode>
                    </asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="cc" Value="cc">
                    <asp:TreeNode Text="ccc" Value="ccc">
                        <asp:TreeNode Text="cccc" Value="cccc"></asp:TreeNode>
                    </asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
            <SelectedNodeStyle BackColor="#C00000" />
        </asp:TreeView>
    </form>

    <script type="text/javascript">
      if (document.all) {
          ie5menu.className = menuskin;
          document.getElementById('TreeView1').oncontextmenu = showmenuie5;
          document.body.onclick = hidemenuie5;
      }
    </script>
</body>
</html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.