c# - 是否可以在javascript函数中将变量作为参数传递?

标签 c# javascript asp.net treeview tree-nodes

我有这个功能:

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

如您所见,我有一个变量 nid,它根据单击的树节点获得不同的值 1,2,3。
我可以将该 nid 变量值作为参数直接传递给 javascript 函数作为 show(nid) 吗?
如果不是,我该怎么办?

我的javascript函数如下:

function show(nid) 
{

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

最佳答案

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

关于c# - 是否可以在javascript函数中将变量作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24505967/

相关文章:

javascript - DrawImage() 不在 Canvas 上绘制

asp.net - ASP.NET 中的 C++/CLI 组件出现 "module could not be found"错误

c# - SMTP邮件发送

c# - 为什么使用 "as"运算符进行转换会导致使用 C# 动态转换的对象为 null

Javascript JSON 未知行为

javascript - 如何在离开页面时添加类并在返回同一页面时删除该类?

C# 枚举 - 根据掩码检查标志

c# - 如何重置 DropDownList 索引

c# - IHttpAsyncHandler 和 IObservable 网络请求

asp.net - 是否有与 Tomcat 等效的 IIS?