如何在 C# 中执行下一行之前显示警报弹出窗口 asp.net

本文关键字:显示 net asp 窗口 一行 执行 | 更新日期: 2023-09-27 18:32:22

我想在我的 Web 应用程序中显示警报/消息。我尝试了下面的代码,但在执行所有代码行后都会显示警报。

ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 

但是我想显示代码执行行的中间,如果可能的话,请帮助我。.

如何在 C# 中执行下一行之前显示警报弹出窗口 asp.net

正如我已经评论过的:

你不能。RegisterStartupScript 正如您可以阅读的那样,将注册启动脚本而不调用它。此外,仅当客户端收到并处理响应时,才会调用此脚本。可以针对你的方案尝试解决方法。创建一个隐藏按钮,并定义一个在警报到此按钮后要处理的函数。然后在脚本中,显示警报并调用单击此按钮。

您的函数可能如下所示:

public string myFunction(){
  int a = 123;
  .
  .
  .
  ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 
  .
  .
  .
  .
  .
  return str;
}

您必须将此功能分为两部分:

public string myFunction1(){
  int a = 123;
  .
  .
  .
  ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 
  return str;
}
public string myFunction2(){
  .
  .
  .
  .
  .
}

并将myFunction1关联到原始事件,myFunction2关联到隐藏按钮。然后将脚本更新为:

ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message'); document.getElementById('<% hiddenBtn.clientId %>').click()", true); 

这将调用剩余的代码和进程逻辑。

注意:这是一个解决方法。理想情况下,您应该尝试拆分功能并尝试使用后台处理。