tags:

views:

613

answers:

1

I have an ASP.NET 3.5 page with some AJAX, some Validators and some business-rule validation done in the code-behind page (VB.NET).

The Validators set focus to the 'offending' control when an error is detected.

When a business rule is violated, I used the following code to generate some javascript "on the fly" and execute it to simulate the Winforms "MessageBox.Show" functionality:

Sub DisplayMessages()
    Dim lblError As New Label
    lblError.Text = "<script language='javascript'>" & Environment.NewLine & "window.alert('"
+ strError + "')</script>"
    Page.Controls.Add(lblError)
    strError = "" 
End Sub

(and the appropriate calling of that subroutine when necessary)

Now, when the user hits the "Save" button and everything is ok, the page stays scrolled down at the bottom where the Save button is. I want to give the user an indication that things finished successfully by crolling the page back to the top.

I tried to adapt the previous use of javascript to this with another subroutine - adding the control didn't seem to work so I tried to just put the raw code in to execute ad hoc:

Sub ScrollPageToTop()
    Dim strScroll As String
    strScroll = "<script language='javascript'>" & Environment.NewLine & "window.scrollTo(0,0);</script>"
    ClientScript.RegisterStartupScript(Me.GetType, "ScrollToTop", strScroll.ToString)
End Sub

At the end of the "btnSave.Click" code, if everything went ok, calling that subroutine.

What I get is the page scrolling to the top and then back to where it was when the Save button was clicked.

For various reasons, 'MaintainScrollPositionOnPostback="True"' is in the Page directives at the top of the .aspx code. What I'd really like is to make that FALSE for just a moment so that the page resets at the top - again, only if everything is ok and only for this button (there are other buttons on the page that require the page to keep it's scroll position).

A: 

Assuming on a successful save, you have no other reason to maintain the state of the current form, you could simply redirect to the same page and pass a successful flag.

Response.Redirect ("CurrentPage.aspx?Saved=Y");

Use the querystring parameter to make your success message visible.

You may have to be more creative with building the redirect url to capture the item you are editing but this might work for you.

HectorMac
Sometimes the simplest answers work best.
David