Execute code before response.redirect

Tag: asp.net , vb.net , response.redirect Author: newman1234 Date: 2014-04-23

In my code, i want to update records and call EncryptPDF function. And then redirect to another page afterward.

But i found that it will not execute the code before response.redirect.

According to Microsoft information, the code will be terminate it before response.redirect.

How can i force the code to be execute before redirect to another page ?

is there any code can serves as redirect ? Thanks

If Not rec_old Is Nothing Then
    For i = 0 To rec_old.Count - 1

        rec_old.Item(i).CurrentRenumeration = False
        dc.SubmitChanges()

        If (rec_old.Item(i).RemFile Is Nothing Or rec_old.Item(i).RemFile = 0) Then
            EncryptPDF(rec_old.Item(i).RenumID, rec_old.Item(i).SID)
        End If

        If i = rec_old.Count - 1 Then
            LastRecord = True
        Else
            LastRecord = False
        End If
    Next
End If

dc.Dispose()

Response.Redirect("XXX.aspx?SID=" & Request("SID") & "&SSID=" & GetProfile() & "&returnPath=12")
You need to get rid of the End If between Next and Else - I'm surprised the code even runs as I would not expect it to work with an Else following an End If.
hi, just "copy and paste" mistake. there is no Else case

Other Answer1

Try calling Response.Redirect with an additional False attribute -

Response.Redirect("XXX.aspx?SID=" & Request("SID") & "&SSID=" & GetProfile() & "&returnPath=12", False)

Here is the explanation (from here http://forums.asp.net/t/1396869.aspx?Response+Redirect+True+False)

If you are processing page "A" an then you issue a redirect

Response.Redirect('Default.aspx",True)  

The client will be sent the redirect for the new page and Page "A" will immediately stop processing as a thread abort will occur. This is the default behavior of a redirect.

If you issued the redirect as

Response.Redirect('Default.aspx",False) 

Then the client will be sent the redirect for the new page, but Page "A" will be allowed to continue processing. Perhaps page "A" has cleanup work to do or something. The client will never see the results from page "A" as they were redirected.