英文:
Confirmation Message before closing the page (asp.net, vb.net, webapp)
问题
I know message box is only for window apps, but what about for web apps. I would like to ask help on how to do this on web apps, confirm first from the user when he/she clicks [Sign out] button, if he/she really wants to sign out with a [yes] or [no] button. Thank you!
英文:
I know message box is only for window apps, but what about for web apps. I would like to ask help on to do this on web apps, confirm first from the user when he/she clicks [Sign out] button, if he/she really wants to sign out with a [yes] or [no] button. Thank you!
答案1
得分: 1
以下是翻译好的部分:
你不能在代码后台完成这项任务,因为客户端与用户进行交互,而不是与服务器交互。尽管如此,你可以为客户端提供执行所需操作的指令。你只需将它与Javascript结合使用。
然后,你需要Javascript部分:
<script type="text/javascript">
function Confirmation(message, target) {
if (confirm(message)) {
__doPostBack("Confirmation", target);
}
}
</script>
最后,在 Page_Load
中,检查确认是否已确认:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If IsPostBack Then
If Request.Params.Get("__EVENTTARGET") = "Confirmation" Then
Select Case Request.Params.Get("__EVENTARGUMENT")
Case "ConfirmSignOut"
DoConfirmationFunctionality()
End Select
End If
End If
End Sub
DoConfirmationFunctionality
应该包含你要执行的代码。
英文:
You (sort of) can't do this in code-behind, because the client interacts with the user, not the server. That said, you can give instructions for the client to do what you want. You just have to combine it with Javascript.
Protected Sub ConfirmBox(msg As String, callback As String, title As String)
Dim confirmScript As String = String.Format("Confirmation('{0}','{1}');", msg, callback)
ScriptManager.RegisterStartupScript(Me, Me.GetType, "WindowsScript", confirmScript, True)
End Sub
Then you need the Javascript:
<script type="text/javascript">
function Confirmation(message, target) {
if (confirm(message)) {
__doPostBack("Confirmation", target);
}
}
</script>
Finally, in Page_Load
, check if your confirmation is confirmed:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If IsPostBack Then
If Request.Params.Get("__EVENTTARGET") = "Confirmation" Then
Select Case Request.Params.Get("__EVENTARGUMENT")
Case "ConfirmSignOut"
DoConfirmationFunctionality()
End Select
End If
The DoConfirmationFunctionality
should have the code you want to perform.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论