Solved

Scroll to named hyperlink on page load

Posted on 2004-09-14
4
601 Views
Last Modified: 2008-03-03
How do I cause the web page to scroll to a named hyperlink on page load?

I have an area on the page where a user uses a listbox to select sub-categories until they reach the desired level. Above this listbox I have a "bread crumb trail" of hyperlinks to the higher levels:

CurrentLevel : Rooty > Level 1 > Level 2 (Root, Level 1, Level 2 are hyperlinks back to this page)

[Listbox of subcategories for current selection] (subcategories for Level 2 in this example).

Since this "control" is towards the bottom of the page, when a user selects either the links, or a selection in the list box the page reloads and the page is positioned at the top.

I can get the hyperlinks to work by appending a #category to the link and adding a <a name=category></a> tag.

But how do I accomplish something similar when an entry in the listbox is selected (the listbox has AutoPostBack=true)

Thanks.
0
Comment
Question by:sirk_a
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
allanau20 earned 250 total points
ID: 12059134
Hi sirk_a:

Try this:

    Page.RegisterStartupScript("GoToUsrCtrl", "<scriptlanguage='javascript'>self.location.href='#category'</script>")

on the where the listbox is triggered.  It will jump to where '#category' is placed on your page.

HtH.
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 12059349
   Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Response.Redirect(Request.ServerVariables("script_name") & "#" & DropDownList1.SelectedValue)
    End Sub
0
 
LVL 5

Expert Comment

by:allanau20
ID: 12059510
I'm pretty sure that if you use response.redirect, you'll lose your viewstate.  Not sure if that's what you want.

However, if you use  Page.RegisterStartupScript you won't lose your viewstate.  Taking the above example, you can do this:

  Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Page.RegisterStartupScript("GoToUsrCtrl", "<scriptlanguage='javascript'>self.location.href='#category'</script>")
    End Sub

HtH.
0
 
LVL 3

Author Comment

by:sirk_a
ID: 12059749
allanau20 - Thank you. The RegisterStartupScript was what I needed.


 hismightiness - Since allanau20's worked I did not pursue yours. Although I would need viewstate maintained and I agree that the response.redirect would have lost that.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Technology Resume 7 68
Please explain purpose of GZIP 4 58
SSRS Deployment problem 5 94
Phone Does Not Abide By CSS Breakpoint For Navigation Controls 6 32
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall‚Ķ
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question