Link to home
Create AccountLog in
Avatar of MAIGUI
MAIGUI

asked on

How to get the clientid a treenode object in a treeview

Hello,

I am using an asp.net treeview control. I need to find out what the clientid of a given TreeNode object is. To get the clientid of the treeview itself is as simple as calling treeview.clientid, however, there doesn't seem to be such a property propery for a  treenode object. I have been searching everywhere to no avai.l. Any help on this is greatly appreciated.

This is how I am adding the nodes to the tree:

 Dim Rootnode As New TreeNode("TheRoot", "TheRoot")
 TreeView1.Nodes.Add(Rootnode)

Dim childNode As New TreeNode("cars", "1")  
Rootnode .ChildNodes.Add(childNode)
'Trying to get the clientid of this node

childNode As New TreeNode("boats", "2")
Rootnode .ChildNodes.Add(childNode)

Avatar of surajguptha
surajguptha
Flag of United States of America image

When you add a new node try setting the Tag property or the name property of the node to that of a primary key of the node
Avatar of MAIGUI
MAIGUI

ASKER

Thank you for your reply, I was not able to find a tag or name property for this class, however, there is a text and a value property. I tried setting the value property to the PK (100794 in this case) and the code is rendered to the client as the following:

<td style="white-space:nowrap;"><a class="TreeView1_0" href="javascript:__doPostBack('TreeView1','sTheRoot\\100794')" onclick="TreeView_SelectNode(TreeView1_Data, this,'TreeView1t2');" id="TreeView1t2">boats</a></td>

The thing is, with the way that the treeview renders this. It appears that I would not be able to access this node from javascript by name or id. The tree is rather large and I would prefer not to resort to iterating through all hrefs on the page and checking the innerHTML of each link to match, however, I may have to end up doing that if there is not a better way.

Thanks
If you dont mind doing a round trip to the server, you could implenent the same without javascript.
Avatar of MAIGUI

ASKER

Actually getting the clientid of the node is a means to an end. The reason I was thinking to get the clientid is because I am programatically expanding a particualr node on the server side, however, when the page renders the user has to scroll to the middle of the page to see the expanded node. I thought if I could get the clientid I could use Javascript to set the scrollbar position.  If there is a way to set the scrollbar via server side code, that would be be great too.
>>On Submit of the form you can store the current position of the tree control to a hidden variable and on load you can read the hidden variable and set the position back

>> You can also try switching "on" smart navigation. It kind of attempts to set the screen the way it was before the submit.

Avatar of MAIGUI

ASKER

Thank you surajguptha, I know we're gonna get this thing tackled, I can feel it. Let me just clarify the problem a little. This all happens on an initial load, no submit occuring at this point. I can post the actual code tomorrow when I get to work.
Avatar of MAIGUI

ASKER

Never mind, I figured this out. If you turn on EnableClientScript on the treeview, the treeview will render to the browser as treeviewName_Data. Then you can call treeviewName_Data.selectedNodeID.value to the the selected node. Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account