user35
user35

Reputation: 468

Call a javascript inside SelectedNodeChanged event handler in c#

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.

Upvotes: 3

Views: 1435

Answers (2)

Cheruvian
Cheruvian

Reputation: 5867

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);  

Upvotes: 2

Ali
Ali

Reputation: 2592

Use this one inside the method:

ScriptManager.RegisterStartupScript(this, this.GetType(), 
                                    "anyName", "alert('test');", true);

Upvotes: 2

Related Questions