This is a migrated thread and some comments may be shown as answers.

RadTreeView - Events inside node template

1 Answer 100 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Gergely
Top achievements
Rank 1
Gergely asked on 03 May 2013, 11:05 AM
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.

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 07 May 2013, 02:41 PM
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.
Tags
TreeView
Asked by
Gergely
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or