Click here to Skip to main content
11,433,170 members (58,804 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi friends,

I am working with ASP.Net 2008, I wanna show the selected node in a text box, without postback.
Please help me.

Thanks Advance..


Thanks
Adersh Ram
Posted 3-Aug-12 19:58pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Well, I googled for you. Apparently, if your tree view is called, for example, Adersh, then there's a js object called Adersh_Data. This has a selectedNodeID property, which has a value property.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
This is quite easy to get the selected node of treeview.
Try this:
HTML:
<asp:treeview id="TreeView1" runat="server" xmlns:asp="#unknown">
    <SelectedNodeStyle ForeColor="Black" />
</asp:treeview>
<asp:button id="Button1" runat="server" text="GetSelectedNode" onclientclick="return GetSelectedNode();" xmlns:asp="#unknown" />
CS:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TreeView1.Nodes.Add(new TreeNode("Mango", "Fruit1"));
        TreeView1.Nodes.Add(new TreeNode("Apple", "Fruit2"));
        TreeView1.Nodes.Add(new TreeNode("Pineapple", "Fruit3"));
        TreeView1.Nodes.Add(new TreeNode("Orange", "Fruit4"));
        TreeView1.Nodes.Add(new TreeNode("Grapes", "Fruit5"));
    }
}
JavaScript:
<script type="text/javascript">
function GetSelectedNode() {
    var treeViewData = window["<%=TreeView1.ClientID%>" + "_Data"];
    if (treeViewData.selectedNodeID.value != "") {
        var selectedNode = document.getElementById(treeViewData.selectedNodeID.value);
        var value = selectedNode.href.substring(selectedNode.href.indexOf(",") + 3, selectedNode.href.length - 2);
        var text = selectedNode.innerHTML;
        alert("Text: " + text + "\r\n" + "Value: " + value);
    } else {
        alert("No node selected.")
    }
    return false;
}
</script>
Refer this[^] also.


--Amit
  Permalink  
Comments
Adersh Ram at 4-Aug-12 1:32am
   
Thank you so much _Amy. :)
_Amy at 4-Aug-12 1:35am
   
Welcome. :)
Sweetynewb at 20-Dec-12 1:22am
   
Hi Amit,

Can you tell me how to add new node , below selected node?
i mean new node should be child node of selected node.
Please help me out..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150428.2 | Last Updated 4 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100