1
votes

I just switched to using the CSS Adapters for my TreeView control on one my ASPX pages. Now, for some reason, I cannot use the SelectedNode method except in my OnClick() event. ALL other events show that the SelectedNode is NULL. I am currently using a bunch of non-visible labels to store tree info everytime someone clicks on a node. Very, very, very wrong way to use this control. What is the secret to getting the control in code behind when CSS Adapters are enabled?

1

1 Answers

1
votes

The adapter for the TreeView control does have some viewstate handling but you are right the selected node property is null on postback.

If you hover over the root node the status bar on the browser will show something like:

    javascript:__doPostBack('controlid', 'eventargument');
//or
    javascript:__doPostBack('tv', 'srootnodevalue\\childnodevalue');

You can catch the event argument in the code behind which describes the node you are accessing and its value as a string like 'node\value'.

        if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTARGUMENT"] != null)
        {
            if (Request.Form["__EVENTTARGET"].Equals("tv")) //tv is my treeview control id, and the first parameter in __doPostBack()
                Tv_SelectedNodeChanged(Request.Form["__EVENTARGUMENT"]);
        }


        protected void Tv_SelectedNodeChanged(string argument)
        {
            //process the argument string 'srootnodevalue\childnodevalue'
        }

The treeview viewstate seems aware of the selected node by the adapter's representation of it as class="AspNet-TreeView-Leaf AspNet-TreeView-Selected" when it renders.

Still, there may be a nicer way of doing this...