3

I have my treenode populated with data from database.The treeview code is as follows:

<asp:TreeView ID="mytv" runat="server" ImageSet="Arrows" 
        ondatabinding="Page_Load" onselectednodechanged="mytv_SelectedNodeChanged">

And the code-behind for this is as follows:

protected void mytv_SelectedNodeChanged(object sender, EventArgs e)
{
  // how to call java-script function from here.
}

What I am trying to achieve is to display the content of div as per the tree-node clicked using JavaScript.

Or is there any other way to display the content from database or from div on clicking the treeview node.

2 Answers 2

2

Use this one inside the method:

ScriptManager.RegisterStartupScript(this, this.GetType(), 
                                    "anyName", "alert('test');", true);
Sign up to request clarification or add additional context in comments.

2 Comments

What is that anyname and what does it do?
It's a unique identifier for the script block, you could name it anything that you like.
2

I just reread your post, if you're just trying to get the selected value from the treeview, you can use something more like this in your JS

function CheckTreeValue()
{
    var treeView = document.getElementById('treeviewID');
    if(treeView.selectedNodeID.value != null)
    {
        var selectedNode = document.getElementById(treeView.selectedNodeID.value);
        //Get Whatever you need from the node
        var text = selectedNode.text;
        WebService.PullValue(text, callback);
    } 
    else // No Node Selected
        return;
}

You can create the script manually by abusing an

<asp:Literal>

But it is better to use ScriptManager

ScriptManager.RegisterStartupScript(
    this,
    this.GetType(), 
    "UniqueScriptKey", 
    "FunctionYouWantToCall();
     alert(document.getElementById('OrAnyJavascript').innerHTML);", 
    true);  

5 Comments

But can we use the attributes like selectedNodeId of treeview inside javaScript function.I don't think its possible. Is it?
Is selectedNodeID a predefined attribute or you made it because i can find it nowhere.Are you using any treeview attributes or methods in your javascript function.Can we use any treeview attribute or method inside the javascript function?
Should be built into ASP.NET don't remember where I found the documentation for it, but here is the official for getting selected node link
Getting the selected node is not what i am seeking.It can be done as TreeView tv = sender as TreeView; string nid = tv.SelectedNode.Text; .But it is done inside the function of the .cs file not inside the function of javascript. You are mistaken friend you cannot use the predefined attributes of asp.net inside the javascript function.
please make sure the code you have provided is valid.If not edit id because it is already two vote up.Other user might get offended if found wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.