TreeNode.Value - where can I find it in a client-side script?

H

Homer J. Simpson

Hi all,

I'm trying to persist the expanded/collapsed state of a TreeView's nodes to
cookies. I've managed to accomplish this using OnTreeNodeCollapsed() and
OnTreeNodeExpanded(). I'm using TreeNode.Value as the name of the cookie,
and values of either 0 (collapsed) or 1 (expanded) for the cookie's value.
At any given point later on, I can rely on the cookie's value to determine
whether I should call TreeNode.Collapse() or TreeNode.Expand() as I'm
rebuilding the tree.

This works great, but requires a server round-trip. I'd prefer to avoid
that if at all possible, especially since the tree's default behavior works
so nicely and entirely client-side without these two OnTreeNodeXXX()
overrides.

In my quest to accomplish this, I've stumbled upon the following blog at
http://daniellarson.spaces.live.com...ogview&_c=BlogPart&partqs=amonth=5&ayear=2006
....scrollling down to the bottom, the last entry suggests a somewhat clean
method to intercept the client-side TreeView_ToggleNode() and inject your
own code (and still call the expand/collapse client script generated by the
framework). The available parameters in the example are named 'data',
'index', 'node', 'lineType' and 'children'. It looks like 'children'
contains the div containing the child nodes, and I may be able to use its
style.display property to determine whether the current node is expanded or
collapsed (it'll be set to either 'block' or 'none').

The only thing missing for all these pieces to fall into place is the string
I assigned (server-side) to TreeNode.Value--I'll be damned if I can find it
anywhere in these parameters! I need this for the name of the cookie.

I've tried looking for alternatives to using TreeNode.Value on the client,
but I can't use the node's unique ID, generated automatically server-side,
either (eg, "tvnSomething##"), because this name appears to be tied with the
node's position in the tree--which would be screwy if I start inserting or
reordering nodes in my tree. The TreeNode.Value property seems to make the
most sense.

All this to say: in my TreeView_ToggleNode() override, where's the value
mapped to TreeNode.Value?
 
G

Guest

Hi,
unfortunatelly, TreeNode.Value is used only if you want to do postback. This
is common way how the node is renderd (3rd level node) if you don't populate
NavigateUrl :

<table cellpadding="0" cellspacing="0" style="border-width:0;">
<tr>
<td>
<div style="width:20px;height:1px"></div>
</td>
<td>
<div style="width:20px;height:1px"></div>
</td>
<td>
<img
src="/TreeView/WebResource.axd?d=ltoyXpSCaVbV2lw9MLsY4MjWwOZEdN72dgmRzP4DJw1&t=632878758459657744" alt="" />
</td>
<td style="white-space:nowrap;">
<a class="myTreeView_0"
href="javascript:__doPostBack('myTreeView','sroot\\1\\1_3')"
onclick="TreeView_SelectNode(myTreeView_Data, this,'myTreeViewt5');"
id="myTreeViewt5">
ThirdChild
</a>
</td>
</tr>
</table>

As you can see node contains
href="javascript:__doPostBack('myTreeView','sroot\\1\\1_3')" where 1_3 is
value of current node, 1 is value of parent node and sroot is value of root
node. If you pass any value to navigate url, for example
http://www.google.com, this postback call will be simple overriden to
href="http://www.google.com" and you will never get the value.

Important: Postback with values is used only if node SelectAction is Select
or SelectExpand.

I hope this will answer your question where you can find node's value.

Regards,
Ladislav
 
H

Homer J. Simpson

I hope this will answer your question where you can find node's value.

Yeah, I guess. Further research was indicating the value wasn't getting
sent to the browser at all if there weren't any postbacks.

FWIW, I've worked around the problem (to a certain extent) by looking at the
state of the tree's nodes on Page_Load() instead. If IsPostBack is true, I
save the state in cookies at that point and add those cookies to Response.
The only drawback is that if you switch a node's state and there's no
further postback after that, you lose *that* change...but I can live with
that for now, I suppose.

Further details on the design of this--I have a row of button at the top of
the screen; clicking them reloads the same page (but with a different
parameter value). Then based on the value of that parameter, the tree has
different content, eg:

Button1 -> page.aspx?id=1
Button2 -> page.aspx?id=2
Button3 -> page.aspx?id=3

Page_Load():
switch( id ) {
case 1: tree contains nodes A, B, C; expand/collapse nodes as per cookie
settings
case 2: tree contains nodes D, E, F; expand/collapse nodes as per cookie
settings
case 3: tree contains nodes G, H, I; expand/collapse nodes as per cookie
settings
}

So the tree itself is not responsible for any postback
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top