按下按钮按ASP.NET运行javascript

时间:2017-03-22 13:42:05

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

我想调用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);
}

1 个答案:

答案 0 :(得分: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);
}