Handling events fired from dynamically created NodeTemplate

5 posts, 0 answers
  1. Barbaros Saglamtimur
    Barbaros Saglamtimur avatar
    110 posts
    Member since:
    Jul 2012

    Posted 08 May 2012 Link to this post

    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.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 May 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Barbaros Saglamtimur
    Barbaros Saglamtimur avatar
    110 posts
    Member since:
    Jul 2012

    Posted 09 May 2012 Link to this post

    Thanks for your reply, but I couldn't do is handling this event at page.
  5. Barbaros Saglamtimur
    Barbaros Saglamtimur avatar
    110 posts
    Member since:
    Jul 2012

    Posted 09 May 2012 Link to this post

    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
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 May 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017