Link to home
Start Free TrialLog in
Avatar of rema10
rema10

asked on

My selectednodechanged event handler is not firing(ASP.NET, c#)

Hello,

First, I am a newbie...

I have created a treeview programmatically.  The tree shows up fine in the page.  But when I click on a node the tree just disappears from the page and the 'selectednodechanged' event is not fired.

eg: In page_load:
{
if (!IsPostBack)
{
buildtree tree1 = new buildtree();
tvtest = tree1.create_tree();
tvtest.ShowExpandCollapse = true;
tvtest.ShowLines = true;
tvtest.DataBind();
this.Form.FindControl("Panel1").Controls.Add(tvtest);
Panel1.Controls.Add(tvtest);
tvtest.SelectedNodeChanged += new EventHandler(tvtest_SelectedNodeChanged);
        }

void tvtest_SelectedNodeChanged(object sender, EventArgs e)
{

.....
some functions written here to do calculations etc...
..
}


Thanks for your help and time.
Avatar of masterpass
masterpass
Flag of India image

When you are creating the childnodes add this

childnode.SelectAction=TreeNodeSelectAction.SelectExpand;
Avatar of rema10
rema10

ASKER

Thanks for respdoning masterpass. I have that in my code already.   But, that didn't help.  

create your tree in  the paqe _init event
see this link

http://forums.asp.net/p/1458201/3343969.aspx

just read the second post ... I hope that is your problem
Avatar of rema10

ASKER

Thanks both of you.  

I tried both the solutions and I tried some of my own too - but didn't work.
1.  creating inthe page_init event: didn't help.
2.  setting the navigateurl property to null: didn't help.

Just so you know:
the event-function is called if I use the 'TreeNodeExpanded' event.

So, is there something that I should be adding/writing to know that the node is being selected/clicked?  

Avatar of rema10

ASKER

ADDITONALLY..

Another FYI:  Here is also something that I noticed.

When I click on a node, there is a postback event.  After the postback event, I check the 'watch window' for the contents of the tree and it shows as null.

So, pls let me know where I am going wrong..

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of xav056
xav056

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rema10

ASKER

Thanks much.  That worked for me.

I am still trying to understand placing the server controls in the page_init vs the page_load event.  I was reading the msdn website on the page's life cycle.  But, if you have simple words that can explain why the tree should be created in the page_init event etc. please let me know.  

Again, thanks and appreciate your help and time.
When dealing with dynamic controls, you have to make sure your controls arecreated in the page init, this is where the stateview would be loaded, it would be too late to load the view state in the page load event and thus if you are creating dynamic controls in the page load you will loose the state.
Hope this will give you a simple methodology to follow
Avatar of rema10

ASKER

Thanks Sagir for the follow-up comment.  This helps me to understand the 'why' and will help me in my future applications.