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

How to assign NodeClick handler in Code Behind?

1 Answer 108 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Marcus
Top achievements
Rank 1
Marcus asked on 06 Dec 2013, 01:29 AM

Hi There

I have master/content page with two ContentPlaceHolders: Nav and Main. both Nav and Main contain UserControls, where Nav has a navigation UserControl (RadPanel with RadTreeView) and Main has a RadGrid (kind of obvious, right ;-) ).

Obviously the objective is to populate the RadGrid with data dependent on the node that is clicked.

Instead of bubbling up events from the user controls, which I find awkward, I thought I could put the NodeClick Event Handler in the code behind on the content page and assign the event handler in the page load event:



protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolderNav");
            RadPanelBar rpb = (RadPanelBar)cph.FindControl("RadPanelBar1");
            RadPanelItem itm = (RadPanelItem)rpb.Items[1];
            UserControl uc = (UserControl)itm.FindControl("Nav");
            RadTreeView rtv = (RadTreeView)uc.FindControl("RadTreeView2");
            rtv.NodeClick += RadTreeView1_NodeClick;
        }
    }


The code works - when stepping through I can see that the TreeView is found - but the event doesn't fire when clicking on a node (-> a breakpoint in RadTreeView1_NodeClick in the content page is not reached).

Is what I'm trying to do not possible? Or does this just need to go somewhere else in the page lifecycle (Page_Init?).

Thx in advance for help and/or pointers!

Rgds - Marcus.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Dec 2013, 04:14 AM
Hi Marcus,

In order for the NodeClick event to fire, a 'postback' should be made when a Node is clicked. Therefore the PostBack property of Nodes which fire the NodeClick event should be set to True. Please try the following code snippet which works fine at my end and you can also write the code inside the Page_Init event.

C#:
protected void Page_Load(object sender, EventArgs e)
{
    UserControl user = (UserControl)this.Master.FindControl("WebUserControl1");
    RadPanelBar panel = (RadPanelBar)user.FindControl("RadPanelBar1");
    RadTreeView tree = (RadTreeView)panel.FindItemByText("Item1").FindControl("RadTreeView2");
    tree.NodeClick += new RadTreeViewEventHandler(tree_NodeClick);
}
void tree_NodeClick(object sender, RadTreeNodeEventArgs e)
{
    Response.Write("Fired" + e.Node.Text);
}

Let me know if you have any concern.
Thanks,
Shinu.

Tags
TreeView
Asked by
Marcus
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or