Quick and Dirty Navigation State in ASP.NET
There are cleaner ways to do this, but thought this was handy so I'd throw it out...
Just add the following code to your Base Page Class (if you have one that your pages inherit from) or add them as Shared (static) to any Class.
Public Sub RedirectFrom(ByVal DefaultPage As String)
Dim Context As HttpContext = HttpContext.Current
If Context.Request.QueryString("from") <> "" Then
Context.Response.Redirect(Common.BasePath & Context.Server.UrlDecode(Context.Request.QueryString("from")))
Else
Context.Response.Redirect(DefaultPage)
End If
End Sub
Public Function GetUrlFrom() As String
Dim Context As HttpContext = HttpContext.Current
Dim Temp As String = Context.Request.RawUrl
Return Context.Server.UrlEncode(Temp.Substring(Context.Request.ApplicationPath.Length, Temp.Length - Context.Request.ApplicationPath.Length))
End Function
On any links you have on your page that need to be redirected back to this page when finished, such as an edit page, add “&from=” & GetUrlFrom() on the end of the link. Then on each page you want to return back to it's previous page, just do something like RedirectFrom(”Default.aspx”) where that string is the page it will go back to in case it doesn't find a from path in the querystring. This should help smooth things out and make it more like a modal dialog in WindowsForms almost, but in the same browser window.