views:

181

answers:

3

I have a 5 aspx page "wizard". each one contains a "SaveAndExit" button that executes a c# function on a common static class. After saving , the c# code redirects to another page.

Is there a way for running javascript: alert('Data Saved'); after the saving, and before new page is Loaded.

+2  A: 

You'll have to register a startup script on postback to do the alert. Then redirect via javascript after the alert.

Kon
A: 

You can't do it exactly like you want.

The C# code (server-side) can't run until the page has already posted back and unloaded from the browser. So by the time your server-side code is running the page doesn't even exist any more as far as the browser is concerned and it's too late to run your javascript code. This is true even in the middle of a post back, and even if the result of the post back will be the same page. That is why in certain circumstances ASP.Net pages can appear to flicker during post backs.

So you can't force a javascript alert after the data is saved without doing it via some kind of ajax call back-- and that's certainly one option. Another choice is to have the page to which you redirect show some kind of "data saved" message in certain circumstances; trigger it with something simple like a session variable or the value of hidden input on load so it's not obvious to the user.

But I think probably the best thing to do is nothing at all. It sounds like if the save fails you won't redirect. You'll probably even show some kind of error. If your user doesn't have enough confidence in your app that they don't trust it even when there's no error message and it's moved on to the next step, you're in trouble.

Joel Coehoorn
He wants to alert the user BEFORE the redirect to the next page.
Kon
And AFTER saving the data. That's really not an option without adding an extra page view in there- pick one or the other.
Joel Coehoorn
It is very much so an option - as I said, register a startup script on the postback, will alert the user. Only after that alert will the redirect occur. It's very possible and a very popular option.
Kon
That means using javascript to do the redirect, and I'm pretty sure he was thinking a server-side redirect.
Joel Coehoorn
He said 'c# code redirects', but c# code can still determine what the redirect target should be. It doesn't have to redirect on the server... you're talking technical solution while redrawing the business objective.
Kon
A: 

Vik, unless this is a homework I would highly suggest you don't.

Javascript alert are very annoying to most users and seem completely useless in this case as explained by Joel Coehoorn.

If you insist on showing a message when it is saved then think of adding maybe a session variable and on the redirected page show the "Data saved" message at the top if the session variable exist and then delete the session variable.

Again though, as Joel Coehoorn said, you should definately show a message if there is an error but redirecting should be all the "proof" they need that their data was saved.