简体   繁体   English

来自 C# 类的 JavaScript 警报

[英]JavaScript alerting from a C# class

I have a 5 ASPX page wizard .我有一个 5 ASPX 页wizard Each one contains a SaveAndExit button that executes a C# function on a common static class.每个都包含一个SaveAndExit按钮,该按钮在公共静态类上执行 C# 函数。 After saving, the C# code redirects to another page.保存后,C# 代码重定向到另一个页面。

Is there a way for running javascript: alert('Data Saved');有没有办法运行javascript: alert('Data Saved'); after the saving, and before new page is Loaded.保存后,加载新页面之前。

You'll have to register a startup script on postback to do the alert.您必须在回发时注册一个启动脚本才能发出警报。 Then redirect via javascript after the alert.然后在警报后通过 javascript 重定向。

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.在页面已经回发并从浏览器卸载之前,C# 代码(服务器端)无法运行。 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.因此,当您的服务器端代码运行时,就浏览器而言,该页面甚至不再存在,并且运行您的 javascript 代码为时已晚。 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.这就是为什么在某些情况下 ASP.Net 页面在回发期间可能会闪烁的原因。

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.因此,如果不通过某种 ajax 回调,您就不能在保存数据后强制执行 javascript 警报——这当然是一种选择。 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.如果您的用户对您的应用程序没有足够的信心,以至于即使没有错误消息,他们也不信任它,并且已进入下一步,那么您就有麻烦了。

Vik, unless this is a homework I would highly suggest you don't. Vik,除非这是家庭作业,否则我强烈建议你不要。

JavaScript alert is very annoying to most users and seems completely useless in this case as explained by Joel Coehoorn.正如 Joel Coehoorn 所解释的那样,JavaScript 警报对大多数用户来说非常烦人,并且在这种情况下似乎完全没用。

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.如果您坚持在保存时显示一条消息,那么可以考虑添加一个会话变量, if会话变量exist ,则在重定向页面的顶部显示“数据已保存”消息,然后删除会话变量。

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.不过,正如 Joel Coehoorn 所说,如果出现错误,您绝对应该显示一条消息,但重定向应该是他们保存数据所需的所有“证据”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM