Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I have a button click event and in some IF condition 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.

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?

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();
    }
}
like image 579
R S Avatar asked Dec 19 '11 19:12

R S


People also ask

What value is returned when a confirm box is Cancelled in JavaScript?

Confirm Box If the user clicks "Cancel", the box returns false.

How does Confirm work in JavaScript?

The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. message is the optional string to be displayed in the dialog.

How to close Confirm box in JavaScript?

Confirm box in JavaScript is used to take the permission from the user by clicking the Ok button or Cancel button. If we click Ok button then action will move to next step, if we click Cancel button then it will terminate the action there itself.

What does confirm box return in JavaScript?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .


2 Answers

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();
    }
}
like image 113
rick schott Avatar answered Oct 19 '22 10:10

rick schott


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

if (window.confirm)
{
//handle the OK
}
else
{
//handle the cancel
}
like image 33
Chris Disley Avatar answered Oct 19 '22 11:10

Chris Disley