ItemCommand event ?

8 posts, 0 answers
  1. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 17 Jul 2009 Link to this post

    Hi,

    I am building my treeview in codebehind, with serverside callback. Some nodes contains ButtonLink with CommandName and CommandArgument filled (to allow richer node interaction).

    Is there a way to create a "ItemCommand" event to handle the click on those ButtonLink ?

    Thanks.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Jul 2009 Link to this post

    Hi Damien,

    I am not sure about whether it is possible to add ItemCommand event for RadTreeView. One suggestion to achieve same functionality, would be adding common buttonclick event and check for the CommandName and get the ComandArguments in that event. You can also get access to the particular node which is clicked by using NamingContainer property of button. Here is the example.

    C#:
     
    protected void Page_Load(object sender, EventArgs e) 
        RadTreeView tree = new RadTreeView(); 
     
        RadTreeNode node1 = new RadTreeNode("Node1"); 
        RadTreeNode node2 = new RadTreeNode("Node2"); 
        RadTreeNode node3 = new RadTreeNode("Node3"); 
     
        Button btn1 = new Button(); 
        btn1.Text = "Click me 1"
        btn1.CommandName = "CommandName1"// Setting CommandName 
        btn1.Click += new EventHandler(btn1_Click); 
     
        Button btn2 = new Button(); 
        btn2.Text = "Click me 2"
        btn2.CommandName = "CommandName2"// Setting CommandName 
        btn2.Click += new EventHandler(btn1_Click); 
     
        node1.Controls.Add(btn1); 
        node2.Controls.Add(btn2); 
     
         
        node1.Nodes.Add(node2); 
        node1.Nodes.Add(node3); 
     
        tree.Nodes.Add(node1); 
        this.form1.Controls.Add(tree); 
    //Common Buttonclick event 
    void btn1_Click(object sender, EventArgs e) 
        Button btn = (Button)sender; 
        Response.Write(btn.CommandName.ToString());  // Get the CommandName 
        RadTreeNode clickedNode = (RadTreeNode)btn.NamingContainer; // Get the Clicked node 
        Response.Write(clickedNode.Text); 
    Hope this helps.

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 20 Jul 2009 Link to this post

    Your sample is working, but in my real case, I add nodes programmatically with serverside callback (node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack). Filling is done in responde to a button click.

    The strange behavior I have is when I click the button, a postback is done, but the event is not triggered, root nodes are cleared (no label, or any of the controls I have put) but are still here, child only shows label I haev added.

    I will try to reproduce this behavior in a simple control.
  5. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 20 Jul 2009 Link to this post

    Page form :
            <telerik:RadScriptManager ID="sm" runat="server" /> 
            <asp:Button ID="btnPopulate" runat="server" Text="Populate"  
                OnClick="btnPopulate_Click" /> 
            <telerik:RadTreeView ID="tree" runat="server" onnodeexpand="tree_NodeExpand" /> 
     

    Code behind :
            protected void btnPopulate_Click(object sender, EventArgs e) 
            { 
                this.tree.Nodes.Clear(); 
                for (int i = 0; i < 10; i++) 
                { 
                    this.AppendNode(this.tree.Nodes, i); 
                } 
            } 
     
            protected void tree_NodeExpand(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e) 
            { 
                for (int i = 0; i < 10; i++) 
                { 
                    this.AppendNode(e.Node.Nodes, i); 
                } 
            } 
     
            private void AppendNode(Telerik.Web.UI.RadTreeNodeCollection collection, int i) 
            { 
                Telerik.Web.UI.RadTreeNode node = new Telerik.Web.UI.RadTreeNode(); 
                Label lbl = new Label(); 
                lbl.ID = "lbl"
                lbl.Text = "Node " + i; 
                node.Controls.Add(lbl); 
                Button btn = new Button(); 
                btn.ID = "btn"
                btn.Text = "Click " + i; 
                btn.CommandName="BtnClick"
                btn.CommandArgument=i.ToString(); 
                btn.Click += this.btn_Click; 
                btn.Command += this.btn_Command; 
                node.Controls.Add(btn); 
     
                node.ExpandMode = Telerik.Web.UI.TreeNodeExpandMode.ServerSideCallBack; 
     
                collection.Add(node); 
            } 
     
            protected void btn_Click(object sender, EventArgs e) 
            { 
                Response.Write("btn_Click:" + sender.ToString()); 
            } 
     
            protected void btn_Command(object sender, CommandEventArgs e) 
            { 
                Response.Write("btn_Command:" + e.CommandArgument); 
            } 

    This code reproduce the behavior I have. I'm going to make more tests to see if I can find a workaround.
  6. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 20 Jul 2009 Link to this post

    With :
    node.ExpandMode = Telerik.Web.UI.TreeNodeExpandMode.ServerSide;

    All nodes that are not in the expanding child collection are cleared of their controls. The behavior is like previous, but extanded to each nodes, not only root ones.



  7. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 21 Jul 2009 Link to this post

    I think this buggy behavior is due to the fact that controls are created server-side but not in page_load event. It seams that controls dynamically added to nodes are not persisted accross postbacks, so server can't map event to handler. And when rendered, nodes are still shown on page, but without data and template (empty nodes).

    I have tried a workaround by creating my custom HierarchicalDataSource. Template is rendered as expected, button has its events correctly handled, but it is not a solution. Do you have any idea to handle that case ?

    Thanks.
  8. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 22 Jul 2009 Link to this post

    Hi Damien,

    Indeed dynamically added controls in the tree node's collection are not persisted after postback. This behavior is by design as RadTreeView does not know what type of controls may be inserted in it and how to initialize them after postback. This behavior is the same as in placeholders or panel controls - the developer is responsible to instantiate the controls on every postback. Here is some sample code:

    public void Page_Load(object sender, EventArgs e) 
        if (!Page.IsPostBack) 
        { 
            //RadTreeView persists its nodes between postbacks 
            RadTreeView1.Nodes.Add(new RadTreeNode()); 
        } 
         
        TextBox textBox = new TextBox(); 
        textBox.ID = "TextBox1"
        RadTreeView1.Nodes[0].Controls.Add(textBox); 
        if (!Page.IsPostBack) 
        { 
            //ViewState of custom controls is persisted by RadTreeView.  
            textBox.Text="Text"
        } 

    Best wishes,
    Albert
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Dédé
    Dédé avatar
    60 posts
    Member since:
    May 2009

    Posted 22 Jul 2009 Link to this post

    Hi Albert,

    I'm going to discuss about my initial problem in this thread since both problems are related to same behavior.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017