vik
vik

Reputation:

JavaScript alerting from a C# class

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.

Upvotes: 0

Views: 358

Answers (3)

tbate
tbate

Reputation:

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

JavaScript alert is very annoying to most users and seems 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 definitely show a message if there is an error but redirecting should be all the "proof" they need that their data was saved.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

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.

Upvotes: 0

Kon
Kon

Reputation: 27451

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

Upvotes: 2

Related Questions