Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jump to another part of the same page after a postback 1

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
0
0
Is there a way with ASP.NET that after the page is post back jump to another location of the same page? For example, when a user clicks on a LinkButton control in ‘offer_details.aspx’ page, jump to a location of the page so that is visible a specified table from the bottom of the page.

Thank you
 
Yes, there is. One way is to turn smartNavigation on and the page will jump down to the control that caused the postback.

Another way is to use your own javascript function to focus the relevant control and use RegisterStartUpScript to register the function e.g.
Code:
    Private Sub FocusControl(ByVal ctrl As Control)
        Dim sbFocus As New StringBuilder
        sbFocus.Append("<script language='javascript'>" & Environment.NewLine)
        sbFocus.Append("document.getElementById('" + ctrl.ClientID & "').focus();" & Environment.NewLine)
        sbFocus.Append("</script>")
        Page.RegisterStartupScript("FocusScript", sbFocus.ToString)
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
SmartNavigation... I see, it’ s interesting, and I see that it works. But I though that in order to maintain scroll position I had to implement a javascipt function like in this article:
Your suggestions are interesting. But to be more exact, what I am trying to accomplish is: When user presses a Linkbutton a postback occurs and a table is made visible (appears) below that control. The idea I want to implement is that when that Linkbutton is at the bottom of the page, and user click it, the table is displayed completely from the bottom, instead of the table appears hided below the visible page and user has to scroll down to see it.

What do you think?
 
If you want to scroll to the bottom of the table, the easiest solution will be to add an anchor tag after the table e.g.
Code:
<a id="bottomOfTable" />
Then, you can simply focus that object like in my example above e.g.
Code:
    Private Sub FocusTableBottom()
        Dim sbFocus As New StringBuilder
        sbFocus.Append("<script language='javascript'>" & Environment.NewLine)
        sbFocus.Append("document.getElementById('bottomOfTable').focus();" & Environment.NewLine)
        sbFocus.Append("</script>")
        Page.RegisterStartupScript("FocusScript", sbFocus.ToString)
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I tried what you said (add an anchor tag after the table) but it doesn’ t work. Instead I wrote table id (<table id="my_table" ... >), and then the LinkButton onclick event triggers:
Code:
Sub whole_table_bottom(Sender As Object, e As System.EventArgs)

   Dim SetLocation As New StringBuilder
   
    SetLocation.Append("<scr" & "ipt language='javascript'>")
    SetLocation.Append("function SetScrollLocation () {")
	SetLocation.Append("[b]document.getElementById('my_table').focus();[/b]")
    SetLocation.Append("}")
	SetLocation.Append("document.body.onload=SetScrollLocation ;")
    SetLocation.Append("</scr" & "ipt>")
  
    RegisterStartupScript("setLoc", SetLocation.ToString())

End Sub

And works fine! :)

Thank you very much,
Cesar
 
Strange, my example was taken from a new test page that I created to make sure it worked before I posted (and it worked fine). I've just created a new page again just to make sure and it still works (in both IE6 and Firefox 1.0.7).

Also, with your example, you shouldn't need the following line as the script will be called on the load of the page anyway:
Code:
SetLocation.Append("document.body.onload=SetScrollLocation ;")
You also appear to be concatenating your opening and closing "script" tags rather strangely (it doesn't matter as it will still work, it just looks rather odd!).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm - your didn't even compile when i tried it. will you double check your test page, please?
 
What section doesn't compile?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
it was the </script> tag, i had to break it up. it doesn't jump the the location, however
 
cesark,
did you get your solution to work? after reading the article you posted, i cannot set the value of __SCROLLLOC correctly, it is always zero. i think that the onscroll event is not triggering. any idea's?
 
That will most likely be IE not understanding the javascript focus method for the anchor tag. You could simply replace the focus line with this
Code:
    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] FocusTableBottom()
        [COLOR=blue]Dim[/color] sbFocus [COLOR=blue]As[/color] [COLOR=blue]New[/color] StringBuilder
        sbFocus.Append("<script language='javascript'>" & Environment.NewLine)
        [b]sbFocus.Append("window.location.href='#bottomOfTable'" & Environment.NewLine)[/b]
        sbFocus.Append("</script>")
        Page.RegisterStartupScript("FocusScript", sbFocus.ToString)
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
briancostea,

