Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    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.

    Posted: Mar 29 2004, 02:27 PM by HumanCompiler | with 4 comment(s)
    Filed under:

    Comments

    AJ said:

    Excellent, just what I was looking for.
    # March 29, 2004 8:02 PM

    Jerry Pisk said:

    Excelent? Why don't you just use Server.Transfer so you save yourself (and the client) completely unnecessary extra request?
    # March 29, 2004 8:29 PM

    HumanCompiler said:

    1. Notice the title "Quick and Dirty"
    2. My understanding of Server.Transfer, which may be wrong, is that it redirects the request from under the browser's nose, meaning the actual url the user sees in their browser would be wrong. If they save that to their favorites, they'd probably be a bit confused why the wrong page is coming up. Unless I'm understanding how Server.Transfer works.

    ;)
    # March 29, 2004 8:37 PM

    JENN said:

    HI WHOEVER CAN HELP ME!
    I RECENTLY MESSED UP MY SETTING FOR MY INTERNET EXPLORER AND KNOW THE ADDRESS BAR IS AN ICON ON THE RIGHT HAND SIDE OF THE SCREEN
    HOW DO I GET IT TO TURN BACK INTO AN ADDRESS BAR WHERE I CAN TYPE MY WEBSITES IN IT AGAIN!
    # April 30, 2004 3:53 PM