none
如何在asp.net中实现 TreeView右键菜单 RRS feed

答案

  • 你好:

    你可以尝试使用oncontextmenu来显示一个菜单层。

    <%@ 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>
    

    其它详情请参考:

    http://www.cnblogs.com/hxling/archive/2010/10/21/1857223.html
    http://www.cnblogs.com/blodfox777/archive/2008/08/29/1279675.html
    http://www.cnblogs.com/wayne-ivan/archive/2009/03/26/1422300.html
    http://www.cnblogs.com/kfcccer/archive/2007/05/20/753533.html

    希望对你有所帮助。


    I am here!
    2010年12月21日 6:08
    版主

全部回复

  • ????????
    学习,学习,再学习..........
    2010年12月20日 3:04
  • 这个要自己写,asp。net控件没有这样的功能。或者找一些js的 TreeView
    【孟子E章】
    2010年12月20日 9:41
    版主
  • 你好:

    你可以尝试使用oncontextmenu来显示一个菜单层。

    <%@ 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>
    

    其它详情请参考:

    http://www.cnblogs.com/hxling/archive/2010/10/21/1857223.html
    http://www.cnblogs.com/blodfox777/archive/2008/08/29/1279675.html
    http://www.cnblogs.com/wayne-ivan/archive/2009/03/26/1422300.html
    http://www.cnblogs.com/kfcccer/archive/2007/05/20/753533.html

    希望对你有所帮助。


    I am here!
    2010年12月21日 6:08
    版主
  • 请问用这个方法实现右键菜单后,在右键菜单中如何获得treenode的值,我是新手。。。。
    2010年12月29日 6:42