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

Handling button click events in ITemplate object used for NodeTemplate

1 Answer 166 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nicholas Cloud
Top achievements
Rank 1
Nicholas Cloud asked on 12 Feb 2010, 09:18 PM
Hello, I have a custom ITemplate class that I am using to format the output of nodes in a RadTreeView.  In the template, I have several asp:Button objects.  I want to wire up the Click events of each, and handle them on the page with AJAX calls to server-side methods.  I have added event handlers for each button to the ITemplate object but as far as I can tell they never fire.  When I click the buttons on the page I get an error message that tells me that ViewState has failed to load, but nothing else happens.  Do I need to do anything special to the page in order to handle the events?

.aspx page markup

<telerik:RadAjaxManager ID="radAjaxManager" runat="server"
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="radOrgNodeTree"
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="radOrgNodeTree" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
    </AjaxSettings> 
</telerik:RadAjaxManager> 
 
<telerik:RadTreeView ID="radOrgNodeTree" runat="server" 
    AllowNodeEditing="false" 
    OnClientLoad="tree.load" 
    OnClientNodeClicking="tree.nodeClicking" 
    OnClientNodeClicked="tree.nodeClicked" 
    OnClientContextMenuShowing="tree.contextMenuShowing" 
    OnClientMouseOver="tree.mouseOver" 
    OnClientContextMenuItemClicked="tree.contextMenuItemClicked" 
    > 
    <ContextMenus> 
        <telerik:RadTreeViewContextMenu  
            ID="orgNodeContextMenu"  
            runat="server"  
            ClickToOpen="true"  
            Skin="Vista"
            <Items> 
                <telerik:RadMenuItem Text="Add New..." Value="add" /> 
                <telerik:RadMenuItem Text="Edit" Value="edit" /> 
                <telerik:RadMenuItem Text="Delete" Value="delete" /> 
                <telerik:RadMenuItem Text="Collapse All" Value="collapse" /> 
            </Items> 
        </telerik:RadTreeViewContextMenu> 
    </ContextMenus> 
</telerik:RadTreeView> 

code behind
        protected void Page_Load(object sender, EventArgs e) { 
 
            if(!IsPostBack) { 
 
                TreeNodeTemplate treeNodeTemplate = new TreeNodeTemplate(); 
                treeNodeTemplate.Command += treeNodeTemplate_Command; 
                radOrgNodeTree.NodeTemplate = treeNodeTemplate; 
                //set data source 
            } 
 
            radOrgNodeTree.DataBind(); 
        } 
 
        private void treeNodeTemplate_Command(object sender, CommandEventArgs e) { 
            //... 
        }

template class

private class TreeNodeTemplate : ITemplate { 
 
        public event CommandEventHandler Command; 
 
        private void OnCommand(CommandEventArgs e) { 
 
            if (Command != null) { 
                Command(this, e); 
            } 
        } 
 
        public void InstantiateIn(Control container) { 
            //... 
 
            Button saveButton = new Button(); 
            //set properties 
            saveButton.Command += saveButton_Command; 
            //data binding event handler 
            //add to controls collection 
 
            //... 
        } 
 
        private void saveButton_Command(object sender, CommandEventArgs e) { 
            OnCommand(e); 
        } 
    } 





1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 18 Feb 2010, 03:27 PM
Hi Nicholas,

Your code looks correct. We have used to to create a test page and everything worked fine. Please, download the attached sample and use it for reference. Note, that when adding controls in InstantiateIn, you have to specify the ID:
public void InstantiateIn(Control container)
  {
      //...  
      Button saveButton = new Button();
      saveButton.ID = "saveButton1";
      saveButton.Text = "save";
       
      //set properties  
      saveButton.Command += saveButton_Command;
      //data binding event handler  
      //add to controls collection  
      container.Controls.Add(saveButton);
      //...  
  }

Could this be the missing piece of the puzzle?

Kind regards,
Peter
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Nicholas Cloud
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or