单击 Javascript 上的取消确认框不会阻止网格刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8566504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:50:16  来源:igfitidea点击:

Clicking on cancel on Javascript Confirm box does not stop the grid from refreshing

c#javascriptasp.net

提问by R S

How to stop the page from rendering the .cscode on click of cancel button on javascript confirm box?

.cs单击javascript确认框上的取消按钮时,如何阻止页面呈现代码?

I have a button click event and in some IFcondition I am asking the user to confirm. Now when the user clicks on OK button, I have to bind the new data in a grid. but if the user clicks on cancel button, it should not refresh the grid with new data.

我有一个按钮点击事件,在某些IF情况下我要求用户确认。现在,当用户单击“确定”按钮时,我必须将新数据绑定到网格中。但是如果用户点击取消按钮,它不应该用新数据刷新网格。

Question: On click of cancel button on JavaScript confirm box, how can I stop the following code to be executed, or how can I just return/break from JavaScript?

问题:单击 JavaScript 确认框上的取消按钮时,如何停止执行以下代码,或者如何仅从 JavaScript 返回/中断?

Can anyone please help me out with this? Thanks in advance.

任何人都可以帮我解决这个问题吗?提前致谢。

Markup

标记

<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" OnClick="btnCopy_Click" ValidationGroup="Copy" />

Code

代码

public void btnCopy_Click(object sender, EventArgs e) { 
    if (Convert.ToInt32(hidId.Value) > 0) {
        string scriptString = "<script language='JavaScript'> ";
        scriptString += "if (confirm('Are you sure to proceed?') == false) {return} ";
        scriptString += "</script>";
        Page.RegisterStartupScript("Confirm", scriptString);
        BindDataGrid();
    }
}

回答by rick schott

You are mixing server and client side code in the same event, it has to be separated:

您在同一个事件中混合服务器和客户端代码,它必须分开:

Script:

脚本:

function Confirm()
{
    var record=confirm('Are you sure to proceed??');
    if(record == 1)
    {
        return true;
    }
    else if(record == 0)
    {
        return false;
    }
 }

Markup:

标记:

<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" 
    OnClick="btnCopy_Click" OnClientClick='return Confirm();' ValidationGroup="Copy" />

Code-behind:

代码隐藏:

public void btnCopy_Click(object sender, EventArgs e) { 
    if (Convert.ToInt32(hidId.Value) > 0) {      
        BindDataGrid();
    }
}

回答by Chris Disley

You'll need to catch the response, for example:

您需要捕获响应,例如:

if (window.confirm)
{
//handle the OK
}
else
{
//handle the cancel
}

回答by Sam Sussman

Please show the code, but without seeing your code I would guess you are not returning the response?

请显示代码,但如果没有看到您的代码,我猜您没有返回响应?

<input type="button" onclick=" return confirm('is this right?'); ">

回答by Vosobe Kapsimanis

You can simply returnfrom the code or exit(0).

您可以简单地return从代码或exit(0).

function fillGrid()
{
 if( confirm("....") )
 {
  //populate your grid
 }
 else
 {
   return;
 }

 //do anything after the grid is populated
 //and exit the function
}

回答by Piyey

You are showing the confirm dialog in the codebehind of your button click, that wont stop the flow of your code.

您正在按钮单击的代码隐藏中显示确认对话框,这不会停止您的代码流。

You need to show the confirm dialogbox before do the postback

您需要在回发之前显示确认对话框

<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="true" OnClientClick="return confirm('sure?');" ValidationGroup="Copy" />