move back to bottom of page after postback

I have a fairly long page with a submit button, a validation summary and a
status label at the bottom of the page. I need it to go back to the bottom
after the post so that the user can see any status information.

I found some code that is supposed to do this but I cannot get it to work.
it compiles and runs but does not move me down to the bottom fo the page.
Here's my VB code that runs at the end of a sub where I post data back to
the database:

Dim Script As New StringBuilder()
Script.Append("<script language=JavaScript id='BookMarkScript'>
")
Script.Append("var hashValue='#BottomOfPage'; ")
Script.Append("if(location.hash!=hashValue) ")
Script.Append("location.hash=hashValue; ")
Script.Append("</script>")

If (Not
ClientScript.IsClientScriptBlockRegistered("BookMarkScript") ) Then
ClientScript.RegisterClientScriptBlock(Me.GetType,
"BookMarkScript", Script.ToString())
End If

Here's the anchor that is near the bottom of the page:

<a name="#BottomOfPage"></a>


Can anyone tell me what I'm doing wrong?

Thanks,

Keith
Keith G Hicks [ Di, 15 April 2008 05:44 ] [ ID #1942593 ]

Re: move back to bottom of page after postback

"Keith G Hicks" <krh [at] comcast.net> wrote in message
news:%23GQGbrqnIHA.4208 [at] TK2MSFTNGP02.phx.gbl...

> Can anyone tell me what I'm doing wrong?

You're using RegisterClientScriptBlock.

This places the JavaScript at the beginning of the webform i.e. just after
the opening <form> tag. This means that (almost certainly) the rest of the
form including the anchor tag hasn't been created when the script tries to
run.

Use RegisterStartUpScript instead, as this places the JavaScript at the end
of the webform i.e. just before the closing </form> tag.

Also, there's no need to include the <script> and </script> tags explicitly,
as ASP.NET will do this for you, e.g.

Dim Script As New StringBuilder()
Script.Append("var hashValue='#BottomOfPage';")
Script.Append("if(location.hash!=hashValue)")
Script.Append("location.hash=hashValue;")

If (Not ClientScript.IsClientScriptBlockRegistered("BookMarkScript") ) Then
ClientScript.RegisterStartUpScript (GetType(), "BookMarkScript",
Script.ToString(), True)
End If


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
mark [ Di, 15 April 2008 15:13 ] [ ID #1942612 ]

Re: move back to bottom of page after postback

Thanks Mark. That did it. I also had to removee the # from the <a
name="#BottomOfPage"></a>


"Mark Rae [MVP]" <mark [at] markNOSPAMrae.net> wrote in message
news:#EvhPqvnIHA.3512 [at] TK2MSFTNGP03.phx.gbl...
> "Keith G Hicks" <krh [at] comcast.net> wrote in message
> news:%23GQGbrqnIHA.4208 [at] TK2MSFTNGP02.phx.gbl...
>
> > Can anyone tell me what I'm doing wrong?
>
> You're using RegisterClientScriptBlock.
>
> This places the JavaScript at the beginning of the webform i.e. just after
> the opening <form> tag. This means that (almost certainly) the rest of the
> form including the anchor tag hasn't been created when the script tries to
> run.
>
> Use RegisterStartUpScript instead, as this places the JavaScript at the
end
> of the webform i.e. just before the closing </form> tag.
>
> Also, there's no need to include the <script> and </script> tags
explicitly,
> as ASP.NET will do this for you, e.g.
>
> Dim Script As New StringBuilder()
> Script.Append("var hashValue='#BottomOfPage';")
> Script.Append("if(location.hash!=hashValue)")
> Script.Append("location.hash=hashValue;")
>
> If (Not ClientScript.IsClientScriptBlockRegistered("BookMarkScript") ) Then
> ClientScript.RegisterStartUpScript (GetType(), "BookMarkScript",
> Script.ToString(), True)
> End If
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
>
Keith G Hicks [ Di, 15 April 2008 15:48 ] [ ID #1942615 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » move back to bottom of page after postback

Vorheriges Thema: AJAX Script Manager, 3.5, and Master Page Woes
Nächstes Thema: RegularExpression Validation for password in ASP.NET