【问题标题】:Run javascript on button press ASP.NET按 ASP.NET 在按钮上运行 javascript
【发布时间】:2017-03-22 22:01:16
【问题描述】:

我想调用 javascript 函数以防我背后的代码中发生某些事情。

如果它类似于下面的代码,则在回发警报窗口显示并正确显示后它可以正常工作。但是,如果我从 else 块中删除注释,那么 else 块中的这两个脚本都不会发生?

我可以从代码隐藏中执行多少这样的操作有任何限制吗?

if (condition) {
  if (condition2) {
    var message = "It happened !";
    Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('" + message + "')", true);
  }

} else {
  var msg = "It does not work like that";
  Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('" + msg + "!')", true);
  //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true);
}

【问题讨论】:

    标签: javascript c# asp.net code-behind


    【解决方案1】:

    这样就可以了。正如函数名所说,它注册了启动脚本,所以你改变它而不是插入 2。这样它会做两个 ^^

    if (condition)
    {
        if (condition2)
        {
            var message = "It happened !"; 
            Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('"+message+"');", true);
        }
    }
    else
    {
        var msg = "It does not work like that";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!'); alert('" + msg + "');", true);
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!')", true);
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true);
    }

    【讨论】:

    • 那么可以注册的启动脚本个数是1,那么多个函数的调用方式是把它们串成一个字符串呢?在第二个 if 语句中没有声明一个注释 msg。 //是的,它有效。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2011-02-24
    • 1970-01-01
    • 2011-06-16
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多