简体   繁体   中英

Message After Redirecting VB.Net

I redirect the user to the same page but I would like for a popup message stating the error. I cannot use messagebox because it does not work in the app. I tried Response.Write did not work. I have put a label with the error message but I do not know how to make it appear when the page reloads because of the error.

Could I use page_load, and IsPostBack? If so, how can I tell that it is reloading because of the catch error? Or if you know a better way...

Idea I have when error happens about the 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

My code to redirect the user when error happens:

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

You could attach an error code to the URL as a parameter when you redirect. Then on page load, check if the parameter is there and deal with it as you see fit.

Yes, MsgBox won't work when you publish your app to the server but you can make a pop up message with client side script. In this example, I force an error to happen and show you the error message in a pop up message that should run fine on your server.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM