Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Returning to a specific place after a post event.

Is there a way to return to a specific place on the form after a post event?  This would be equivalent to using a "a name=#??" anchor and then "a href=#??".  I have a large form with several autopost drop down lists that change the look of the form.  Is there a way to go to an area or set the focus on specific text or control after a post event?

William Nordgren
Friday, March 26, 2004

I'm not sure about ASP.NET, but usually you can do this by setting the action attribute of your form to the address plus anchor.

If you send them to a pre-processing page before hand, you can use the server side language to just redirect them to the page with the anchor link embedded in it:

Response.Redirect("/mypage.asp?t=1&b=2#RESULTS")

Is this what you are looking for?

CF
Friday, March 26, 2004

AFAIK this is a bug in IE, in that if you issue a Redirect which includes a #bookmark, it ignores it. As far as I can remember Mozilla was OK about it. Therefore, a while back I added something like the following to our base Page class

protected void ClientRedirect(string url)
{
  Response.Write("<script type='text/javascript'>");
  Response.Write("window.location=\"+HttpUtility.AttributeEncode(url)+\";");
  Response.Write("</script>");
  Response.End();
}

Duncan Smart
Friday, March 26, 2004

2) Also consider:
<%@Page SmartNavigation="true" ...%>
but it requires IE5.5 or later.

3) or... have a bit of client script store the document.scrollTop during onsubmit (you can register these server side with Page.RegisterOnSubmitStatement() )in a hidden variable, and then during the client's onload set the document.scrollTop to the value of the field (document.forms[0].myScrollTopHiddenField.value).

Duncan Smart
Friday, March 26, 2004

A slightly easier approach would be to use IE's scrollIntoView() function:

Page.RegisterClientScriptBlock("hoopa", "<script language='javascript'>document.anchors['myBookmark'].scrollIntoView()</script>");

Aaron Boodman
Sunday, March 28, 2004

Whoops, forgot to mention that you could easily generalize this in the same way the previous poster did - using the page base-class with a script like:

// filter for IE
if (document.all && location.bookmark.length > 1) {
    var bm = location.bookmark.substring(1);
    document.anchors[bm].scrollIntoView();
}

Aaron Boodman
Sunday, March 28, 2004

Thanks for the suggestions.  I will begin testing now!

This is what I was looking for.

William Nordgren
Sunday, March 28, 2004

Here is my solution:

·    I created a small script to load the window to the correct anchor on the web page.

    <HEAD>
        <script language="javascript" type="text/javascript">
        <!--
        
            function OnLoadNav()
            {
                if (document.getElementById("hNav").getAttribute("value") != "top")
                {
                    window.location.hash = document.getElementById("hNav").getAttribute("value");
                }
            }
            
        //-->
        </script>
    </HEAD>

·    I added an onload event handler to the body tag.

<body onload="OnLoadNav()">

·    I added a hidden field with a runat=”server” attribute.

    <INPUT id="hNav" type="hidden" value="top" name="Nav" runat="server">

When I want the page to return to a specific anchor I set the hidden field value in the server code:

// Return the web page to where we are working using the hNav hidden field
    hNav.Value = "Address";

This is my solution.  If anyone feels that I used a hammer to kill a mosquito or has another way let me know.  I had interesting results when I set the %@Page SmartNavigation="true" ...% setting.

William Nordgren
Saturday, May 01, 2004

*  Recent Topics

*  Fog Creek Home