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

Handling events fired from dynamically created NodeTemplate

4 Answers 70 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Barbaros Saglamtimur
Top achievements
Rank 1
Barbaros Saglamtimur asked on 08 May 2012, 08:09 PM
Hi,

I am adding Node Templates like;
protected void RadTreeView2_TemplateNeeded(object sender, RadTreeNodeEventArgs e)
{
    bool someCondition = true;
    if (someCondition)
        e.Node.NodeTemplate = new BoxTemplate();
    else
        e.Node.NodeTemplate = new OtherTemplate();
}

And here is my BoxTemplate class;
class BoxTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        Label nodeLabel = new Label();
        nodeLabel.ID = "NodeLabel";
        nodeLabel.DataBinding += new EventHandler(NodeLabel_DataBinding);
        container.Controls.Add(nodeLabel);
 
        ImageButton delButton  = new ImageButton();
        delButton.ImageUrl = "~/images/icons/delete16.png";
        delButton.OnClientClick = "return ConfirmDelete();";
        container.Controls.Add(delButton);
     }
 
    private void NodeLabel_DataBinding(object sender, EventArgs e)
    {
        Label theLabel= (Label)sender;
        RadTreeNode node = (RadTreeNode)theLabel.BindingContainer;
        theLabel.Text = (string)DataBinder.Eval(node, "Text");
    }
}

I couldn't figure it out how to add server side click event to my delButton and handle it at code behind.
Any help would be appreciated. TIA.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 May 2012, 07:46 AM
Hi,

Try adding the click event of delButton as follows.

C#:
class BoxTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        .........
        ImageButton delButton = new ImageButton();
        delButton.ImageUrl = "~/Images/bullet5.jpg";
        delButton.OnClientClick = "return ConfirmDelete();";
        delButton.Click += new ImageClickEventHandler(delButton_Click);
        container.Controls.Add(delButton);
    }
 
    void delButton_Click(object sender, ImageClickEventArgs e)
    {
        //your code
    }
}

Hope this helps.

Thanks,
Princy.
0
Barbaros Saglamtimur
Top achievements
Rank 1
answered on 09 May 2012, 11:44 AM
Thanks for your reply, but I couldn't do is handling this event at page.
0
Barbaros Saglamtimur
Top achievements
Rank 1
answered on 09 May 2012, 02:39 PM
Ok I figured it out but there is still something to achieve. Here is my code;
public class NodeTemplate : ITemplate
{
    private readonly ImageButton _theButton;
    public NodeTemplate()
    {
        _theButton = new ImageButton
        {
            ImageUrl = "~/images/icons/delete16.png",
            Width = Unit.Pixel(10),
            Height = Unit.Pixel(10),
            OnClientClick = "return ConfirmDelete();",
            CausesValidation = false,
            CommandName = "Delete"
        };
        _theButton.Attributes.Add("style", "margin-left:10px;");
    }
    public void InstantiateIn(Control container)
    {
        Label label = new Label { ID = "ItemLabel" };
        label.DataBinding += new EventHandler(label_DataBinding);
        container.Controls.Add(label);
        _theButton.DataBinding += new EventHandler(theButton_DataBinding);
        container.Controls.Add(_theButton);
    }
 
    public event ImageClickEventHandler Click
    {
        add { this._theButton.Click += value; }
        remove { this._theButton.Click -= value; }
 
    }
 
    private void label_DataBinding(object sender, EventArgs e)
    {
        Label target = (Label)sender;
        RadTreeNode node = (RadTreeNode)target.BindingContainer;
        string nodeText = (string)DataBinder.Eval(node, "Text");
        target.Text = nodeText;
    }
    private void theButton_DataBinding(object sender, EventArgs e)
    {
        ImageButton target = (ImageButton)sender;
        RadTreeNode node = (RadTreeNode)target.BindingContainer;
        target.CommandArgument = (DataBinder.Eval(node.DataItem, "RecordId")).ToString();
        target.ID = (DataBinder.Eval(node.DataItem, "RecordId")).ToString();
    }
}

And here is the code which I use to assign the template, register and than handle its event at page
protected void RadTreeView2_TemplateNeeded(object sender, RadTreeNodeEventArgs e)
  {
      if (!Page.IsPostBack)
      {
          NodeTemplate t = new NodeTemplate();
          t.Click += new ImageClickEventHandler(theButton_Clicked);
          e.Node.NodeTemplate = t;
      }
  }
  protected void theButton_Clicked(object sender, ImageClickEventArgs e)
  {
      int recordId = int.Parse(((ImageButton) sender).CommandArgument);
      // now I can get in here but...
  }

But if I bind data at Page_Load using if(!Page.IsPostBack), postback occurs but I can't reach theButton_Clicked event. If I bind data every time page loads (by removing if(!Page.IsPostBack)) I can reach theButton_Clicked event but this time RadTreeView looses its state. So I need to reach theButton_Clicked event without loading TreeView data every postback. How can I achieve this?

TIA
0
Princy
Top achievements
Rank 2
answered on 10 May 2012, 08:08 AM
Hi,

Try the following code to fire theButton_Clicked event.

C#:
protected void RadTreeView2_TemplateNeeded(object sender, RadTreeNodeEventArgs e)
  {
          NodeTemplate t = new NodeTemplate();
          t.Click += new ImageClickEventHandler(theButton_Clicked);
          e.Node.NodeTemplate = t;
  }
protected void theButton_Clicked(object sender, ImageClickEventArgs e)
  {
      int recordId = int.Parse(((ImageButton) sender).CommandArgument);
  }

Hope this helps.

Thanks,
Princy.
Tags
TreeView
Asked by
Barbaros Saglamtimur
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Barbaros Saglamtimur
Top achievements
Rank 1
Share this question
or