none
请教:.NET CF的treeview中我想重复点击node也能触发事件怎么办 RRS feed

答案

  • 你可以试试下面的方案,采用JavaScript。
    <%@ Page Language="C#"  enableEventValidation="false" %>


      <script language="C#" runat="server">
         
          protected void Page_Load(object sender,EventArgs e)
          {
            
              this.TreeView1.Attributes.Add("onclick", "return treeNode(event)");
              Random r=new Random();
              Response.Write(r.Next().ToString());
             
          }    
          protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
          {
              base.RaisePostBackEvent(sourceControl, eventArgument);
              if (sourceControl is TreeView)
              {
                  TreeView tv = (sourceControl as TreeView);
                   if ((tv.ID==this.TreeView1.ID)&&(string.Equals(eventArgument,this.TreeView1.ClientID+"_Update")))
                  {
                    // do your business login
                  }
              }
            
          }
        
         
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head id="Head1" runat="server">
        <title>Populating TreeView Nodes from a SQL Database</title>
        <script type="text/javascript" language="javascript">   
        function treeNode()

    {                     

    __doPostBack('<%=this.TreeView1.ClientID%>','<%=this.TreeView1.ClientID%>'+"_Update");                  

                            return false;
                        }


        </script>
      </head>
      <body>
          <form id="form2" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
          <h2>Populating TreeView Nodes from a SQL Database</h2>

            <asp:TreeView ID="TreeView1" SkinId="Simple" Width="250" ExpandDepth="3"
                runat="server"
                    ontreenodedatabound="TreeView1_TreeNodeDataBound">
                    <Nodes>
                        <asp:TreeNode NavigateUrl="~/AnchorDemo.aspx" Text="AnchorDemo"></asp:TreeNode>
                       <asp:TreeNode NavigateUrl="~/Button.aspx" Text="Button"></asp:TreeNode>
                    </Nodes>
            </asp:TreeView>
          <asp:Label ID="Label1" runat="server"></asp:Label>
           

        </div>
          <aspiteMapDataSource ID="SiteMapDataSource1" runat="server" />
          </form>
      </body>
    </html>

    2009年1月12日 9:53

全部回复

  •     个人感觉是可以实现的

     

     private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                 // TODO: Double Click
                 // if (e.Action == TreeViewAction.ByMouse) { }

            }

     

    查看  TreeViewEventArgs 定义   

     

        // Summary:
        //     Provides data for the System.Windows.Forms.TreeView.AfterCheck, System.Windows.Forms.TreeView.AfterCollapse,
        //     System.Windows.Forms.TreeView.AfterExpand, or System.Windows.Forms.TreeView.AfterSelect
        //     events of a System.Windows.Forms.TreeView control.
        public class TreeViewEventArgs : EventArgs
        {
            // Summary:
            //     Initializes a new instance of the System.Windows.Forms.TreeViewEventArgs
            //     class for the specified tree node and with the specified type of action that
            //     raised the event.
            //
            // Parameters:
            //   node:
            //     The System.Windows.Forms.TreeNode that the event is responding to.
            //
            //   action:
            //     The type of System.Windows.Forms.TreeViewAction that raised the event.
            public TreeViewEventArgs(TreeNode tn, TreeViewAction tva);

            // Summary:
            //     Gets the type of action that raised the event.
            //
            // Returns:
            //     The type of System.Windows.Forms.TreeViewAction that raised the event.
            public TreeViewAction Action { get; }
            //
            // Summary:
            //     Gets the tree node that has been checked, expanded, collapsed, or selected.
            //
            // Returns:
            //     The System.Windows.Forms.TreeNode that has been checked, expanded, collapsed,
            //     or selected.
            public TreeNode Node { get; }
        }

     

     


        // Summary:
        //     Specifies the action that raised a System.Windows.Forms.TreeViewEventArgs
        //     event.
        public enum TreeViewAction
        {
            // Summary:
            //     The action that caused the event is unknown.
            Unknown = 0,
            //
            // Summary:
            //     The event was caused by a keystroke.
            ByKeyboard = 1,
            //
            // Summary:
            //     The event was caused by a mouse operation.
            ByMouse = 2,
            //
            // Summary:
            //     The event was caused by the System.Windows.Forms.TreeNode collapsing.
            Collapse = 3,
            //
            // Summary:
            //     The event was caused by the System.Windows.Forms.TreeNode expanding.
            Expand = 4,
        }

    2009年1月8日 7:23
  • 你可以试试下面的方案,采用JavaScript。
    <%@ Page Language="C#"  enableEventValidation="false" %>


      <script language="C#" runat="server">
         
          protected void Page_Load(object sender,EventArgs e)
          {
            
              this.TreeView1.Attributes.Add("onclick", "return treeNode(event)");
              Random r=new Random();
              Response.Write(r.Next().ToString());
             
          }    
          protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
          {
              base.RaisePostBackEvent(sourceControl, eventArgument);
              if (sourceControl is TreeView)
              {
                  TreeView tv = (sourceControl as TreeView);
                   if ((tv.ID==this.TreeView1.ID)&&(string.Equals(eventArgument,this.TreeView1.ClientID+"_Update")))
                  {
                    // do your business login
                  }
              }
            
          }
        
         
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head id="Head1" runat="server">
        <title>Populating TreeView Nodes from a SQL Database</title>
        <script type="text/javascript" language="javascript">   
        function treeNode()

    {                     

    __doPostBack('<%=this.TreeView1.ClientID%>','<%=this.TreeView1.ClientID%>'+"_Update");                  

                            return false;
                        }


        </script>
      </head>
      <body>
          <form id="form2" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
          <h2>Populating TreeView Nodes from a SQL Database</h2>

            <asp:TreeView ID="TreeView1" SkinId="Simple" Width="250" ExpandDepth="3"
                runat="server"
                    ontreenodedatabound="TreeView1_TreeNodeDataBound">
                    <Nodes>
                        <asp:TreeNode NavigateUrl="~/AnchorDemo.aspx" Text="AnchorDemo"></asp:TreeNode>
                       <asp:TreeNode NavigateUrl="~/Button.aspx" Text="Button"></asp:TreeNode>
                    </Nodes>
            </asp:TreeView>
          <asp:Label ID="Label1" runat="server"></asp:Label>
           

        </div>
          <aspiteMapDataSource ID="SiteMapDataSource1" runat="server" />
          </form>
      </body>
    </html>

    2009年1月12日 9:53
  •    
    1         protected void Treeview1_SelectedNodeChanged(object sender, EventArgs e)  
    2         {  
    3             Treeview1.SelectedNode.ToggleExpandState();  
    4             //每次选择节点事件后都取消节点的Selected 属性
    5             Treeview1.SelectedNode.Selected = false;  
    6         } 
    2009年3月12日 6:23
  • 添加TreeView的Load事件    
    protected void TVMenu_Load(object sender, EventArgs e)
       {
           if (!object.Equals(null, TVMenu.SelectedNode))
           {
               TVMenu.SelectedNode.Select();
               TVMenu.SelectedNode.Selected = false;
           }
       }
    2010年6月18日 3:50