Javascript notifications from c # class

I have a 5 page aspx "master". they each contain a "SaveAndExit" button that performs a C # function in a shared static class. After saving, the C # code is redirected to another page.

Is there a way to run javascript: alert ('Data Saved'); after saving and before loading a new page.


source to share

3 answers

You will need to register the script to run on postback in order to issue a warning. Then redirect via javascript after the warning.



You cannot do it exactly the way you want it.

C # (server side) code cannot work until the page is posted back and unloaded from the browser. Thus, by the time your server-side code is executed, the page doesn't even exist as far as the browser is concerned, and it's too late for the javascript code to run. This is true even in the middle of a post back, and even if the return post result is the same page. This is why, in some cases, ASP.Net pages may flicker during messages.

So you cannot force a javascript alert after saving the data without executing it through some ajax response, and that is definitely one option. Another option is for the page you are redirecting to show certain "data stored" under certain circumstances; run it with something simple like a session variable or hidden input value on boot so that it isn't obvious to the user.

But I think it's probably best to do nothing. It looks like if the failure fails, you won't be redirecting. You will probably even see some kind of error. If your user doesn't have enough confidence in your application that they don't trust him even when there is no error message and he went to the next step, you're in trouble.



Vic, if this isn't homework, I'd really like you not to.

The Javascript warning is very annoying for most users and seems completely useless in this case as explained by Joel Coehorn.

If you insist on showing the message when you save it, consider adding perhaps a session variable, and on the redirected page display the "Data saved" message at the top if the session variable exists, then remove the session variable.

Again, as Joel Coehorn said, you should definitely show a message if there is an error, but the redirect should be all the "proof" they need in order for their data to be saved.



All Articles