RadTreeView - Events inside node template

2 posts, 0 answers
  1. Gergely
    Gergely avatar
    1 posts
    Member since:
    May 2013

    Posted 03 May 2013 Link to this post

    Hy

    I would like to use custom templates for the treenodes with two linkbutton. 
    How can i use events for this buttons? 
    I tried this, but it is not working:
    protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)
    {
    DataRowView nodeData = (DataRowView)e.Node.DataItem;
    LinkButton rovat = (LinkButton)e.Node.FindControl("r");
    rovat.Click += new EventHandler(LinkButton_Click);
    e.Node.ToolTip = nodeData["folder"].ToString();
    }
    protected void LinkButton_Click(Object sender, EventArgs e)
      {    }

       
    My template class:
    class NodeTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            LinkButton linkR = new LinkButton();
            linkR.ID = "r";
            linkR.Text = "";
            linkR.CssClass = "r";
            linkR.DataBinding += new EventHandler(hypR_DataBinding);
            container.Controls.Add(linkR);
     
            LinkButton linkC = new LinkButton();
            linkC.ID = "c";
            linkC.Text = "c";
            linkC.CssClass = "c";
            linkC.DataBinding += new EventHandler(hypC_DataBinding);
            container.Controls.Add(linkC);
        }
     
        private void hypR_DataBinding(object sender, EventArgs e)
        {
            LinkButton target = (LinkButton)sender;
            RadTreeNode node = (RadTreeNode)target.BindingContainer;
            DataRowView nodeData = (DataRowView)node.DataItem;
            target.Text = nodeData["title"].ToString();
        }
     
        private void hypC_DataBinding(object sender, EventArgs e)
        {
            LinkButton target = (LinkButton)sender;
            RadTreeNode node = (RadTreeNode)target.BindingContainer;
            DataRowView nodeData = (DataRowView)node.DataItem;
     
            if ((int)nodeData["cikktemplate"] != -1)
            {
                target.Visible = true;
            }
            else
            {
                target.Visible = false;
            }
        }
    }

    Thanks.
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 07 May 2013 Link to this post

    Hello,

    I would suggest subscribing for the click event in your InstantiateIn method of your  NodeTemplate class as you have done with the DataBinding event. It will look like this:
    //code behind
    class NodeTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            LinkButton linkR = new LinkButton();
            linkR.ID = "r";
            linkR.Text = "";
            linkR.CssClass = "r";
            
            linkR.Click += new EventHandler(LinkButton_Click);
            container.Controls.Add(linkR);
     
            LinkButton linkC = new LinkButton();
            linkC.ID = "c";
            linkC.Text = "c";
            linkC.CssClass = "c";
            linkC.Click += new EventHandler(LinkButton_Click);
            container.Controls.Add(linkC);
        }
     
        protected void LinkButton_Click(Object sender, EventArgs e)
        {
            //here goes your custom logic when user click on the link button within your node template
        }
    }

    In this case you may differentiate which button was clicked based on the value of the sender parameter. Of course you are free to use two separate click handlers for each button as well. 

    Kind regards,
    Boyan Dimitrov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top