重定向VB.Net后的消息

时间:2015-08-06 23:58:03

标签: asp.net vb.net

我将用户重定向到同一页面,但我想要一条说明错误的弹出消息。我不能使用messagebox,因为它在应用程序中不起作用。我试过Response.Write没用。我已经添加了带有错误消息的标签,但我不知道如何在页面由于错误重新加载时显示它。

我可以使用page_load和IsPostBack吗?如果是这样,我怎么能告诉它因为捕获错误而重新加载?或者如果你知道更好的方式......

关于page_load发生错误的想法:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    //This could be in IsPostBack if needed.
    If (the catch error happen) then 
     Show Label next to the button.
    EndIf

    If Page.IsPostBack Then


    End If

我的代码在发生错误时重定向用户:

ElseIf e.CommandName = "Remove" Then
        Try
            Dim dto As PDto = MyPage.DelegateP.GetPDtoByPrimaryKey(Me.OPId)
        If dto.Id > 0 Then
            dto.LUser = MyPage.LUser
            MyPage.DelegateP.DeletePDto(dto)
        End If
        Me.PanelOtherPForm.Style("display") = "none"
        Me.FormViewDetails.Style("display") = "none"
        Me.CurrentOPId = 0
        Me.Repeater1.DataBind()
        BubbleOPChanged()
        Catch ex As Exception
            Response.Redirect(String.Format("/Default.aspx?i={0}&c={1}", MyPage.DtoPage.PageID, Me.CId))
        End Try

2 个答案:

答案 0 :(得分:1)

您可以在重定向时将错误代码附加到URL作为参数。 然后在页面加载时,检查参数是否存在并按您认为合适的方式处理。

答案 1 :(得分:0)

是的,当您将应用发布到服务器时,MsgBox无法正常工作,但您可以使用客户端脚本制作弹出消息。在此示例中,我强制发生错误,并在弹出消息中显示错误消息,该消息应该在您的服务器上正常运行。

Try
    'Force an error to happen for testing
    Dim test As Integer
    test = "test"
Catch ex As Exception
    Dim errorText As String
    errorText = Replace(ex.Message, """", "")   'Script will error if we don't replace double quotes
    errorText = Replace(errorText, "'", "")     'Replace single quotes, too
    ClientScript.RegisterStartupScript(Me.[GetType](), "testAlert", "alert('" + errorText + "');", True)
End Try