views:

343

answers:

3

I'm building an ASP.NET website and I'm puzzled with the behavior of one page, I've got a long form and a submit button, I've got the piece of javascript below in the page to handle scrolling the page back up upon submiting the form, the first time I click the submit button it works all the sequent clicks don't work at all, any idea why?

<script type="text/javascript">
    $(".thebutton").click(function(){
       $("html, body").animate({scrollTop: 200}, 1000); 
    });
</script>

Cheers, Thi

A: 

If your Submit button performs a full postback, I would view source on the second web page instance, and make sure that the script is still there.

Robert Harvey
Doesn't perform a full postback it's using ajax to submit the form data, either way I had already checked and the javascript is still there.
Thi
A: 

Your script will run BEFORE the Post.

Imagine if you will:

Initial Page Load Bind Click event with jQuery

Click Button jQuery Click is raised html and body scrollTop are set to 200 during the "animate" the form is submitted

asp.net back end click event code is run page is reloaded.

string script = "$(function() { $('html, body').scrollTop(200); });";
ClientScript.RegisterStartupScript(this.GetType(), "scrollTop", script, true);

That should take care of what you want to accomplish.

Hope it helps.

Aaron
It doesn't work either, the first execution (click) it works just fine, subsequent ones it doesn't.
Thi
A: 

Ahh,

Using an Ajax post makes this different that my post above.

Does your ajax call change the buttons on the page? I assume you are using an UpdatePanel with the buttons in qustion in it.

Since when you make the Ajax call, the controls in the UpdatePanel are being rebuilt, the DOM is seeing them as different objects and these new objects are no longer bound to the jQuery click function. You will need to re-bind these buttons click event after the ajax post to re-enable the functionality you are looking for.

Using jQuerys new "Live" Handlers should do the trick for you:

$(".thebutton").live("click", function() {
      $("html, body").animate({scrollTop: 200}, 1000);
});

Hope it helps.

Aaron
That worked beautifully =) I've got one left over issue now, I scroll back up because the user will either enter a new registry or will read the error messages in the validation summary, the scroll is scrolling just fine to the right place, although when the validation summary is shown the scroll position changes moving down the position of the screen. Any ideas?
Thi
$("form").submit(function() { $('html, body').scrollTop(200); });or $("form").live('submit', function() { $('html, body').scrollTop(200); });might be more suitable for what you are looking for. the asp validators run on form submit, which happens AFTER the button click event. You will want your function to be tied to the form submit so that it will run AFTER the validators.
Aaron
replace my .scrollTop code wth your animate code :)
Aaron
Not always the validation summary has a quick response when you submit a form, so sometimes the scroll happens before the validation summary is processed and shown/hidden and the scroll end ups leading to the wrong place. Any other ideas?
Thi