【问题标题】:How to show a JavaScript popup when an exception occurs in a WebMethod当 WebMethod 中发生异常时如何显示 JavaScript 弹出窗口
【发布时间】:2011-12-15 15:45:12
【问题描述】:

我想让用户知道在处理查询时是否有错误。错误应该使用 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

【问题讨论】:

标签: javascript vb.net exception


【解决方案1】:
Page.RegisterStartupScript("ClientScript", String.Format("<script>alert('Fout: Data not saved {0}{1}');</script>", vbCrlf, ex.Message))

上面的方法应该可以解决问题,但它有点老了。还有其他方法可以做到这一点,您可能需要 Google RegisterStartupScript 并阅读它。只需确保在放置时将 Throw 注释掉即可。

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 2014-01-24
    • 2013-07-13
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多