I am having the same issue, and the responses to this question seem to be
missing the point entirely. I've seen this question in a few places on the
web, and their answers also seem to miss the point. I'm starting to wonder
if I'm crazy, although I can see that others are having the same problem I am.
In my case, I have a hierarchy of facilities that I need to manage. I have
a TreeView on the left side of the page showing the hierarchy, and three
buttons to the right: "Add," "Edit," and "Delete." The user first selects a
site in the TreeView, then clicks one of the three buttons to the right to
indicate what they want to do with the site. Only after that button click do
I want a PostBack to the server.
This actually works properly using the pre-.NET2.0 TreeView (i.e., the
Microsoft IE WebControls TreeView control).
With the .NET 2.0 TreeView, I can't for the life of me figure out how to
select a node (i.e., have its style change to SelectedNodeStyle, and its ID
recorded as the SelectedNode client-side) without an immediate PostBack.
It's as if the TreeView engineers never fathomed that someone would want to
do *anything* besides (a) immediately PostBack, or (b) immediately navigate
away from the page, when a user selects a node in the TreeView. I know this
can't be right. (And this is oversimplified -- by setting
<node>.SelectAction = TreeNodeSelectAction.Expand or .None, I can get
client-based behavior -- but not the behavior I want.)
Investigating this problem, I decided to take a peek inside the TreeView
JavaScript library and see what's going on there. I did a View Source and
found a couple of "<script src=" statements, and the second one turned out to
be the one emitted for TreeView pages... so I plugged that URL directly into
my Address bar and saved the .js file to my desktop.
Inside this file, among other things, appears a definition for:
"TreeView_SelectNode(data, node, nodeId)". This function is actually called
from within my .aspx page. The problem is that, while I can see the emitted
JavaScript for the call
[TreeView_SelectNode(ctl00_Content_ctl00_tvOutages_ Data,
this,'ctl00_Content_ctl00_tvOutagest5');], I have no idea how to write C#
code on the server that will emit the proper parameters for each individual
TreeNode. (In case you're wondering, I have a MasterPage, and this call
occurs from within a ContentPlaceHolder called "Content".)
The TreeView_SelectNode function does three things: (1) un-applies the
"selected" style from the currently-selected node, if any; (2) applies the
"selected" style to the newly-selected node, and (3) records the ID of the
newly-selected node in a JavaScript structure intended to track TreeView
state on the client (passed into the function as the "data" parameter).
I don't want to write an entire parallel JavaScript function for selecting
nodes when I know that there's one built into the TreeView's JavaScript
library. Does anyone know how this library works well enough to help write
the appropriate calls to do client-side node selection? Or is there a better
way to do this that I'm just missing?
Thanks,
-Bryan
"Matt MacDonald" wrote:
Hi All,
I have a form that displays hierarchical categories in a treeview. Ok so
far so good. What I was to do is have users be able to select a node in the
treeview as part of filling out the form. I only want to allow single
selection, so using checkboxes is out of the question. It works as is, but
it makes the form very cumbersome if every time that a user selects a node,
the whole page has to reload. Is there a way to have a node become selected
without using postback? I tried setting the navigateurl of all the nodes to
"javascript:void(0);" as was suggested in one post. This works, however, if
a node is currently selected in the tree via server side code, this doesn't
de-select that node. This also doesn't work very well in non-IE browsers.
I also started to experiment using ICallbackEventHandler to use server side
functions without reposting, but that doesn't seem to work in this scenario
(I don't think). Does anyone have any way of getting around this? I really
wish the autopostback property had been built into the treeview like in
other serverside data controls.
Thanks in advance for any help,
Matt