Link to home
Start Free TrialLog in
Avatar of Donna Kainer
Donna KainerFlag for United States of America

asked on

How do you return to a specific location on a page after leaving the page using a NavigateURL?

I have a Repeater on Page1.aspx which uses the NavigateURL property to jump to Page2.aspx which displays detail information associated with the item in the Repeater. I am able to return to PAGE1.aspx from Page2.aspx, but I return at the top of my Repeater list. Is there a way to return to the location in the list from which I left?
Avatar of Rouchie
Rouchie
Flag of United Kingdom of Great Britain and Northern Ireland image

You can link to an HTML element with a particular ID value.  So, to jump to a certain H2 heading on page2 you would write

<h2 id="index123">Heading 123</h2>

Then in page1 you would create your links to look like this:

<asp:HyperLink runat="server" ID="lnk1" NavigateUrl="page2.aspx#index123" Text="Link Text" />
I think the idea about #index123 is on the right track, however I have always seen this used with the anchor tag instead of the id attribute of the h2 tag.

I would probably try it this way:

<a name="index123">
<h2 id="whatever">Heading 123</h2>
</a>
NAME is no longer supported in XHTML and above, which is why I opt for ID now, which has to be referenced within anchor tags using #
Avatar of Donna Kainer

ASKER

As a newbee some of this discussion is going over my head.

Page1.asp contains a Repeater which lists over 200 rows of information. On each row a NavigateURL that point to Page2.asp?Cust_Code=codevalue.

When you click on one of the values in a Repeater row on Page1.asp you are taken to Page2.asp which is opened containing the detail information concerning Cust_Code=codevalue.

The first response from Rouchie is interesting, but I am not trying to jump from Page1 to a specific place in Page2. What I am trying to do is jump from a specific place on Page1, which is generated by a Repeater, to Page2 (which shows the detail) and then I want to return to the place within the Repeater on Page1 from which I left.

An example of what I am trying to do would be something like a job posting website. You are presented with a list of jobs from which to chose. If you click on one, you go to the detail for that job. After reviewing the detail you return to the first web page, which contains the list of job postings, at the location in the list of job postings containing the specific posting you were looking at. Their method of providing the list may be more complex than can be accomodiated using a Repeater. If so, please offer some suggestions or links, that a newbee can understand, on how to accomplish this slight of hand.
Sorry for the delay, I have been away all week.

Before I show the ASP.NET method (that I would use), you might also try the javascript BACK trick.  Because the BACK command in the browser always returns the user to the part of the page where they left, this might work.  In page2.aspx, remove the NavigateUrl property that sends them to page1.aspx, and replace it with something like this:

<asp:HyperLink ID="MyLinkOnPage2" runat="Server" NavigateUrl="#" onclick="onClick="history.go(-1)" Text="Back" />

Open in new window


If that doesn't work, what you need to do is reference the current ID of the hyperlink on page1, pass that into page2, then use it in page2 to amend your page2 links.  

So in your repeater's code-behind you can do something like this:

Protected Sub rpt_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rpt.ItemDataBound
	Select Case e.Item.ItemType
		Case ListItemType.Item, ListItemType.AlternatingItem
			Dim MyHyperLink As HyperLink = CType(e.Item.FindControl("MyHyperLinkID"), HyperLink)
			MyHyperLink.NavigateUrl = "Page2.asp?Cust_Code=" & e.Item.DataItem("codevalue").ToString & "&senderID=" & MyHyperLink.ClientID
	End Select
End Sub

Open in new window


That will create a Hyperlink with a NavigateUrl of the actual link you are clicking on, with your codevalue also in the querystring.

Then on page2.aspx, in Page.Pre_Render you can read the querystring, and append it to any links that are on that page.  So say you have a HyperLink called Page2Link, you can do this:

Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
	If Request.QueryString("senderID") IsNot Nothing AndAlso Request.QueryString("senderID").ToString.Trim <> String.Empty Then
		Page2Link.NavigateUrl = Page2Link.NavigateUrl & "#" & Request.QueryString("senderID").ToString.Trim
	End If
End Sub

Open in new window


Pre_Render is the last Sub to run on a page, so it runs after all data has been loaded into the page.  So we find the hyperlink back to page1, and then add the ID of the link on page1, so when you navigate back, the page will use the technique from my original post to return to that exact spot.
The javascript BACK trick did not work. There is a syntax problem with the onclick="onClick="history.go(-1)" portion of the code. I tried changing it to onclick="onClick='history.go(-1)'" but that did not work.

I would really like to use your perferred method, but I am not certain what to do.

My Page1.asp Repeater hyperlink code right now is:  
<td>
    <asp:HyperLink runat="server" target="_blank" ID="Hyperlink1"
                            NavigateUrl='<%# "CustomerDetailForm.aspx?Cust_Code=" + DataBinder.Eval(Container.DataItem,"Cust_Code")%>'   
                            Text='<%#DataBinder.Eval(Container.DataItem, "Cust_Code")%>' 
                        />
</td>

Open in new window


How do I change it so that I build the hyperlink using the Protected Sub rpt_ItemDataBound you described above?
Just try the Js version first it's far easier! Try replacing NavigateUrl="#" with

NavigateUrl="javascript:history.go(-1)"
ASKER CERTIFIED SOLUTION
Avatar of Donna Kainer
Donna Kainer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not going to pursue this matter any further.