Is it possible to pass variable as parameter in javascript function?

1.7k views Asked by At

I have this function:

 protected void mytv_SelectedNodeChanged(object sender, EventArgs e)
 {
    TreeView tv = sender as TreeView;
    var nid = tv.SelectedNode.Value;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(nid)", true);
}

As you can see, I have a variable nid that gets different value as 1,2,3 according to the treenode clicked.
Can i pass that nid variable value directly as a parameter to the javascript function as show(nid)?
If not what do i do?

My javascript function is as follows:

function show(nid) 
{

    if (nid == 4) {
        alert('testing');
    }
    else
     {
         alert('what???');
     }
}
2

There are 2 answers

0
Zaki On BEST ANSWER

try concatenating :

ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(" + nid + ")", true);
0
Robert Fricke On
//for string value
ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", string.Format("show('{0}')", nid), true);
//for int value
ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", string.Format("show({0})", nid), true);