如何在WebMethod中出现异常时显示一个JavaScript弹出窗口

How to show a JavaScript popup when an exception occurs in a WebMethod

本文关键字:一个 窗口 JavaScript WebMethod 异常 显示      更新时间:2023-09-26

我想让用户知道在处理查询时是否有错误。错误应该用javascript在弹出窗口中显示。

使用我的代码,Throw new exception确实工作。信息不显示

有谁能帮帮我吗?我的html代码:
<script type="text/javascript" language="javascript"><br />
    Function gotoSave() {
        PageMethods.saveMaster(deData, tipe, OnRequestComplete, OnRequestError);
    } 
    Function OnRequestComplete(result, userContext, methodName) {
        if (result != '') { 
            alert(result); 
        }
    }
    Function OnRequestError(error, userContext, methodName) {
        if (error != null) { 
            alert(error.get_message()); 
        }
    }
</script>

我的代码后面(VB):

<System.Web.Services.WebMethod()> _
Public Shared Sub saveMaster(ByVal deData As String, ByVal tipe As String)
  ..
  ..
  Try
      cmd.ExecuteNonQuery()
  Catch ex As Exception
      Throw New Exception("Fout: Data not saved" & vbCrLf & ex.Message.ToString)
  End Try
End Sub
Page.RegisterStartupScript("ClientScript", String.Format("<script>alert('Fout: Data not saved {0}{1}');</script>", vbCrlf, ex.Message))

上面的方法应该可以达到目的,但是它有点过时了。还有其他方法可以做到这一点,你可能想要谷歌RegisterStartupScript并阅读它。只要确保在放置该代码时注释掉Throw。