【问题标题】:Is it possible to pass variable as parameter in javascript function?是否可以在javascript函数中将变量作为参数传递?
【发布时间】:2014-07-01 08:44:36
【问题描述】:

我有这个功能:

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

【问题讨论】:

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


    【解决方案1】:
    //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); 
    

    【讨论】:

      【解决方案2】:

      尝试连接:

      ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(" + nid + ")", true);
      

      【讨论】:

        猜你喜欢
        • 2018-10-20
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 2014-03-07
        • 1970-01-01
        • 2014-04-15
        • 2020-02-19
        • 2010-12-14
        相关资源
        最近更新 更多