Instead of implementing the hidden field in this way: RegisterHiddenField("__SCROLLLOC", "0"), try to simply put a hidden field after the form tag, thus and here:
Code:
...
  <form id="test_page" runat="server">
  <input type="hidden" id="__SCROLLLOC" value="0" runat="server">
...
 
i discovered that the onscroll() event is on being triggered. i'm not sure why, maybe it has something to do with the version of ie i am using? for some reason the SaveScrollLocation function is not being called. how did you get yours to work?
 
My code:

Code:
Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then
  ....
 
  Dim SaveScrollLocation As New StringBuilder
  Dim SetScrollLocation As New StringBuilder

  SaveScrollLocation.Append("<scr" & "ipt language='javascript'>")
  SaveScrollLocation.Append("function SaveScrollLocation () {")
  SaveScrollLocation.Append("document.offer_detail.__SCROLLLOC.value = document.body.scrollTop;") 
  SaveScrollLocation.Append("}")
  SaveScrollLocation.Append("document.body.onscroll=SaveScrollLocation ;")
  SaveScrollLocation.Append("</scr" & "ipt>")
  
  RegisterStartupScript("saveScroll", SaveScrollLocation.ToString())

  
  If Page.IsPostback Then
    
    SetScrollLocation.Append("<scr" & "ipt language='javascript'>")
    SetScrollLocation.Append("function SetScrollLocation () {")
    SetScrollLocation.Append("document.body.scrollTop = " & Request("__SCROLLLOC") & ";")
    SetScrollLocation.Append("}")
	SetScrollLocation.Append("document.body.onload=SetScrollLocation ;")
    SetScrollLocation.Append("</scr" & "ipt>")
  
    RegisterStartupScript("setScroll", SetScrollLocation.ToString())
  
  End If
  
End Sub

And then that input hidden field.
 
I' m sorry it lacked that 'End If' (in bold):

Code:
Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then
  ...
 [b]End If[/b] 

  Dim SaveScrollLocation As New StringBuilder
  Dim SetScrollLocation As New StringBuilder

  SaveScrollLocation.Append("<scr" & "ipt language='javascript'>")
  SaveScrollLocation.Append("function SaveScrollLocation () {")
  SaveScrollLocation.Append("document.offer_detail.__SCROLLLOC.value = document.body.scrollTop;") 
  SaveScrollLocation.Append("}")
  SaveScrollLocation.Append("document.body.onscroll=SaveScrollLocation ;")
  SaveScrollLocation.Append("</scr" & "ipt>")
  
  RegisterStartupScript("saveScroll", SaveScrollLocation.ToString())

  
  If Page.IsPostback Then
    
    SetScrollLocation.Append("<scr" & "ipt language='javascript'>")
    SetScrollLocation.Append("function SetScrollLocation () {")
    SetScrollLocation.Append("document.body.scrollTop = " & Request("__SCROLLLOC") & ";")
    SetScrollLocation.Append("}")
    SetScrollLocation.Append("document.body.onload=SetScrollLocation ;")
    SetScrollLocation.Append("</scr" & "ipt>")
  
    RegisterStartupScript("setScroll", SetScrollLocation.ToString())
  
  End If
  
End Sub
 
thanks, cesark but no dice. the SaveScrollLocation () code is not being executed.
 
Here's a semi-reliable alternative that uses no javascript. thread855-754344
 
ok, well it does use javascript... sorry guys, I am confused today. %-)
 
That's also the last method I suggested above [smile]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

i amde the changes you suggested, but no luck. i have <a id="bottom" /> at the bottom of my page. the script executes, but the page is still at the top. the address, however, is index.aspx#bottom. quite the mystery?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top