Solved

Select a Treenode from clientside or serverside

Posted on 2009-06-29
2
614 Views
Last Modified: 2012-05-07
Hi All,
Can i set TreeView1.SelectedValue dynamically in serverside or clientside.This is my treeview
<asp:TreeView ID="TreeView1" runat="server" onselectednodechanged="TreeView1_SelectedNodeChanged">
<nodes>
<asp:TreeNode Value="1" Text="Parent1">
<asp:TreeNode Value="11" Text="child11"/>
<asp:TreeNode Value="12" Text="child12"/>
</asp:TreeNode>
<asp:TreeNode Value="2" Text="Parent2"></asp:TreeNode>
<asp:TreeNode Value="3" Text="Parent3"></asp:TreeNode>
</nodes>
</asp:TreeView>

I want to set TreeView1.SelectedValue="11" on pageload .How can i do this?Any Suggestions.
0
Comment
Question by:sastry_sagi
  • 2
2 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24742574
Dude this is so messsed up it makes me wanna throw a tantrem.

Anyway the property for selectednode is readonly (which is crazy) as you probably already figured out. So the only way to set the node is to loop through the treeview.nodes, check their value and if it is the value you want then set it as selected using the node.selected property.

This will only work if you have 1 level of treenodes though. If you try this on a treeview with say 3 levels of nodes you will not have success. I put together a small sample using your treeview that does a recursive check through all node levels, finds the one you want and selects it....enjoy!

PS: I didnt know if you wanted C# or VB so you got what I was working with at the time (vb)
****YOUR ASPX CODE, WITH SELECTED NODE STYLE ADDED SO WE CAN SEE WHAT HAPPENS ****

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

	<asp:TreeView ID="TreeView1" runat="server" ForeColor="Red" SelectedNodeStyle-BackColor="Green">

		<Nodes>

			<asp:TreeNode Value="1" Text="Parent1">

				<asp:TreeNode Value="11" Text="child11" />

				<asp:TreeNode Value="12" Text="child12" />

			</asp:TreeNode>

			<asp:TreeNode Value="2" Text="Parent2"></asp:TreeNode>

			<asp:TreeNode Value="3" Text="Parent3"></asp:TreeNode>

		</Nodes>

	</asp:TreeView>

	</form>

</body>

</html>
 
 

****CODE BEHIND****

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        recursiveNodeCheck(TreeView1.Nodes, "3")

    End Sub
 

    Public Sub recursiveNodeCheck(ByVal nod As TreeNodeCollection, ByVal sValue As String)

        For Each tn As TreeNode In nod

            If tn.Value = sValue Then

                tn.Selected = True

            Else

                recursiveNodeCheck(tn.ChildNodes, sValue)

            End If

        Next

    End Sub

Open in new window

0
 
LVL 20

Accepted Solution

by:
ddayx10 earned 500 total points
ID: 24742585
PSS: Make sure you dont do this before you have bound your control. In your sample it was manually built so it doesnt matter, but if you add/remove to the treeview using code behind then you cant check/set the selectedvalue until after you finish manipulating it.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to Popup 37 79
Javascript - getdate 5 51
Voice recognition ASP or ASP.NET or JavaScript 2 45
Asp.net mvc entity issue 6 19
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now